-
Notifications
You must be signed in to change notification settings - Fork 9
/
vt100.spin
1661 lines (1394 loc) · 53.9 KB
/
vt100.spin
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
{{
ANSI / VT-100 Terminal Emulator
Copyright (c) 2017-20 Marco Maccaferri and others
TERMS OF USE: MIT License
}}
CON
_XINFREQ = 5_000_000
_CLKMODE = XTAL1 + PLL16X
scrn_columns = 80 ' screen columns
scrn_rows = 25 ' screen rows
scrn_bcnt = scrn_columns * scrn_rows
vgrp = 2 ' video pin group
mode = 0 ' 0: FG on/off, 1: FG :==: BG
video = (vgrp << 9 | mode << 8 | %%333_0) << 21
CURSOR_ON = vga#CURSOR_ON
CURSOR_OFF = vga#CURSOR_OFF
CURSOR_ULINE = vga#CURSOR_ULINE
CURSOR_BLOCK = vga#CURSOR_BLOCK
CURSOR_FLASH = vga#CURSOR_FLASH
CURSOR_SOLID = vga#CURSOR_SOLID
CURSOR_MASK = vga#CURSOR_MASK
#0, CM, CX, CY
' USB HID
REQUEST_OUT = 0
REQUEST_CLASS = $20
REQUEST_TO_INTERFACE = 1
REQ_SET_REPORT = REQUEST_OUT | REQUEST_CLASS | REQUEST_TO_INTERFACE | $0900
REPORT_TYPE_OUTPUT = $0200
LED_NUM_LOCK = $01
LED_CAPS_LOCK = $02
LED_SCROLL_LOCK = $04
BELL_PINA = 14
BELL_PINB = 15
BELL_FREQ = 800
BELL_MS = 200
EEPROM_CONFIG = $7FE0
VAR
long scrn[scrn_bcnt / 2] ' screen buffer
long scrn1[scrn_bcnt / 2] ' settings screen buffer
long link[vga#res_m] ' mailbox
long cursor ' text cursor
long cursor_save
byte usb_buf[64]
byte usb_report[8]
byte usb_led
word kb_delay
word kb_repeat
long kb_last
long kb_timer
long kb_mod
long kb_str_table
long kb_map
long kb_settings
long kb_nrcs_table
byte ee_config[32] ' 0-1 ID = "P", "X"
' 2 = keyboard map (0-5)
' 3 = led status
' 4 = cursor style
' 5 = cursor keys (vt-100, app, ws)
' 6 = app. cursor keys (vt-100, app, ws)
OBJ
hc : "usb-fs-host"
ser : "com.serial"
vga : "waitvid.80x25.nine.driver"
kb : "keyboard"
i2c : "i2c"
PUB start | retval, ifd, epd
ser.StartRxTx(8, 9, 0, 115200)
' user configuration
i2c.init(29, 28, false)
i2c.eeprom_read(EEPROM_CONFIG, @ee_config, 32)
if ee_config[0] <> "P" or ee_config[1] <> "X"
bytefill(@ee_config, $00, 32)
ee_config[0] := "P"
ee_config[1] := "X"
ee_config[4] := constant(CURSOR_ULINE | CURSOR_FLASH)
ee_config[6] := 1
ee_config[8] := %00_00100 ' 250 ms / 20 cps
'i2c.eeprom_write(EEPROM_CONFIG, @ee_config, 32)
' initialize vga
wordfill(@scrn, $20_70, scrn_bcnt)
cursor.byte[CX] := 0
cursor.byte[CY] := 0
cursor.byte{CM} := (cursor.byte{CM} & constant(!CURSOR_MASK)) | CURSOR_ON | ee_config[4]
link{0} := video | @scrn{0}
link[2] := @cursor
vga.init(-1, @link{0})
' keyboard maps
kb_map := kb.get_map(ee_config[2])
case ee_config[5]
0:
kb_str_table_1 := @strTable
1:
kb_str_table_1 := @strTableApp
2:
kb_str_table_1 := @strTableWS
case ee_config[6]
0:
kb_str_table_2 := @strTable
1:
kb_str_table_2 := @strTableApp
2:
kb_str_table_2 := @strTableWS
kb_str_table := kb_str_table_1
kb_str_table_ptr := @kb_str_table
kb_nrcs_table_1 := @nrcs
kb_nrcs_table_2 := get_nrcs_map(ee_config[2])
kb_nrcs_table := kb_nrcs_table_1
kb_nrcs_table_ptr := @kb_nrcs_table
' settings screen setup
wordfill(@scrn1, $20_70, scrn_bcnt)
wordfill(@scrn1 + constant((scrn_bcnt - (2 * scrn_columns + 0)) * 2), $C4_F0, 80)
wordfill(@scrn1 + constant((scrn_bcnt - (24 * scrn_columns + 0)) * 2), $C4_F0, 80)
printAt(0, 31, $F0, string("TERMINAL SETTINGS"))
printAt(5, 22, $70, string("1 - Keyboard Mapping:"))
printAt(7, 22, $70, string("2 - Cursor Keys:"))
printAt(9, 22, $70, string("3 - Application Cursor Keys:"))
printAt(11, 22, $70, string("4 - Cursor Style:"))
printAt(13, 22, $70, string("5 - Num. Lock:"))
printAt(15, 22, $70, string("6 - Caps Lock:"))
printAt(17, 22, $70, string("7 - Key Repeat Delay:"))
printAt(19, 22, $70, string("8 - Key Repeat Rate:"))
updateSettings
printAt(24, 55, $F0, string("CTRL-F10 - Save and Exit"))
kb_settings := 0
' initialize terminal emulation
retval := ser.GetMailbox
rx_head := retval
rx_tail := retval + 4
rx_buffer := LONG[retval][8]
tx_head := retval + 8
tx_tail := retval + 12
tx_buffer := rx_buffer + ser#BUFFER_LENGTH
txt_cursor := @cursor
txt_scrn := @scrn + constant(scrn_bcnt << 1)
esc_overlay_par := OverlayParams(@_esc, @_esc_end)
attr_overlay_par := OverlayParams(@_attr, @_attr_end)
vt_overlay_par := OverlayParams(@_vt, @_vt_end)
cognew(@vt100_entry, 0)
' USB loop
kb_delay := word[@repeatDelay][(ee_config[8] & %11_00000) >> 5]
kb_repeat := word[@repeatPeriod][ee_config[8] & %00_11111]
repeat
if \hc.Enumerate < 0
waitcnt(CNT + CLKFREQ)
next
if \hc.Configure < 0
repeat
waitcnt(CNT + CLKFREQ)
while hc.GetPortConnection <> hc#PORTC_NO_DEVICE
next
if not (ifd := hc.FindInterface(3))
repeat
waitcnt(CNT + CLKFREQ)
while hc.GetPortConnection <> hc#PORTC_NO_DEVICE
next
' First endpoint on the first HID interface
epd := hc.NextEndpoint(ifd)
' Blink LEDs
usb_led := LED_NUM_LOCK|LED_CAPS_LOCK|LED_SCROLL_LOCK
hc.ControlWrite(REQ_SET_REPORT, REPORT_TYPE_OUTPUT, 0, @usb_led, 1)
waitcnt(CNT + CLKFREQ / 2)
usb_led := ee_config[3]
hc.ControlWrite(REQ_SET_REPORT, REPORT_TYPE_OUTPUT, 0, @usb_led, 1)
kb_last := 0
kb_timer := 0
repeat while hc.GetPortConnection <> hc#PORTC_NO_DEVICE
retval := \hc.InterruptRead(epd, @usb_buf, 64)
if retval == hc#E_TIMEOUT
' No data available. Try again later.
elseifnot retval < 0
' Successful transfer
decode(@usb_buf)
if kb_last <> 0 and kb_timer <> 0
if (kb_timer - CNT) =< 0
keyPressed(kb_last, kb_mod)
kb_timer := CNT + (CLKFREQ / 1000 * kb_repeat)
waitcnt(CNT + CLKFREQ)
PRI decode(buffer) | i, k, mod
usb_report[0] := BYTE[buffer][0]
usb_report[1] := BYTE[buffer][1]
if (usb_report[0] & %00100010) <> 0 ' SHIFT
if (usb_report[0] & %01000000) <> 0 ' SHIFT+ALT GR ?
mod := 3
else
mod := 1
elseif (usb_report[0] & %01000000) <> 0 ' ALT GR
mod := 2
else
mod := 0
repeat i from 2 to 7
k := BYTE[buffer][i]
if k <> 0 and lookdown(k : usb_report[2], usb_report[3], usb_report[4], usb_report[5], usb_report[6], usb_report[7]) == 0
keyPressed(k, mod)
if k <> kb_last
kb_last := k
kb_mod := mod
kb_timer := CNT + (CLKFREQ / 1000 * kb_delay)
usb_report[i] := k
if kb_last <> 0 and lookdown(kb_last : BYTE[buffer][2], BYTE[buffer][3], BYTE[buffer][4], BYTE[buffer][5], BYTE[buffer][6], BYTE[buffer][7]) == 0
kb_last := 0
PRI keyPressed(k, mod) | c, i, ptr
if (usb_report[0] & %00010001) and k == $43 ' CTRL-F10
if kb_settings == 0
cursor_save := cursor
cursor.byte[CX] := 0
cursor.byte[CY] := 0
cursor.byte{CM} := (cursor.byte{CM} & constant(!CURSOR_MASK)) | CURSOR_OFF
link{0} := @scrn1{0}
kb_settings := 1
else
i2c.eeprom_write(EEPROM_CONFIG, @ee_config, 32)
cursor := cursor_save
cursor.byte := (cursor.byte & constant(!CURSOR_MASK)) | CURSOR_ON | ee_config[4]
link{0} := @scrn{0}
kb_map := kb.get_map(ee_config[2])
case ee_config[5]
0:
kb_str_table_1 := @strTable
1:
kb_str_table_1 := @strTableApp
2:
kb_str_table_1 := @strTableWS
case ee_config[6]
0:
kb_str_table_2 := @strTable
1:
kb_str_table_2 := @strTableApp
2:
kb_str_table_2 := @strTableWS
kb_str_table := kb_str_table_1
kb_nrcs_table_2 := get_nrcs_map(ee_config[2])
if kb_nrcs_table <> kb_nrcs_table_1
kb_nrcs_table := kb_nrcs_table_2
kb_delay := word[@repeatDelay][(ee_config[8] & %11_00000) >> 5]
kb_repeat := word[@repeatPeriod][ee_config[8] & %00_11111]
kb_settings := 0
return
if (usb_report[0] & %00010001) and k == $42 ' CTRL-F9
if kb_nrcs_table == kb_nrcs_table_1
kb_nrcs_table := kb_nrcs_table_2
else
kb_nrcs_table := kb_nrcs_table_1
return
if (usb_led & LED_NUM_LOCK) and k => $59 and k =< $63
c := WORD[kb_map][k * 4 + 1]
else
c := WORD[kb_map][k * 4 + mod]
if kb_settings == 1
case c
"1":
ee_config[2]++
if ee_config[2] => 6
ee_config[2] := 0
updateSettings
"2":
ee_config[5]++
if ee_config[5] => 3
ee_config[5] := 0
updateSettings
"3":
ee_config[6]++
if ee_config[6] => 3
ee_config[6] := 0
updateSettings
"4":
if ee_config[4] == constant(CURSOR_ULINE | CURSOR_FLASH)
ee_config[4] := constant(CURSOR_BLOCK | CURSOR_FLASH)
elseif ee_config[4] == constant(CURSOR_BLOCK | CURSOR_FLASH)
ee_config[4] := constant(CURSOR_ULINE | CURSOR_SOLID)
elseif ee_config[4] == constant(CURSOR_ULINE | CURSOR_SOLID)
ee_config[4] := constant(CURSOR_BLOCK | CURSOR_SOLID)
else
ee_config[4] := constant(CURSOR_ULINE | CURSOR_FLASH)
updateSettings
"5":
ee_config[3] ^= LED_NUM_LOCK
updateSettings
"6":
ee_config[3] ^= LED_CAPS_LOCK
updateSettings
"7":
ee_config[8] += %01_00000
ee_config[8] &= %11_11111
updateSettings
"8":
i := (ee_config[8] + 1) & %00_11111
ee_config[8] := (ee_config[8] & %11_00000) | i
updateSettings
return
case c
"A".."Z":
if (usb_report[0] & %00010001) ' CTRL
ser.Char(c - "A" + 1)
elseif (usb_led & LED_CAPS_LOCK)
ser.Char(c ^ $20)
else
ser.Char(c)
"a".."z":
if (usb_report[0] & %00010001) ' CTRL
ser.Char(c - "a" + 1)
elseif (usb_led & LED_CAPS_LOCK)
ser.Char(c ^ $20)
else
ser.Char(c)
0..$FF:
repeat i from 0 to 11
if c == byte[kb_nrcs_table][i]
c := byte[@nrcs][i]
ser.Char(c)
kb#KeyNumLock:
usb_led ^= LED_NUM_LOCK
hc.ControlWrite(REQ_SET_REPORT, REPORT_TYPE_OUTPUT, 0, @usb_led, 1)
kb#KeyCapsLock:
usb_led ^= LED_CAPS_LOCK
hc.ControlWrite(REQ_SET_REPORT, REPORT_TYPE_OUTPUT, 0, @usb_led, 1)
kb#KeyScrollLock:
usb_led ^= LED_SCROLL_LOCK
hc.ControlWrite(REQ_SET_REPORT, REPORT_TYPE_OUTPUT, 0, @usb_led, 1)
kb#KeySpace..kb#KeyMaxCode:
ptr := @@word[kb_str_table][c - kb#KeySpace]
repeat strsize(ptr)
ser.Char(byte[ptr])
ptr++
PRI updateSettings | i
case ee_config[2]
0:
printAt(5, 44, $F0, string("US"))
1:
printAt(5, 44, $F0, string("IT"))
2:
printAt(5, 44, $F0, string("UK"))
3:
printAt(5, 44, $F0, string("FR"))
4:
printAt(5, 44, $F0, string("DE"))
5:
printAt(5, 44, $F0, string("NO"))
case ee_config[5]
0:
printAt(7, 39, $F0, string("VT-100 "))
1:
printAt(7, 39, $F0, string("VT-100 APPL."))
2:
printAt(7, 39, $F0, string("WordStar "))
case ee_config[6]
0:
printAt(9, 51, $F0, string("VT-100 "))
1:
printAt(9, 51, $F0, string("VT-100 APPL."))
2:
printAt(9, 51, $F0, string("WordStar "))
case ee_config[4]
CURSOR_ULINE:
printAt(11, 40, $F0, string("ULINE "))
CURSOR_BLOCK:
printAt(11, 40, $F0, string("BLOCK "))
CURSOR_ULINE | CURSOR_FLASH:
printAt(11, 40, $F0, string("BLINK ULINE"))
CURSOR_BLOCK | CURSOR_FLASH:
printAt(11, 40, $F0, string("BLINK BLOCK"))
if (ee_config[3] & LED_NUM_LOCK)
printAt(13, 37, $F0, string("ON "))
else
printAt(13, 37, $F0, string("OFF"))
if (ee_config[3] & LED_CAPS_LOCK)
printAt(15, 37, $F0, string("ON "))
else
printAt(15, 37, $F0, string("OFF"))
case ee_config[8] & %11_00000
%00_00000:
printAt(17, 44, $F0, string("250 ms"))
%01_00000:
printAt(17, 44, $F0, string("500 ms"))
%10_00000:
printAt(17, 44, $F0, string("750 ms"))
%11_00000:
printAt(17, 44, $F0, string("1 s "))
i := printDecAt(19, 43, $F0, 10000 / word[@repeatPeriod][ee_config[8] & %00_11111], 1)
printAt(19, i, $F0, string(" cps "))
PRI printAt(row, column, attr, stringptr) | xy
xy := scrn_bcnt - (row * scrn_columns + column)
repeat strsize(stringptr)
WORD[@scrn1][xy--] := (BYTE[stringptr++] << 8) | attr
PRI printDecAt(row, column, attr, value, decimals) | div, z_pad, xy
xy := scrn_bcnt - (row * scrn_columns + column)
div := 100_000 ' initialize divisor
z_pad~ ' clear zero-pad flag
repeat 6 - decimals
if (value => div) ' printable character?
WORD[@scrn1][xy--] := ((value / div + "0") << 8) | attr ' yes, print ASCII digit
column++
value //= div ' update value
z_pad~~ ' set zflag
elseif z_pad or (div == 1) ' printing or last column?
WORD[@scrn1][xy--] := constant("0" << 8) | attr
column++
div /= 10
if decimals > 0
WORD[@scrn1][xy--] := constant("." << 8) | attr
column++
repeat decimals
if (value => div) ' printable character?
WORD[@scrn1][xy--] := ((value / div + "0") << 8) | attr ' yes, print ASCII digit
value //= div ' update value
else
WORD[@scrn1][xy--] := constant("0" << 8) | attr
column++
div /= 10
return column
PRI get_nrcs_map(i)
case i
1: return @nrcs_map_it
2: return @nrcs_map_uk
3: return @nrcs_map_fr
4: return @nrcs_map_de
5: return @nrcs_map_no
return @nrcs
PRI OverlayParams (o_start, o_end) : params | len, hubend, cogend
' This code sets up the parameters for an overlay (in a format to increase overlay loading speed)
len := o_end - o_start
hubend := o_start + len - 1
cogend := ((@overlay_start - @vt100_entry + len) / 4) - 1
params := hubend << 16 + cogend
DAT
org 0
vt100_entry mov DIRA, bell_mask
mov scroll_top, txt_scrn
mov scroll_count, txt_bcnt
sub scroll_count, #scrn_columns
jmp #_bell
_loop call #charIn
cmp ch, #$07 wz ' bell
if_z jmp #_bell
cmp ch, #$08 wz ' backspace
if_z jmp #_bs
cmp ch, #$09 wz ' tab
if_z jmp #_tab
cmp ch, #$0A wz ' line feed
if_z jmp #_lf
cmp ch, #$0C wz ' form feed
if_z jmp #_ff
cmp ch, #$0D wz ' carriage return
if_z jmp #_cr
cmp ch, #$1B wz ' esc
if_z mov overlay_par, esc_overlay_par
if_z jmp #overlay_load
' NRCS
_print rdlong t2, kb_nrcs_table_ptr
mov t1, kb_nrcs_table_1
mov t3, #12
rdbyte a, t1
add t1, #1
cmp a, ch wz
rdbyte a, t2
add t2, #1
if_nz djnz t3, #$-5
if_z mov ch, a
' write ch to vga buffer
cmpsub x, #scrn_columns wc
if_nc jmp #:l1
cmp y, #scrn_rows-1 wc,wz
if_c add y, #1
if_nc call #scroll
:l1 mov t1, y ' t2 := y * 80
shl t1, #4
mov t2, t1
shl t2, #2
add t2, t1
add t2, x ' t2 := t1 + x
shl t2, #1
mov t1, txt_scrn
sub t1, t2
sub t1, #2
mov a, ch
shl a, #8
or a, txt_attr
wrword a, t1
add x, #1
_done mov t1, txt_cursor ' updates cursor position
add t1, #CX
mov a,x
cmp a, #scrn_columns wz,wc
if_nc mov a, #scrn_columns-1
wrbyte a, t1
add t1, #1
wrbyte y, t1
jmp #_loop
_bell mov FRQA, bell_frq
mov CTRA, bell_ctr
mov a, CNT
add a, bell_duration
waitcnt a, #0
mov CTRA, #0
jmp #_loop
_bs cmpsub x, #1
jmp #_done
_tab cmpsub x, #scrn_columns
andn x, #7
add x, #8
jmp #_done
_lf cmp y, #scrn_rows-1 wc,wz
if_c add y, #1
if_nc call #scroll
jmp #_done
_ff mov x, #0
mov y, #0
_cls mov t1, #$20
shl t1, #8
or t1, txt_attr
mov a, t1
shl a, #16
or a, t1
mov t1, txt_scrn
sub t1, #4
mov t3, txt_bcnt
shr t3, #1
:l1 wrlong a, t1
sub t1, #4
djnz t3, #:l1
jmp #_done
_cr mov x, #0
jmp #_done
' resident code
charIn rdlong t1, rx_head
rdlong t2, rx_tail
cmp t1, t2 wz
if_z jmp #charIn
mov t1, rx_buffer
add t1, t2
rdbyte ch, t1
add t2, #1
and t2, #ser#BUFFER_MASK
wrlong t2, rx_tail
charIn_ret ret
scroll mov t1, scroll_top
sub t1, #4
mov t2, t1
sub t2, #scrn_columns << 1
mov t3, scroll_count
shr t3, #1
:l1 rdlong a, t2
sub t2, #4
wrlong a, t1
sub t1, #4
djnz t3, #:l1
mov t2, #$20
shl t2, #8
or t2, txt_attr
mov a, t2
shl a, #16
or a, t2
mov t3, #scrn_columns >> 1
:l2 wrlong a, t1
sub t1, #4
djnz t3, #:l2
scroll_ret ret
scroll_top long 0
scroll_count long 0
' initialised data and/or presets
incdst long 1 << 9
rx_buffer long 0
rx_head long 0
rx_tail long 0
tx_buffer long 0
tx_head long 0
tx_tail long 0
txt_cursor long 0
txt_scrn long 0
txt_bcnt long scrn_bcnt
txt_attr long $70
txt_cursor_s long 0
bell_mask long (1 << BELL_PINA) | (1 << BELL_PINB)
bell_ctr long (%00100 << 26) | (BELL_PINB << 9) | BELL_PINA
bell_frq long trunc(53.6870912 * float(BELL_FREQ))
bell_duration long (80000000 / 1000) * BELL_MS
kb_nrcs_table_1 long 0
kb_nrcs_table_ptr long 0
x long 0
y long 0
esc_overlay_par long 0
attr_overlay_par long 0
vt_overlay_par long 0
' uninitialised data and/or temporaries
a long 0
ch long 0
ch_mod long 0
t1 long 0
t2 long 0
t3 long 0
argc long 0
args long 0[8]
' overlay loader
overlay_par long 0-0
_0x400 long $0000_0400
_djnz0 djnz overlay_par, #_cp2
overlay_load mov overlay_start, _djnz0
movd _cp2, overlay_par
sub overlay_par, #1
movd _cp1, overlay_par
shr overlay_par, #16
_cp2 rdlong 0-0, overlay_par
sub overlay_par, #7
sub _cp2, _0x400
_cp1 rdlong 0-0, overlay_par
sub _cp1, _0x400
overlay_start
fit $1F0
org overlay_start
_esc mov argc, #0
mov args, #0
mov args+1, #0
mov ch_mod, #0
call #charIn
cmp ch, #$1B wz ' esc (again, print it)
if_z jmp #_print
cmp ch, #"A" wz ' VT-52 compatibility
if_z jmp #_up
cmp ch, #"B" wz
if_z jmp #_down
cmp ch, #"C" wz
if_z jmp #_right
cmp ch, #"D" wz
if_z jmp #_left
cmp ch, #"H" wz
if_z jmp #_cup
cmp ch, #"J" wz
if_nz cmp ch, #"K" wz
if_nz cmp ch, #"7" wz
if_nz cmp ch, #"8" wz
if_z mov overlay_par, vt_overlay_par
if_z jmp #overlay_load
cmp ch, #"[" wz
if_nz jmp #_done
call #charIn
cmp ch, #"?" wz ' check private prefix after "["
if_nz jmp #:l2+1
mov ch_mod, ch
:l2 call #charIn
cmp ch, #"0" wc,wz
if_c jmp #:l1
cmp ch, #"9"+1 wc,wz
if_nc jmp #:l1
:s1 mov t1, args ' multiply x 10
shl t1, #1
mov t2, t1
shl t2, #2
add t2, t1
sub ch, #"0" ' adds digit
add t2, ch
:d1 mov args, t2
jmp #:l2
:l1 cmp ch, #";" wz
if_nz jmp #:l3
add argc, #1
add :d1, incdst
add :d2, incdst
add :s1, #1
:d2 mov args, #0
jmp #:l2
:l3 cmp ch, #"A" wz
if_z jmp #_up
cmp ch, #"B" wz
if_z jmp #_down
cmp ch, #"C" wz
if_z jmp #_right
cmp ch, #"D" wz
if_z jmp #_left
cmp ch, #"H" wz
if_z jmp #_cup
cmp ch, #"f" wz
if_z jmp #_cup
cmp ch, #"r" wz
if_z jmp #_stbm
cmp ch, #"m" wz
if_z mov overlay_par, attr_overlay_par
if_z jmp #overlay_load
mov overlay_par, vt_overlay_par
jmp #overlay_load
_up cmp args, #0 wz
if_z add args, #1
sub y, args wc
if_c mov y, #0
jmp #_done
_down cmp args, #0 wz
if_z add args, #1
add y, args
cmp y, #scrn_rows wc
if_nc mov y, #scrn_rows-1
jmp #_done
_right cmp args, #0 wz
if_z add args, #1
add x, args
cmp x, #scrn_columns wc
if_nc mov x, #scrn_columns-1
jmp #_done
_left cmp args, #0 wz
if_z add args, #1
sub x, args wc
if_c mov x, #0
jmp #_done
_cup mov y, args
cmp y, #scrn_rows wc
if_nc mov y, #scrn_rows
cmpsub y, #1
mov x, args+1
cmp x, #scrn_columns wc
if_nc mov x, #scrn_columns
cmpsub x, #1
jmp #_done
_stbm cmp argc, #0 wz
if_z jmp #:stbm1
cmp args, #1 wc,wz
if_c jmp #_done
cmp args, #25-1 wc,wz
if_nc jmp #_done
cmp args + 1, #25+1 wc,wz
if_nc jmp #_done
cmp args + 1, args wc,wz
if_c_or_z jmp #_done
:stbm1 mov scroll_top, txt_scrn
mov scroll_count, txt_bcnt
sub scroll_count, #scrn_columns
cmp argc, #0 wz
if_z jmp #_done
mov y, args
sub y, #1 wc,wz
if_nz sub scroll_top, #scrn_columns << 1
if_nz sub scroll_count, #scrn_columns
if_nz djnz y, #$-2
mov y, #25
sub y, args + 1 wc,wz
if_nz sub scroll_count, #scrn_columns
if_nz djnz y, #$-1
jmp #_done
long $0[($ - overlay_start) // 2]
_esc_end fit $1F0
org overlay_start
_attr mov a, args
cmp a, #0 wz ' reset attr
if_z jmp #:reset
cmp a, #1 wz ' bright
if_z jmp #:bright
cmp a, #5 wz ' blink
if_z jmp #:blink
cmp a, #7 wz ' reverse
if_z jmp #:reverse
cmp a, #30 wc ' foreground
if_c jmp #:l2
cmp a, #38 wc,wz
if_c jmp #:fg
if_z jmp #:ext_fg
:l2 cmp a, #39 wz ' reset foreground
if_z jmp #:res_fg
cmp a, #40 wc ' background
if_c jmp #:l3
cmp a, #48 wc,wz
if_c jmp #:bg
if_z jmp #:ext_bg
:l3 cmp a, #49 wz ' reset background
if_z jmp #:res_bg
:l1 add _attr, #1
sub argc, #1 wc
if_nc jmp #_attr
jmp #_done
:reset mov txt_attr, #$70
jmp #:l1
:bright or txt_attr, #$80
jmp #:l1
:blink or txt_attr, #$01
jmp #:l1
:reverse mov t1, txt_attr
and t1, #$0E
shl t1, #3
and txt_attr, #$70
shr txt_attr, #3
or txt_attr, t1
jmp #:l1
:fg sub a, #30
shl a, #4
and txt_attr, #$8F
or txt_attr, a
jmp #:l1
:ext_fg add _attr, #1
movs :xs1, _attr
add _attr, #1
movs :xs2, _attr
sub argc, #2
:xs1 mov a, 0-0
cmp a, #2 wz ' 38;2;r;g;b not supported, skip
if_z jmp #:xs3
cmp a, #5 wz ' 38;5;n supported for n <= 15
if_nz jmp #:l1
:xs2 mov a, 0-0
cmp a, #16 wc
if_nc jmp #:l1
shl a, #4
and txt_attr, #$0F
or txt_attr, a
jmp #:l1
:xs3 add _attr, #2
sub argc, #2
jmp #:l1
:res_fg and txt_attr, #$0F
or txt_attr, #$70
jmp #:l1
:bg sub a, #40
shl a, #1
and txt_attr, #$F1
or txt_attr, a
jmp #:l1
:ext_bg add _attr, #1
movs :xs4, _attr
add _attr, #1
movs :xs5, _attr
sub argc, #2