-
Notifications
You must be signed in to change notification settings - Fork 31
/
print.asm
1045 lines (793 loc) · 13.2 KB
/
print.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;
; ZX Diagnostics - fixing ZX Spectrums in the 21st Century
; https://github.com/brendanalford/zx-diagnostics
;
; Original code by Dylan Smith
; Modifications and 128K support by Brendan Alford
;
; This code is free software; you can redistribute it and/or
; modify it under the terms of the GNU Lesser General Public
; License as published by the Free Software Foundation;
; version 2.1 of the License.
;
; This code is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
; Lesser General Public License for more details.
;
; print.asm
;
; Routines for printing a string to the screen.
; Control codes AT, PAPER, INK, BRIGHT
; and INVERSE are handled as you'd expect.
; HL holds the location of the string to print
; Defines for in-string control codes
define LEFT 8
;define RIGHT 9
define CRLF 10
;define UP 11
define CR 13
define TAB 14
define ATTR 15
define INK 16
define PAPER 17
define FLASH 18
define BRIGHT 19
define INVERSE 20
define TEXTBOLD 21
define TEXTNORM 22
define AT 23
define WIDTH 24
; Tokens (C0-FE)
define TKN_SPECTRUM 0xc0
define TKN_ROM 0xc1
define TKN_SPANISH 0xc2
define TKN_MICRODIGITAL 0xc3
; Token expansions
tkn_spectrum
defb "Spectrum", 0
tkn_rom
defb "ROM...", 0
tkn_spanish
defb "Spanish", 0
tkn_microdigital
defb "Microdigital", 0
define ATRB_TRANS 0xff
; define PROPORTIONAL_PRINT_SUPPORT
;
; Prints a string to the screen using ROM routines.
; Assumes the ROM is actually paged in...
; String to print in HL, zero terminated.
;
IFNDEF SAVEMEM
print_rom
; Open normal screen channel
ld a, 2
push hl
call 0x1601
pop hl
print_rom_loop
ld a, (hl)
cp 0
ret z
push hl
rst 0x10
pop hl
inc hl
jr print_rom_loop
ENDIF
;
; Prints a string to the screen.
; Inputs: HL=location of string to be printed
;
print
; Save all registers that are used by this routine
push hl
push de
push bc
push af
; Once we have the current char value, HL always points to the next value to be read.
print_nextchar
ld a, (hl)
inc hl
; Check for end of printable string, zero terminated
cp 0
jp z, print_done
; Check for newline (CR/LF)
cp CRLF
jr nz, print_chk_tok_spectrum
call newline
jr print_nextchar
print_chk_tok_spectrum
cp TKN_SPECTRUM
jr nz, print_chk_tok_rom
push hl
ld hl, tkn_spectrum
call print
pop hl
jr print_nextchar
print_chk_tok_rom
cp TKN_ROM
jr nz, print_chk_tok_spanish
push hl
ld hl, tkn_rom
call print
pop hl
jr print_nextchar
print_chk_tok_spanish
cp TKN_SPANISH
jr nz, print_chk_tok_microdigital
push hl
ld hl, tkn_spanish
call print
pop hl
jr print_nextchar
print_chk_tok_microdigital
cp TKN_MICRODIGITAL
jr nz, print_chk_ctrl_chr
push hl
ld hl, tkn_microdigital
call print
pop hl
jr print_nextchar
print_chk_ctrl_chr
; Check for Cursor Left control code
; From here, jump straight to character printing if obviously not
; a control character
cp 0x1f
jp nc, print_char
print_chk_left
cp LEFT
jr nz, print_chk_attr
ld a, (v_width)
ld b, a
ld a, (v_column)
sub b
ld (v_column), a
cp 0xff
jr nz, print_nextchar
ld a, 31
ld (v_column), a
ld a, (v_row)
dec a
ld (v_row), a
cp 0xff
jr nz, print_nextchar
ld a, 23
ld (v_row), a
jr print_nextchar
; Check for ATTR control code
print_chk_attr
cp ATTR
jp nz, print_chk_ink
ld a, (hl)
inc hl
ld (v_attr), a
jp print_nextchar
; Check for INK control code
print_chk_ink
cp INK
jr nz, print_chk_tab
ld a, (hl)
inc hl
and 7
ld d, a
ld a, (v_attr)
and 0xf8
or d
ld (v_attr), a
jp print_nextchar
; Check for TAB control code
print_chk_tab
cp TAB
jr nz, print_chk_paper
ld a, (hl)
inc hl
ld (v_column), a
jp print_nextchar
; Check for PAPER control code
print_chk_paper
cp PAPER
jr nz, print_chk_cr
ld a, (hl)
inc hl
and 7
rla
rla
rla
ld d, a
ld a, (v_attr)
and 0xc7
or d
ld (v_attr), a
jp print_nextchar
; Check for carriage return (CR)s control code
print_chk_cr
cp CR
jr nz, print_chk_bright
xor a
ld (v_column), a
jp print_nextchar
; Check for BRIGHT control code
print_chk_bright
cp BRIGHT
jr nz, print_chk_flash
ld a, (hl)
inc hl
cp 0
jr z, print_chk_bright_2
ld a, 64
print_chk_bright_2
ld d, a
ld a, (v_attr)
and 0xbf
or d
ld (v_attr), a
jp print_nextchar
; Check for FLASH control code
print_chk_flash
cp FLASH
jr nz, print_chk_at
ld a, (hl)
inc hl
cp 0
jr z, print_chk_flash_2
ld a, 128
print_chk_flash_2
ld d, a
ld a, (v_attr)
and 0x7f
or d
ld (v_attr), a
jp print_chk_bold
; Check for AT control code
print_chk_at
cp AT
jr nz, print_chk_bold
ld a, (hl)
inc hl
cp 24
jr c, print_chk_at_2
xor a
print_chk_at_2
ld (v_row), a
ld a, (hl)
inc hl
cp 249
jr c, print_chk_at_3
xor a
print_chk_at_3
ld (v_column), a
jp print_nextchar
; Check for BOLD control code
print_chk_bold
cp TEXTBOLD
jr nz, print_chk_norm
ld a, (v_pr_ops)
set 0, a
ld (v_pr_ops), a
jp print_nextchar
; Check for NORM (restores normal text) control code
print_chk_norm
cp TEXTNORM
jr nz, print_chk_inverse
ld a, (v_pr_ops)
res 0, a
ld (v_pr_ops), a
jp print_nextchar
print_chk_inverse
cp INVERSE
jr nz, print_chk_width
ld a, (hl)
inc hl
cp 0
jr z, print_chk_inverse_on
ld a, (v_pr_ops)
set 1, a
ld (v_pr_ops), a
jp print_nextchar
print_chk_inverse_on
ld a, (v_pr_ops)
res 1, a
ld (v_pr_ops), a
jp print_nextchar
print_chk_width
cp WIDTH
jr nz, print_char
ld a, (hl)
inc hl
ld (v_width), a
jp print_nextchar
; Print a single character to screen
print_char
ld b, a
call putchar
; Update the print position, wrapping around
; to screen start if necessary
ld a, (v_width)
IFDEF PROPORTIONAL_PRINT_SUPPORT
cp 0
jr z, do_proportional
ENDIF
ld b, a
ld a, (v_column)
add b
IFDEF PROPORTIONAL_PRINT_SUPPORT
jr print_wrap
do_proportional
push hl
ld hl, proportional_data
ld e, b
ld d, 0
add hl, de
ld e, 32
add hl, de
ld b, (hl)
pop hl
ENDIF
print_wrap
ld (v_column), a
cp 0
jp nz, print_nextchar
xor a
ld (v_column), a
ld a, (v_row)
inc a
ld (v_row), a
ld a, (v_scroll)
cp 0xff
jr nz, print_scroll
; Wrap text from bottom to top
cp 24
jp nz, print_nextchar
xor a
ld (v_row), a
jp print_nextchar
print_scroll
ld a, (v_scroll)
ld b, a
ld a, (v_scrl_lines)
add a, b
ld b, a
ld a, (v_row)
cp b
jp nz, print_nextchar
dec a
ld (v_row), a
call prt_scroll
jp print_nextchar
; Return without printing the rest if we overflowed the bottom
; of the screen.
print_done
; Done, restore registers before returning
pop af
pop bc
pop de
pop hl
ret
;
; Puts a single character on screen.
; Inputs: A=character to print, HL=y,x coordinates to print at.
; This routine drops directly into the putchar routine.
;
putchar_at
push af
ld a, h
ld (v_row), a
ld a, l
ld (v_column), a
pop af
;
; Puts a single character on screen at the location in the
; v_col and v_row variables, with v_attr colours.
; Inputs: A=character to print.
;
putchar
push hl
push bc
push de
push ix
; Find the address of the character in the bitmap table
sub 32 ; space = offset 0
ld hl, 0
ld l, a
; Multiply by 8 to get the byte offset
add hl, hl
add hl, hl
add hl, hl
; Add the offset
ld bc, charset
add hl, bc
; Store result in de for later use
ex de, hl
; Now find the address in the frame buffer to be written.
ld a, (v_row)
and 0x18
or 0x40
ld h, a
ld a, (v_row)
and 0x7
rla
rla
rla
rla
rla
ld l, a
ld a, (v_column)
and 0x7
ld ixl, a
ld a, (v_column)
srl a
srl a
srl a
add a, l
ld l, a
; DE contains the address of the char bitmap
; HL contains address in the frame buffer
; Calculate mask for printing partial characters
push hl
push de
; Offset goes in IXL for the duration
ld a, ixl
ld hl, mask_bits
ld e, a
xor a
ld d, a
add hl, de
ld a, (hl)
; Mask value goes in IXH
ld ixh, a
pop de
pop hl
ld b, 8
.putchar.loop
; Move character bitmap into the frame buffer
ld a, (de)
push de
; Store bitmap row in d, and mask in e for the duration
ld d, a
ld e, ixh
; Do we need to print the character in bold?
ld a, (v_pr_ops)
bit 0, a
jr z, .putchar.afterbold
; Bold character, grab byte, rotate it right then
; OR it with the original value
ld a, d
ld c, a
rl c
ld a, d
or c
ld d, a
.putchar.afterbold
ld a, (v_pr_ops)
bit 1, a
jr z, .putchar.afterinverse
ld a, d
xor 0xff
ld d, a
.putchar.afterinverse
push bc
; Apply mask to first byte
ld a, e
ld b, (hl)
and b
ld (hl), a
ld a, ixl
cp 0
jr z, .putchar.norot
ld b, a
ld a, d
.putchar.rot1
srl a
djnz .putchar.rot1
jr .putchar.byte1
.putchar.norot
ld a, d
.putchar.byte1
ld b, (hl)
or b
ld (hl), a
pop bc
; Check if we need to do second byte
ld a, ixl
cp 0
jr z, .putchar.nextbmpline
inc hl
push bc
; Apply mask to second byte
ld a, e
cpl
ld b, (hl)
and b
ld (hl), a
ld a, ixl
ld b, a
ld a, 8
sub b
ld b, a
ld a, d
.putchar.rot2
sla a
djnz .putchar.rot2
ld b, (hl)
or b
ld (hl), a
pop bc
dec hl
.putchar.nextbmpline
pop de
inc de ; next line of bitmap
inc h ; next line of frame buffer
.putchar.next
djnz .putchar.loop
.putchar.attr
; Now calculate attribute address
ld a, (v_attr)
cp ATRB_TRANS
jr z, .putchar.end
ld a, (v_row)
srl a
srl a
srl a
and 3
or 0x58
ld h, a
ld a, (v_row)
sla a
sla a
sla a
sla a
sla a
ld l, a
ld a, (v_column)
srl a
srl a
srl a
add a, l
ld l, a
; Write the current colour values in the v_attr
; sysvar to the just printed character
ld a, (v_attr)
ld (hl), a
ld a, ixl
cp 0
jr z, .putchar.end
; Do adjacent character if it straddles two characters
ld a, (v_attr)
inc hl
ld (hl), a
; Done, restore registers and return
.putchar.end
pop ix
pop de
pop bc
pop hl
ret
;
; Sets the current print position.
; Inputs: HL=desired print position y,x.
;
set_print_pos
ld a, h
ld (v_row), a
ld a, l
ld (v_column), a
ret
;
; Prints a 16-bit hex number to the buffer pointed to by DE.
; Inputs: HL=number to print.
;
Num2Hex
ld a,h
call Num1
ld a,h
call Num2
; Call here for a single byte conversion to hex
Byte2Hex
ld a,l
call Num1
ld a,l
jr Num2
Num1
rra
rra
rra
rra
Num2
or 0xF0
daa
add a,#A0
adc a,#40
ld (de),a
inc de
ret
;
; Prints a 16-bit decimal number to the buffer pointed to by DE.
; Inputs: HL=number to print.
;
Num2Dec
ld bc, -10000
call Num1D
ld bc, -1000
call Num1D
ld bc, -100
call Num1D
ld c, -10
call Num1D
ld c, b
Num1D
ld a, '0'-1
Num2D
inc a
add hl,bc
jr c, Num2D
sbc hl,bc
ld (de),a
inc de
ret
;
; Checks to see if printing a string will overwrite the end of the line;
; if so, it will advance the print position to the start of the next line.
; Inputs: A=length of string to be printed in pixels.
;
check_end_of_line
push bc
ld b, a
ld a, (v_column)
add b
pop bc
ret nc
xor a
ld (v_column), a
ld a, (v_row)
inc a
ld (v_row), a
cp 24
ret nz
xor a
ld (v_row), a
ret
;
; Clear the screen.
;
cls
push hl
push de
push bc
push af
; Clear the bitmap locations
xor a
ld hl, 0x4000
ld (hl), a
ld de, 0x4001
ld bc, 0x1800
ldir
; Clear the attribute area. Use the attribute
; value in v_attr for this.
ld a, (v_attr)
ld (hl), a
ld bc, 0x2ff
ldir
pop af
pop bc
pop de
pop hl
ret
;
; Moves print position to a new line.
;
newline
push af
push bc
xor a
ld (v_column), a
ld a, (v_row)
inc a
ld (v_row), a
ld a, (v_scroll)
cp 0xff
jr nz, nl_scroll
; Wrap text from bottom to top
cp 24
jr nz, newline_done
xor a
ld (v_row), a
jr newline_done
nl_scroll
ld a, (v_scroll)
ld b, a
ld a, (v_scrl_lines)
add a, b
ld b, a
ld a, (v_row)
cp b
jr c, newline_done
dec a
ld (v_row), a
call prt_scroll
newline_done
pop bc
pop af
ret
;
; Prints header with Spectrum stripe at top of screen.
; Text to be printed in HL.
;
print_header
push hl
ld a, 0x47
ld hl, 0x5800
ld de, 0x5801
ld bc, 0x1f
ld (hl), a
ldir
pop hl
ld a, (v_attr)
push af
ld a, 0x47
ld (v_attr), a
xor a
ld (v_row), a
ld a, (v_width)
ld (v_column), a
call print
pop af
ld (v_attr), a
ld hl, stripe_attr
ld de, 0x581a
ld bc, 6
ldir
ld a, 8
ld b, a
ld a, 1
ld hl, 0x401a
.stripeloop
ld (hl), a
inc hl
ld (hl), a
inc hl
ld (hl), a
inc hl
ld (hl), a
inc hl
ld (hl), a
dec hl
dec hl
dec hl
dec hl
inc h
rla
set 0, a