-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_pio_ks0108.asm
732 lines (653 loc) · 18.5 KB
/
arduino_pio_ks0108.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
; A2 A1 A0 x x x A/B C/D
pio_a_data equ $00 ; 0 0 0 0 0 0 0 0
pio_a_control equ $01 ; 0 0 0 0 0 0 0 1
pio_b_data equ $02 ; 0 0 0 0 0 0 1 0
pio_b_control equ $03 ; 0 0 0 0 0 0 1 1
sio_a_data equ $20 ; 0 0 1 0 0 0 0 0
sio_a_control equ $21 ; 0 0 1 0 0 0 0 1
sio_b_data equ $22 ; 0 0 1 0 0 0 1 0
sio_b_control equ $23 ; 0 0 1 0 0 0 1 1
; PIO port B (LCD control port)
; 7 6 5 4 3 2 1 0
; x x x R/W RS CS2 CS1 E
; x x x H/L D/I H/L H H H
port_c_data equ $40
port_c_control equ $41
lcd_e_bit equ 0
lcd_rs_bit equ 3
cursor_xy equ $2000 ; 2 bytes
cmd_buffer_ptr equ $2002 ; 1 byte ($00 -> $ff)
char equ $2004 ; 1 byte
cmd_buffer equ $2006 ; 256 bytes ($2006 -> $2106)
org $00
reset:
jp setup
interrupt_vectors:
org $0C
defw ps2_char_available
org $0E
defw ps2_special_condition
; org $0038
; ; Interrupt setup
; di ; Disable interrupts
; ex af,af' ; Save register states
; exx ; Save register states
; ; PARSE CHARACTER
; in a,(port_c_data) ; Read what's on the data bus once Y2 is brought low
; ; The ASCII character code is now in A
; ld (char),a
; cp $7F ; Is it a backspace character?
; jp z,cursor_backspace
; cp $0A ; Is it a new line character?
; jp z,console_process_cmd
; cp $0D ; Is it a new line character?
; jp z,console_process_cmd
; ; cp $20 ; Is it a regular ASCII character (greater than or equal to [space])?
; ; jp c,exit_interrupt ; If not, bail here.
; ld de,(cursor_xy) ; If yes, print it!
; call lcd_send_char
; call cursor_inc
; ; Save character to command buffer
; ld a,(cmd_buffer_ptr)
; ld hl,cmd_buffer
; ld d,0
; ld e,a
; add hl,de
; ld a,(char)
; ld (hl),a
; ; Increment the command buffer pointer once done
; ld a,(cmd_buffer_ptr)
; inc a
; ld (cmd_buffer_ptr),a
; exit_interrupt:
; ; Interrupt setdown
; exx ; Restore register states
; ex af,af' ; Restore register states
; ei ; Enable interrupts
; ret
org $100
setup:
ld sp,$3fff
; im 1 ; Set interrupt mode 1 (go to $0038 on interrupt)
; ei ; Enable interrupts
call setup_sio
ld a,0 ; set high byte of interrupt vectors to point to page 0
ld i,a
im 2 ; set interrupt mode 2
ei ; enable interupts
; initialize output mode for PIO ports A and
ld a,$0f
out (pio_a_control),a
ld a,$0f
out (pio_b_control),a
call lcd_enable
; Set page
ld a,%10111000
call lcd_send_command
; Set column
ld a,%01000000
call lcd_send_command
; Set page
ld a,%10111000
call lcd_send_command_cs2
; Set column
ld a,%01000000
call lcd_send_command_cs2
; Set display start line
ld a,%11000000
call lcd_send_command
ld a,%11000000
call lcd_send_command_cs2
; Reset and clear
ld a,$00
ld (cursor_xy),a
ld (cursor_xy+1),a
ld (cmd_buffer_ptr),a
ld hl,cmd_buffer
ld de,cmd_buffer+1
ld bc,$100
ld (hl),$00
ldir
ld hl,prompt
call lcd_send_asciiz
main_loop:
halt
jp main_loop
setup_sio:
; Consult datasheet and manual for information on these settings.
ld a,%00110000 ; A WR0 Error reset, select WR0
out (sio_a_control),a
ld a,%00011000 ; A WR0 Channel reset, select WR0
out (sio_a_control),a
ld a,%00000100 ; A WR0 Select WR4
out (sio_a_control),a
ld a,%00000100 ; A WR4 x1 clock, 8 bit sync, 1 stop bit, odd parity, disable parity
out (sio_a_control),a
ld a,%00000101 ; A WR0 Select WR5
out (sio_a_control),a
ld a,%01100000 ; A WR5 Tx 8 bit chars, disable Tx
out (sio_a_control),a
ld a,%00000001 ; B WR0 Select WR1
out (sio_b_control),a
ld a,%00000100 ; B WR1 Status affects interrupt vector
out (sio_b_control),a
ld a,%00000010 ; B WR0 Select WR2
out (sio_b_control),a
ld a,%00000000 ; B WR2 Interrupt vector (bits 3-1 will be set according
; to conditions (110x for char available, 111x
; for special condition occurred)
out (sio_b_control),a
ld a,%00000001 ; A WR0 Select WR1
out (sio_a_control),a
ld a,%00011000 ; A WR1 Interrupt on all Rx chars, parity does not affect vector
out (sio_a_control),a
ld a,%00000011 ; A WR0 Select WR3
out (sio_a_control),a
ld a,%11000001 ; A WR3 8 bit chars, disable features, enable Rx
out (sio_a_control),a
ret
cursor_inc:
ld de,(cursor_xy)
ld a,d
cp $1f
jp z,cursor_inc_new_row
inc d
ld (cursor_xy),de
ret
cursor_inc_new_row:
ld d,0
ld a,e
cp $07
jp z,cursor_inc_reset
inc e
ld (cursor_xy),de
ret
cursor_inc_reset:
ld e,0
ld (cursor_xy),de
ret
cursor_backspace:
ld de,(cursor_xy)
; Are we at cmd_buffer character zero?
ld a,(cmd_buffer_ptr)
and a
jr z,console_backspace_done
ld a,d
and a
jr z,console_backspace_previous_row ; If X is 0, see if we can go back to the previous row.
dec d
jr console_backspace_print
console_backspace_previous_row:
ld a,e
and a
jr z,console_backspace_done ; If X is 0 and Y is 0, it's the first character. Stop!
ld d,$1f
dec e
jr console_backspace_print
console_backspace_print:
ld (cursor_xy),de
ld a,' '
call lcd_send_char
ld a,(cmd_buffer_ptr)
; Remove character from command buffer
ld hl,cmd_buffer
ld d,0
ld e,a
add hl,de
ld a,$00
ld (hl),a
ld a,(cmd_buffer_ptr)
dec a
ld (cmd_buffer_ptr),a
console_backspace_done:
jp exit_interrupt
console_new_line:
ld de,(cursor_xy)
ld d,0
ld a,e
cp $07 ; Are we on the final row?
jp z,console_new_line_reset
inc e
ld (cursor_xy),de
ret
console_new_line_reset:
ld e,0
ld (cursor_xy),de
ret
console_process_cmd:
call console_process_cmd_setup
ld de,data_cmd_help_1
call compare_strings
jp z,cmd_match_help
call console_process_cmd_setup
ld de,data_cmd_help_2
call compare_strings
jp z,cmd_match_help
call console_process_cmd_setup
ld de,data_cmd_help_3
call compare_strings
jp z,cmd_match_help
call console_process_cmd_setup
ld de,data_cmd_clear_1
call compare_strings
jp z,cmd_match_clear
call console_process_cmd_setup
ld de,data_cmd_clear_2
call compare_strings
jp z,cmd_match_clear
call console_process_cmd_setup
ld de,data_cmd_clear_3
call compare_strings
jp z,cmd_match_clear
call console_process_cmd_setup
ld de,data_cmd_tarot_1
call compare_strings
jp z,cmd_match_tarot
call console_process_cmd_setup
ld de,data_cmd_tarot_2
call compare_strings
jp z,cmd_match_tarot
jp cmd_match_none
cmd_match_help:
; Move down a row
call console_new_line
; Print help message
ld hl,data_msg_help
call lcd_send_asciiz
jp console_process_cmd_done
cmd_match_clear:
call lcd_clear
jp exit_interrupt
cmd_match_tarot:
; Move down a row
call console_new_line
; Print help message
ld hl,data_msg_tarot
call lcd_send_asciiz
jp console_process_cmd_done
cmd_match_none:
; Move down a row
call console_new_line
; Print error message
ld hl,data_msg_none
call lcd_send_asciiz
jp console_process_cmd_done
console_process_cmd_done:
ld a,$00
ld (cmd_buffer_ptr),a
ld hl,cmd_buffer
ld de,cmd_buffer+1
ld bc,$100
ld (hl),$00
ldir
; Print a new prompt (on a new line)
call console_new_line
ld hl,prompt
call lcd_send_asciiz
; Exit the interrupt
jp exit_interrupt
console_process_cmd_setup:
ld hl,cmd_buffer
call string_length
ld hl,cmd_buffer
ret
data_cmd_help_1:
db "help",0
data_cmd_help_2:
db "?",0
data_cmd_help_3:
db "HELP",0
data_cmd_clear_1:
db "clear",0
data_cmd_clear_2:
db "cls",0
data_cmd_clear_3:
db "CLEAR",0
data_cmd_tarot_1:
db "tarot",0
data_cmd_tarot_2:
db "TAROT",0
data_msg_none:
db "Invalid command.",0
data_msg_help:
db "[clear|cls]: Clear screen. ",
db "[tarot]: Draw Tarot card.",0
data_msg_tarot:
db "~* Magic *~",0
; Compare two strings stored in memory.
; -----------------------------------------------------------------------------
; HL - start address of string 1
; DE - start address of string 2
; B - length of string 1
;
; Output:
; Z flag - set if string1 == string 2
; Z flag - unset if string1 != string2
; -----------------------------------------------------------------------------
compare_strings:
ld a,(de)
cp (hl)
ret nz ; nz means they are not equal
inc hl
inc de
djnz compare_strings
cp a ; set the z flag, which means they're equal
ret
; Get length of null-terminated string stored in memory.
; -----------------------------------------------------------------------------
; HL - start address of the string.
;
; Output:
; B - length of the string.
; HL - corrupt.
; A - corrupt.
; -----------------------------------------------------------------------------
string_length:
ld b,$ff
dec hl
string_length_loop:
inc b
inc hl
ld a,(hl)
cp 0 ; Is the current character the null terminator?
jp nz,string_length_loop
ret
lcd_clear:
; Reset the cursor to start at the beginning of the screen
ld a,$00
ld (cursor_xy),a
ld (cursor_xy+1),a
ld b,$ff
lcd_clear_loop:
push bc
ld de,(cursor_xy)
ld a,' '
call lcd_send_char
call cursor_inc
pop bc
djnz lcd_clear_loop
; Reset and clear command buffer memory
ld a,$00
ld (cursor_xy),a
ld (cursor_xy+1),a
ld (cmd_buffer_ptr),a
ld hl,cmd_buffer
ld de,cmd_buffer+1
ld bc,$100
ld (hl),$00
ldir
; Print prompt
ld hl,prompt
call lcd_send_asciiz
ret
; Start of ASCIIZ (null-terminated ASCII) string in HL
; Cursor XY position in RAM
lcd_send_asciiz:
ld de,(cursor_xy)
ld a,(hl)
and a
jr z,lcd_send_asciiz_done
push hl
call lcd_send_char
pop hl
inc hl
call cursor_inc
jr lcd_send_asciiz
lcd_send_asciiz_done:
ret
; D - X position ($00 -> $1F)
; E - Y position ($00 -> $07)
; A - ASCII character code
lcd_send_char:
push de
sub $20
ld h,0
ld l,a
ld d,h ; Multiply by 3
ld e,l
add hl,hl
add hl,de ; This is now the offset from the font start in ROM
ld de,hl
ld hl,font
add hl,de ; This is now the start position of the character
pop de
; Set cursor position
ld (cursor_xy),de
; Set Y (page) position
ld a,%10111000
add a,e
call lcd_send_command
ld a,%10111000 ; Base instruction for Y position setting
add a,e
call lcd_send_command_cs2 ; We can send the page to both controllers at once
; Set X (line) position
ld c,d ; Copy D (character position in line) to C for later
ld a,d
add a,a ; Multiply X position by 4 (4 columns per character cell)
add a,a ; X position is now in A
ld d,a ; Copy it to D
cp $3c
jr c,lcd_send_char_x_cs1
jr z,lcd_send_char_x_cs1
lcd_send_char_x_cs2:
sub $40 ; Account for CS2 offset
ld d,a ; Update D
ld a,%01000000 ; Base instruction for X position setting
add a,d ; Offset by X position
call lcd_send_command_cs2
jr lcd_send_char_print_cs2
lcd_send_char_x_cs1:
ld a,%01000000 ; Base instruction for X position setting
add a,d ; Offset by X position
call lcd_send_command
jr lcd_send_char_print_cs1
lcd_send_char_print_cs1:
ld b,3
lcd_send_char_print_cs1_loop:
ld a,(hl)
call lcd_send_data
inc hl
djnz lcd_send_char_print_cs1_loop
ld a,$00 ; Add space between characters
call lcd_send_data
jr lcd_send_char_done
lcd_send_char_print_cs2:
ld b,3
lcd_send_char_print_cs2_loop:
ld a,(hl)
call lcd_send_data_cs2
inc hl
djnz lcd_send_char_print_cs2_loop
ld a,$00 ; Add space between characters
call lcd_send_data_cs2
jr lcd_send_char_done
lcd_send_char_done:
ret
lcd_enable:
ld a,%00000110
out (pio_b_data),a
ld a,%00111111
out (pio_a_data),a
ld a,%00000110
call lcd_latch
ret
lcd_busy_wait:
ld a,%00010001
out (pio_b_data),a
in a,(pio_a_data)
rlca
jr c,lcd_busy_wait
ret
; Data in A
; Out: A corrupt
lcd_send_command:
push af
call lcd_busy_wait
ld a,%00000010
out (pio_b_data),a ; 1. Copy control byte to port B
pop af
out (pio_a_data),a ; 2. Copy data byte to port A
ld a,%00000010
call lcd_latch
ret
lcd_send_command_cs2:
push af
call lcd_busy_wait
ld a,%00000100
out (pio_b_data),a ; 1. Copy control byte to port B
pop af
out (pio_a_data),a ; 2. Copy data byte to port A
ld a,%00000100
call lcd_latch
ret
lcd_send_data:
push af
call lcd_busy_wait
ld a,%00001010
out (pio_b_data),a ; 1. Copy control byte to port B
pop af
out (pio_a_data),a ; 2. Copy data byte to port A
ld a,%00001010
call lcd_latch
ret
lcd_send_data_cs2:
push af
call lcd_busy_wait
ld a,%00001100
out (pio_b_data),a ; 1. Copy control byte to port B
pop af
out (pio_a_data),a ; 2. Copy data byte to port A
ld a,%00001100
call lcd_latch
ret
lcd_latch:
set lcd_e_bit,a ; Set enable (command latch) bit
out (pio_b_data),a ; 3. Latch...
res lcd_e_bit,a ; Unset command latch bit
out (pio_b_data),a ; 4. ...and unlatch E
ret
ps2_char_available:
di ; Disable interrupts
push af
in a,(port_c_data)
cp $7F ; Is it a backspace character?
jp z,cursor_backspace
cp $0A ; Is it a new line character?
jp z,console_process_cmd
cp $0D ; Is it a new line character?
jp z,console_process_cmd
ld de,(cursor_xy) ; Else, print the character
call lcd_send_char
call cursor_inc
ld de,(cursor_xy)
call lcd_send_char
call cursor_inc
pop af
exit_interrupt:
ei
reti
ps2_special_condition:
jp $00
; err_bail:
; jp $800
; org $800:
; jp exit_interrupt
prompt:
db "> ",0
font:
db 0x00, 0x00, 0x00 ; [space]
db 0x00, 0x5c, 0x00 ; !
db 0x0c, 0x00, 0x0c ; "
db 0x7c, 0x28, 0x7c ; #
db 0x50, 0xfc, 0x28 ; $
db 0x64, 0x10, 0x4c ; %
db 0x28, 0x54, 0x68 ; &
db 0x00, 0x0c, 0x00 ; '
db 0x00, 0x38, 0x44 ; (
db 0x44, 0x38, 0x00 ; )
db 0x14, 0x08, 0x14 ; *
db 0x10, 0x38, 0x10 ; +
db 0x40, 0x20, 0x00 ; ,
db 0x10, 0x10, 0x10 ; -
db 0x00, 0x40, 0x00 ; .
db 0x60, 0x10, 0x0c ; /
db 0x78, 0x44, 0x3c ; 0
db 0x08, 0x7c, 0x00 ; 1
db 0x64, 0x54, 0x48 ; 2
db 0x44, 0x54, 0x28 ; 3
db 0x1c, 0x10, 0x7c ; 4
db 0x5c, 0x54, 0x24 ; 5
db 0x38, 0x54, 0x34 ; 6
db 0x64, 0x14, 0x0c ; 7
db 0x7c, 0x54, 0x7c ; 8
db 0x5c, 0x54, 0x3c ; 9
db 0x00, 0x28, 0x00 ; :
db 0x40, 0x28, 0x00 ; ;
db 0x10, 0x28, 0x44 ; <
db 0x28, 0x28, 0x28 ; =
db 0x44, 0x28, 0x10 ; >
db 0x04, 0x54, 0x0c ; ?
db 0x38, 0x44, 0x58 ; @
db 0x78, 0x14, 0x78 ; A
db 0x7c, 0x54, 0x28 ; B
db 0x38, 0x44, 0x44 ; C
db 0x7c, 0x44, 0x38 ; D
db 0x7c, 0x54, 0x54 ; E
db 0x7c, 0x14, 0x14 ; F
db 0x38, 0x54, 0x74 ; G
db 0x7c, 0x10, 0x7c ; H
db 0x44, 0x7c, 0x44 ; I
db 0x20, 0x44, 0x3c ; J
db 0x7c, 0x10, 0x6c ; K
db 0x7c, 0x40, 0x40 ; L
db 0x7c, 0x08, 0x7c ; M
db 0x7c, 0x04, 0x78 ; N
db 0x38, 0x44, 0x38 ; O
db 0x7c, 0x14, 0x08 ; P
db 0x38, 0x44, 0x78 ; Q
db 0x7c, 0x14, 0x68 ; R
db 0x48, 0x54, 0x24 ; S
db 0x04, 0x7c, 0x04 ; T
db 0x3c, 0x40, 0x7c ; U
db 0x3c, 0x40, 0x3c ; V
db 0x7c, 0x20, 0x7c ; W
db 0x6c, 0x10, 0x6c ; X
db 0x0c, 0x70, 0x0c ; Y
db 0x64, 0x54, 0x4c ; Z
db 0x00, 0x7c, 0x44 ; [
db 0x0c, 0x10, 0x60 ; \
db 0x44, 0x7c, 0x00 ; ]
db 0x08, 0x04, 0x08 ; ^
db 0x40, 0x40, 0x40 ; _
db 0x04, 0x08, 0x00 ; `
db 0x30, 0x48, 0x78 ; a
db 0x7c, 0x48, 0x30 ; b
db 0x30, 0x48, 0x48 ; c
db 0x30, 0x48, 0x7c ; d
db 0x30, 0x68, 0x58 ; e
db 0x10, 0x78, 0x14 ; f
db 0x30, 0xa8, 0x78 ; g
db 0x7c, 0x10, 0x60 ; h
db 0x00, 0x34, 0x40 ; i
db 0x80, 0x74, 0x00 ; j
db 0x7c, 0x10, 0x68 ; k
db 0x00, 0x3c, 0x40 ; l
db 0x78, 0x10, 0x78 ; m
db 0x78, 0x08, 0x70 ; n
db 0x30, 0x48, 0x30 ; o
db 0xf8, 0x48, 0x30 ; p
db 0x30, 0x48, 0xf8 ; q
db 0x78, 0x10, 0x08 ; r
db 0x50, 0x58, 0x28 ; s
db 0x08, 0x3c, 0x48 ; t
db 0x38, 0x40, 0x78 ; u
db 0x38, 0x40, 0x38 ; v
db 0x78, 0x20, 0x78 ; w
db 0x48, 0x30, 0x48 ; x
db 0x18, 0xa0, 0x78 ; y
db 0x48, 0x68, 0x58 ; z
db 0x10, 0x6c, 0x44 ; {
db 0x00, 0x7c, 0x00 ; |
db 0x44, 0x6c, 0x10 ; }
db 0x10, 0x30, 0x20 ; ~
align 8192