-
Notifications
You must be signed in to change notification settings - Fork 1
/
kernel.dasm16
1310 lines (1171 loc) · 32.2 KB
/
kernel.dasm16
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
; Copyright (c) 2012 Florian Hars
;
; memset and several output methods from https://github.com/jdiez17/0x42c
; Copyright (c) 2012 José Manuel Díez
;
; Permission is hereby granted, free of charge, to any person obtaining
; a copy of this software and associated documentation files (the
; "Software"), to deal in the Software without restriction, including
; without limitation the rights to use, copy, modify, merge, publish,
; distribute, sublicense, and/or sell copies of the Software, and to
; permit persons to whom the Software is furnished to do so, subject to
; the following conditions:
;
; The above copyright notice and this permission notice shall be
; included in all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SET A, 0
:_pc_initloop
SET [_pc_status + A], 0
ADD A, 1
IFG 16, A
SET PC, _pc_initloop
SET [_pc_curr_proc], 15
SET A, 0
SET C, 1
JSR _pc_set_cell_hilit
;start the first process
SET A, init
SET B, 0
JSR fork
JSR yield
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; SCHEDULER CORE
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; fork starts a new process (up to a limit of 16 processes total)
;
; input A: the start address of the new process
; input B: the run mode
; 0: daemon mode, the new process is independent from
; the calling process
; 1: child mode, the calling process can query the
; return value of the child one it has exited
; 2: sync mode, the parent waits for the child to
; terminate
; return A:
; 0 on a successful daemon fork
; child PID on a successful child fork
; return value after a sync child has finished
; -1 if no free slot remains
; return B:
; 0 on a successful fork
; -1 if no free slot remains (to distingush an error
; from a sync return of -1)
:fork
SET [_pc_tmp_PC], A
SET [_pc_tmp_B], B
SET [_pc_tmp_SP], SP
SET SP, 0xF000
SET C, [_pc_curr_proc]
SET B, C
:_pc_next_free
ADD B, 1
AND B, 0xF
IFE B, C
SET PC, _pc_fork_error
SET A, [_pc_status + B]
AND A, 0x8000
IFE A, 0x8000
SET PC, _pc_next_free
; mark as dumb terminal
SET [_pc_vidbufs + B], 0
SET [_pc_vidptrs + B], 0x8040
; now B contains the number of the next free slot
; store the PPID in _pc_tmp_C
SET [_pc_tmp_C], C
; mark the process slot as running and initialize
; the register file
SET [_pc_status + B], 0xC000
SET A, B
MUL A, 10
; next instruction is the value passed in
SET [_pc_save_PC + A], [_pc_tmp_PC]
; SP points one over the top of the allocated page
SET C, B
ADD C, 1
MUL C, 0x100
ADD C, 0xEFFF
SET [C], exit
SET [_pc_save_SP + A], C
; registers XYZIJ are shared with the caller
SET [_pc_save_A + A], 0
SET [_pc_save_B + A], 0
SET [_pc_save_C + A], 0
JSR _pc_save_xyzij
; clear the status cell
SET PUSH, B
SET A, B
SET B, 0
JSR _pc_i2hex_cell
SET A, PEEK
SET C, 0x07
JSR _pc_set_cell_color
SET B, POP
; finally chek the run mode
SET A, [_pc_tmp_B]
SET SP, [_pc_tmp_SP]
; mode is daemon, do not remember PPID, A is 0
IFN A, 0
ADD PC, 2
SET B, 0
SET PC, POP
set C, [_pc_tmp_C]
; remember the PPID
BOR [_pc_status + B], C
BOR [_pc_status + B], 0x10
; if the call is asynchronous, set A to the PID
; and return
IFN A, 1
ADD PC, 3
SET A, B
SET B, 0
SET PC, POP
; else, set the sync bit and put the caller to sleep
SET A, C
:_pc_sleep_waiting
BOR [_pc_status + B], 0x20
AND [_pc_status + A], 0xBFFF
BOR [_pc_status + A], 0x1000
SET PC, yield
:_pc_fork_error
SET A, 0xFFFF
SET B, 0xFFFF
SET PC, POP
; wait for the result of a child process
;
; input A: the PID of the process to wait for
; input B: wait mode, 0: blocking, 1: non-blocking
; output A / B:
; -1 / -1 : PID is not a child of this process
; 0 / -1 : child has not finished (non-blocking)
; res / 0 : child has finished
:wait
IFB A, 0xFFF0
SET PC, _pc_fork_error
SET [_pc_tmp_A], A
SET [_pc_tmp_B], B
SET A, [_pc_status + A]
SET B, A
AND A, 0xF
IFN A, [_pc_curr_proc]
SET PC, _pc_fork_error
SET A, [_pc_tmp_A]
IFB B, 0x2000
SET PC, _pc_waitexited
IFE [_pc_tmp_B], 0
SET PC, _pc_waitblocked
; waiting non-blocking on a runnig process
SET A, 0
SET B, 0xFFFF
SET PC, POP
; if we want a blocking wait on a runnig process,
; treat is as if it had been a sync fork
:_pc_waitblocked
SET B, A
SET A, [_pc_curr_proc]
SET PC, _pc_sleep_waiting
:_pc_waitexited
SET [_pc_tmp_SP], SP
SET SP, 0xF000
JSR _pc_exit_nextscreen
SET [_pc_status + A], 0
SET PUSH, A
SET C, 0
JSR _pc_set_cell_color
SET A, POP
SET SP, [_pc_tmp_SP]
MUL A, 10
SET A, [_pc_save_A + A]
SET B, 0
SET PC, POP
; exit terminates a running process, optionally remembering
; the value of the A register for the parent process.
;
;
; The routine can safely ignore SP (alternatively, it
; doesn't matter if it is called with JSR exit or SET PC, exit),
; since it doesn't return and this processes stack is discarded.
;
; input A : the value to return to the parent proces
:exit
SET X, A
SET A, [_pc_curr_proc]
SET C, [_pc_status + A]
; A daemon process just terminates
IFB C, 0x10
SET PC, _pc_exit_child
SET [_pc_status + A], 0
SET C, 0x00
JSR _pc_set_cell_color
SET A, [_pc_curr_proc]
JSR _pc_exit_nextscreen
SET PC, _pc_next_proc
:_pc_exit_child
AND C, 0xF
IFB [_pc_status + C], 0x1000 ; parent sleeps waiting
SET PC, _pc_exit_sync
; mark the process a zombie and remember A
AND [_pc_status + A], 0xBFFF
BOR [_pc_status + A], 0x2000
MUL A, 10
SET [_pc_save_A + A], X
SET PC, _pc_next_proc
:_pc_exit_sync
; mark slot as free
SET [_pc_status + A], 0
JSR _pc_exit_nextscreen
; mark parent as running
AND [_pc_status + C], 0xEFFF
BOR [_pc_status + C], 0x4000
; set return value for parent
MUL C, 10
SET [_pc_save_A + C], X
SET [_pc_save_B + C], 0
SET C, 0x00
JSR _pc_set_cell_color
SET A, [_pc_curr_proc]
SET PC, _pc_next_proc
; if the process that currently holds the terminal is removed
; from the process list, switch to the next active screen
:_pc_exit_nextscreen
IFN A, [_pc_screen_proc]
SET PC, POP
SET PUSH, A
SET PUSH, C
SET PUSH, X
SET PUSH, Y
JSR _pc_next_screen
SET Y, POP
SET X, POP
SET C, POP
SET A, POP
SET PC, POP
:yield
SET [_pc_tmp_A], A
SET A, [_pc_curr_proc]
SET [_pc_vidptrs + A], [_pc_vidmem]
; save register values of the yielding process
MUL A, 10
SET [_pc_save_A + A], [_pc_tmp_A]
SET [_pc_save_B + A], B
SET [_pc_save_C + A], C
SET [_pc_save_PC + A], POP
SET [_pc_save_SP + A], SP
SET SP, 0xF000
JSR _pc_save_xyzij
JSR _pc_kbd_handler
; DEBUG CODE
; slow down to see something in cycleexact emulation
; SET X, 0
; ADD X, 1
; IFN X, 0x3000
; SUB PC, 4
SET A, [_pc_curr_proc]
IFB [_pc_status + A], 0x40 ; process uses custom status code
ADD PC, 4
SET B, [_pc_tmp_A]
JSR _pc_i2hex_cell
; seach for the next running process
SET A, [_pc_curr_proc]
:_pc_next_proc
ADD A, 1
AND A, 0xF
SET B, [_pc_status + A]
AND B, 0xC000
IFN B, 0xC000
SET PC, _pc_next_proc
:_pc_run_next_proc
SET [_pc_curr_proc], A
SET PUSH, A
SET [_pc_vidbase], 0
SET [_pc_vidmem], [_pc_vidptrs + A]
IFE [_pc_curr_proc], [_pc_screen_proc]
SET PC, _pc_vid_curr
IFE [_pc_vidbufs + A], 0
SET PC, _pc_vid_done
SET [_pc_vidbase], [_pc_vidbufs + A]
SET [_pc_vidend], [_pc_vidbase]
ADD [_pc_vidend], 320
SET PC, _pc_vid_done
:_pc_vid_curr
SET [_pc_vidbase], 0x8040
SET [_pc_vidend], 0x8180
:_pc_vid_done
SET A, POP
MUL A, 10
; restore register values of the next process
SET [_pc_tmp_A], [_pc_save_A + A]
SET B, [_pc_save_B + A]
SET C, [_pc_save_C + A]
SET X, [_pc_save_X + A]
SET Y, [_pc_save_Y + A]
SET Z, [_pc_save_Z + A]
SET I, [_pc_save_I + A]
SET J, [_pc_save_J + A]
SET [_pc_tmp_PC], [_pc_save_PC + A]
SET SP, [_pc_save_SP + A]
SET A, [_pc_tmp_A]
SET PC, [_pc_tmp_PC]
:_pc_save_xyzij
SET [_pc_save_X + A], X
SET [_pc_save_Y + A], Y
SET [_pc_save_Z + A], Z
SET [_pc_save_I + A], I
SET [_pc_save_J + A], J
set PC, POP
; mark the current process as interactive
;
; an interactive process will be suspended unless it has the
; terminal focus
; input A: if zero clear the interactive bit, otherwise set it
:set_interactive
SET B, [_pc_curr_proc]
IFE A, 0
SET PC, _pc_clear_interactive
BOR [_pc_status + B], 0x0080
IFE B, [_pc_screen_proc]
SET PC, POP
BOR [_pc_status + B], 0x0800
AND [_pc_status + B], 0xBFFF
SET PC, yield
:_pc_clear_interactive
AND [_pc_status + B], 0xFF7F
SET PC, POP
; persistend state of the scheduler
:_pc_curr_proc DAT 15
; temporary storage of the scheduler
:_pc_tmp_A DAT 0
:_pc_tmp_B DAT 0
:_pc_tmp_C DAT 0
:_pc_tmp_PC DAT 0
:_pc_tmp_SP DAT 0
; Persistent register files of the processes
; A status word for each process
;
; Bit
; 0 - 3 0x000F PID of the parent process if not a daemon process
; 4 0x0010 1 if this is a child process
; 5 0x0020 1 if this is a sync child
; 6 0x0040 1 if this process uses custom status update code
; 7 0x0080 1 if this is an interactive process
;
; 10 0x0400 1 if this process waits for keyboard input
; 11 0x0800 1 if this process waits for the terminal
; 12 0x1000 1 if this process is waiting for a child
; 13 0x2000 1 if this a a zombie process, 0 if waiting
; 14 0x4000 1 if this process is running, 0 if waiting
; 15 0x8000 1 if this slot is used, 0 if free
;
:_pc_status ; RESERVE 16
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
; A register file for each of the processes
:_pc_save_A DAT 0
:_pc_save_B DAT 0
:_pc_save_C DAT 0
:_pc_save_X DAT 0
:_pc_save_Y DAT 0
:_pc_save_Z DAT 0
:_pc_save_I DAT 0
:_pc_save_J DAT 0
:_pc_save_PC DAT 0
:_pc_save_SP DAT 0
; RESERVE 150
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
DAT 0,0,0,0,0,0,0,0,0,0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; MEMORY HANDLING
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Set C words starting at A to B
;
; Adapted from https://github.com/jdiez17/0x42c
; Credit for this function goes to Tobba/rmmh
;
; input A: target address
; input B: fill value
; input C: number of words to fill
:memset
SET PUSH, X
SET X, SP
SET SP, A
ADD A, C
AND C, 0x0f
XOR C, 0x0f
ADD C, 1
ADD PC, C
:_pc_memset_loop
SET POP, B
SET POP, B
SET POP, B
SET POP, B
SET POP, B
SET POP, B
SET POP, B
SET POP, B
SET POP, B
SET POP, B
SET POP, B
SET POP, B
SET POP, B
SET POP, B
SET POP, B
SET POP, B
IFN SP, A
SET PC, _pc_memset_loop
SET SP, X
SET X, POP
SET PC, POP
; copy C words starting at A from B
;
; If the memory areas overlap, the routine will only work
; if the target address is lower than the source address
;
; input A: target address
; input B: source address
; input C: number of words to copy
:memcpy
SET PUSH, X
SET X, SP
SET SP, A
ADD A, C
AND C, 0x0f
XOR C, 0x0f
ADD C, 1
SUB B, C
SHL C, 1
ADD PC, C
:_pc_memcpy_loop
SET POP, [0 + B]
SET POP, [1 + B]
SET POP, [2 + B]
SET POP, [3 + B]
SET POP, [4 + B]
SET POP, [5 + B]
SET POP, [6 + B]
SET POP, [7 + B]
SET POP, [8 + B]
SET POP, [9 + B]
SET POP, [10 + B]
SET POP, [11 + B]
SET POP, [12 + B]
SET POP, [13 + B]
SET POP, [14 + B]
SET POP, [15 + B]
ADD B, 16
IFN SP, A
SET PC, _pc_memcpy_loop
SET SP, X
SET X, POP
SET PC, POP
; copy C words starting at A from B
;
; If the memory areas overlap, the routine will only work
; if the target address is higher than the source address
;
; input A: target address
; input B: source address
; input C: number of words to copy
:memcpy_rev
SET PUSH, X
SET X, SP
ADD B, C
SET SP, A
ADD SP, C
AND C, 0x0f
SUB B, C
XOR C, 0x0f
ADD C, 1
SHL C, 1
ADD PC, C
:_pc_memcpy_r_loop
SET PUSH, [15 + B]
SET PUSH, [14 + B]
SET PUSH, [13 + B]
SET PUSH, [12 + B]
SET PUSH, [11 + B]
SET PUSH, [10 + B]
SET PUSH, [9 + B]
SET PUSH, [8 + B]
SET PUSH, [7 + B]
SET PUSH, [6 + B]
SET PUSH, [5 + B]
SET PUSH, [4 + B]
SET PUSH, [3 + B]
SET PUSH, [2 + B]
SET PUSH, [1 + B]
SET PUSH, [0 + B]
SUB B, 16
IFN SP, A
SET PC, _pc_memcpy_r_loop
SET SP, X
SET X, POP
SET PC, POP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; RANDOM NUMBERS
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Generate a pseudorandom number using a 32bit linear
; congruential generator
;
; Multiplication constants taken from P. L'Ecuyer, Tables... (1999),
; http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.34.1024
;
; return A: high 16 bit of next random value
; return B: low 16 bit of next random value
:random
SET A, 44118
MUL A, [_pc_rand_hi]
SET B, 19205
MUL B, [_pc_rand_lo]
ADD A, O
ADD B, 101
ADD A, O
SET [_pc_rand_lo], B
SET [_pc_rand_hi], A
SET PC, POP
; mix the values in A and B into the random number state
;
; this probably violates every rule of how to do this (like,
; don't design crypto primitives over lunch), but it does mix
; the bits up. Remember, it's a game.
:_pc_update_entropy
SET C, [_pc_rand_lo]
SHL C, 7
BOR C, O
XOR C, B
XOR C, A
SET A, [_pc_rand_hi]
SHL A, 10
BOR A, O
XOR A, C
XOR A, B
SET [_pc_rand_lo], C
SET [_pc_rand_hi], A
SET PC, POP
:_pc_rand_lo
DAT 13908
:_pc_rand_hi
DAT 692
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; KEYBOARD AND SCREEN HANDLING
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; uses the 320 byte buffer pointed to by A as video backing store
; while the process doesn't have focus
:reserve_screen
SET B, [_pc_curr_proc]
SET [_pc_vidbufs + B], A
SET [_pc_vidptrs + B], A
IFE B, [_pc_screen_proc]
SET PC, _pc_res_curr_screen
SET [_pc_vidbase], A
SET [_pc_vidmem], A
SET [_pc_vidend], A
ADD [_pc_vidend], 320
SET PC, POP
:_pc_res_curr_screen
SET PC, POP
; One task of the keyboard handler is to allow the user to
; switch the display to another process.
:_pc_kbd_handler
ADD [_pc_cycle_count], 1
JSR _pc_getch
IFE A, 0
SET PC, POP
SET PUSH, A
SET B, [_pc_cycle_count]
JSR _pc_update_entropy
SET A, POP
IFE A, 0x153 ; Pg_up
SET PC, _pc_next_screen
IFE A, 0x09 ; tab
SET PC, _pc_next_screen
IFE A, 0x152 ; Pg_dn
SET PC, _pc_prev_screen
; store the character in the buffer for the displayed
; process if it is empty (otherwise discard the key)
SET B, [_pc_screen_proc]
IFB [_pc_status + B], 0x0400
SET PC, _pc_kbd_resume
IFE [_pc_kbd_buf + B], 0
SET [_pc_kbd_buf + B], A
; DEBUG CODE
; show last pressed key in cell 15
SET B, A
SET A, 15
JSR _pc_i2hex_cell
SET A, 15
SET C, 0x07
JSR _pc_set_cell_color
SET PC, POP
:_pc_kbd_resume
BOR [_pc_status + B], 0x4000
AND [_pc_status + B], 0xFBFF
MUL B, 10
SET [_pc_save_A + B], A
SET PC, POP
; move the _pc_getch label to the correct function
:_pc_getch
; getch for a 16 byte ringbuffer
:_pc_getch_16
SET B, [0x9010]
SET A, [0x9000 + B]
IFE A, 0
SET PC, POP
SET [0x9000 + B], 0
ADD B, 1
AND B, 0xF
SET [0x9010], B
SET PC, POP
; getch for dcpustudio
:_pc_getch_dcpustud
SET A, [0x9000]
SET [0x9000], 0
SET PC, POP
:_pc_prev_screen
SET PC, POP
SET X, [_pc_screen_proc]
SET Y, X
SUB Y, 1
AND Y, 0xF
IFB 0x8000, [_pc_status + Y]
SET PC, _pc_switch_screen
SUB PC, 8
:_pc_next_screen
SET X, [_pc_screen_proc]
SET Y, X
ADD Y, 1
AND Y, 0xF
IFB 0x8000, [_pc_status + Y]
ADD PC, 1
SUB PC, 7
; switch to the next active screen
;
; currently does nothing but highlight the status display cell
; for the active process.
;
; input X: the currently displayed process
; input Y: the next process to display
:_pc_switch_screen
; if the old current process is interactive,
; mark it as suspended
IFB [_pc_status + X], 0x0080
SET PC, _pc_switch_suspend
SET PC, _pc_switch_1
:_pc_switch_suspend
BOR [_pc_status + X], 0x0800
AND [_pc_status + X], 0xBFFF
:_pc_switch_1
; if the new current process is waiting for the terminal,
; mark it as running
IFB [_pc_status + Y], 0x0080
SET PC, _pc_switch_resume
SET PC, _pc_switch_2
:_pc_switch_resume
AND [_pc_status + Y], 0xF7FF
BOR [_pc_status + Y], 0x4000
:_pc_switch_2
SET A, X
SET C, 0
JSR _pc_set_cell_hilit
SET A, Y
SET C, 1
JSR _pc_set_cell_hilit
SET [_pc_screen_proc], Y
IFN [_pc_vidbufs + X], 0
JSR _pc_savescreen
IFE [_pc_vidbufs + Y], 0
SET PC, _pc_vid_dumb
SET B, [_pc_vidbufs + Y]
SET A, [_pc_vidptrs + Y]
SUB A, B
ADD A, 0x8040
SET [_pc_vidptrs + Y], A
SET A, 0x8040
SET C, 320
SET PC, memcpy
:_pc_vid_dumb
SET [_pc_vidptrs + Y], 0x8040
SET [_pc_vidbase], 0x8040
SET [_pc_vidend], 0x8180
JSR clear_screen
SET PC, POP
:_pc_savescreen
SET A, [_pc_vidbufs + X]
SET B, [_pc_vidptrs + X]
SUB B, 0x8040
ADD B, A
SET [_pc_vidptrs + X], B
SET B, 0x8040
SET C, 320
SET PC, memcpy
; the PID of the process currently holding the screen
:_pc_screen_proc DAT 0
:_pc_cycle_count DAT 0
; one character keyboard buffer for each process
:_pc_kbd_buf ;RESERVE 0x10
DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
:_pc_vidbufs DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
:_pc_vidptrs DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
:_pc_vidbase DAT 0x8040
:_pc_vidmem DAT 0x8040
:_pc_vidend DAT 0x8180
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; INPUT AND OUTPUT FUNCTIONS
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; return the last key for this process, or 0 if there is none
:getch
SET B, [_pc_curr_proc]
SET A, [_pc_kbd_buf + B]
SET [_pc_kbd_buf + B], 0
SET PC, POP
; blocking read
:readch
SET B, [_pc_curr_proc]
SET A, [_pc_kbd_buf + B]
SET [_pc_kbd_buf + B], 0
IFN A, 0
SET PC, POP
BOR [_pc_status + B], 0x0400
AND [_pc_status + B], 0xBFFF
SET PC, yield
; read a line
;
; input A: the address of the read buffer
; input B: the length of the read buffer (max. 32 incl. trailing null)
:readline
SET C, 0
; edit a line
;
; input A: the address of the read buffer
; input B: the length of the read buffer (max. 32 incl. trailing null)
; input C: the number of characters already in the buffer
:editline
SET PUSH, X
SET PUSH, Y
SET PUSH, Z
SET PUSH, I
SET X, A ; start address
SET Y, B
IFG Y, 32
SET Y, 32
ADD Y, A ; end address
SET Z, C
ADD Z, X ; current insertion point
SET I, Z ; last valid char
SUB Z, 1
:_pc_readl_l
ADD Z, 1
IFG I, Z
SET PC, _pc_readl_inner
SET [I], 0
ADD I, 1
:_pc_readl_inner
SET C, [_pc_vidmem]
SET B, [Z]
AND [C], 0xFF00
IFE [C], 0
SET [C], 0xF000
IFE B, 0
SET B, 0x5F
BOR [C], B
BOR [C], 0x80
JSR readch
AND [C], 0xFF7F
IFE A, 0x0a
SET PC, _pc_readl_end
IFE A, 1
SET PC, _pc_readl_left
IFE A, 2
SET PC, _pc_readl_right
IFE A, 8
SET PC, _pc_readl_backspace
IFE Z, Y
SET PC, _pc_readl_inner
SET [Z], A
JSR printchar
SET PC, _pc_readl_l
:_pc_readl_end
ADD Z, 1
IFN Z, I
SET PC, _pc_readl_end_innner
SET A, [Y]
AND A, 0x7F
IFE A, 0
SET A, 0x20
JSR printchar
:_pc_readl_end_innner
SET I, POP
SET Z, POP
SET Y, POP
SET X, POP
SET PC, POP
:_pc_readl_left
IFN [Z], 0
SET PC, _pc_readl_left_i
SET A, 0x20 ; clean cursor if at end
JSR printchar
SUB [_pc_vidmem], 1
: _pc_readl_left_i
IFE Z, X
SET PC, _pc_readl_inner
SUB Z, 1
SUB [_pc_vidmem], 1
SET PC, _pc_readl_inner
:_pc_readl_right
ADD Z, 1
ADD [_pc_vidmem], 1
IFN Z, I
SET PC, _pc_readl_inner
SET PC, _pc_readl_left ; undo move if past last valid char
:_pc_readl_backspace
IFE Z, X
SET PC, _pc_readl_inner
SUB I, 1
SET A, I
SUB A, Z
ADD A, [_pc_vidmem]
AND [A], 0xFF00
BOR [A], 0x20
SUB Z, 1
SUB [_pc_vidmem], 1
SET A, Z
SET B, [_pc_vidmem]
:_pc_readl_back_l
SET [A], [1+A]
SET [B], [1+B]
ADD A, 1
ADD B, 1
IFN A, I
SET PC, _pc_readl_back_l
SET PC, _pc_readl_inner
:_pc_readline_ptr
DAT 0
:println ; shortcut
jsr print
jsr newline
set pc, pop
:print
IFE [_pc_vidbase], 0
SET PC, POP
set i, a
:_pc_doprint
ife [i], 0
set pc, pop
set a, [i]
jsr printchar
add i, 1
set pc, _pc_doprint