-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.asm
More file actions
671 lines (486 loc) · 11.4 KB
/
Copy pathkernel.asm
File metadata and controls
671 lines (486 loc) · 11.4 KB
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
ddrb = $6002
ddra = $6003
portb = $6000
porta = $6001
; ACIA chip registers
acia_data = $5000 ; data register
acia_stat = $5001 ; status register ; on write we reset the ACIA
acia_comm = $5002 ; command register
acia_ctrl = $5003 ; control register
; PORTA 0-2 : E - R/WB - RS
; PORTB 0-7 : LCD data bus in order
; PAGE ZERO VARIABLES
serial_string_send_low = $05
serial_string_send_high = $06
; Constants
CR = $0D ; carriage return
ESC = $1B
PROMPT = "$" ; prompt character
; Monitor variables
line_buffer = $200 ; starts at $200 and spans till $27F ; 127 characters like the original wozmon
serial_buffer = $300 ; from $300 to $3ff
L = $28 ; hex value parsing low
H = $29 ; hex value parsing high
XAML = $24 ; last opened location low
XAMH = $25 ; last opened location high
MODE = $23 ; MODE
YSAV = $22 ; last Y index we had
STL = $20 ; store address low
STH = $21 ; store address high
ACIA_ERROR = $2A ; check this for serial errors
ACIA_NUMBER = $2B
q_front = $2C ; front of serial queue
q_back = $2D ; back of serial queue
.org $8000
sei
init:
; set up VIA ports
lda #$ff
sta ddrb
lda #$07
sta ddra
cld
ldx #$ff
txs ; initialize stack pointer
; write things to zero page
; set up the circular queue back and front variables for the serial buffer
lda #0
sta q_back
sta q_front
; initialise LCD
jsr krnl_init_lcd
; now write hello world string
lda #'O'
jsr krnl_write_lcd_charac
lda #'K'
jsr krnl_write_lcd_charac
;ACIA init code
sta acia_stat ; soft reset
lda #$09
sta acia_comm
lda #$1F ; full speed at 19200 baud
sta acia_ctrl ;
clc
; send greeting string over serial
lda #$00
sta serial_string_send_low
lda #$B0
sta serial_string_send_high
jsr krnl_send_string_serial ; print the greeting string over serial
ram_test: ; test if ram from 0x0000-0x3FFF is available
; stack is empty at this point so we shouldn't worry about anything
lda #$55
sta $00
sta $01
lda $00
cmp #$55 ; test
bne ram_test_error
lda $01
cmp #$55 ; test
bne ram_test_error
; now we are sure we can use this location to store our address counter to test the rest
lda #02
sta $00
lda #00
sta $01
lda #$55
ldy #00
ram_cont:
sta ($00),Y
lda ($00),Y
cmp #$55 ; test
bne ram_test_error
; we need a 16 bit counter
ldx $00 ; load lower byte of address ( counter )
cpx #$ff
beq adjust_address
inc $00 ; increment address
jmp _cont
adjust_address :
inc $01 ; increment upper byte
ldx #00
stx $00 ; reset lower byte
_cont:
; check is we reached the upper boundary yet, when A14 is high
ldx $01 ; upper byte of address
cpx #$40 ; test for A14
bne ram_cont
jmp ram_test_success
ram_test_error:
lda #(ram_error_message & 0xFF) ; lower byte first
sta serial_string_send_low
lda #(ram_error_message >> 8 ) ; higher byte first
sta serial_string_send_high
jsr krnl_send_string_serial ; send error message over serial
jmp * ; stay here if error
ram_test_success:
lda #(ram_ok_message & 0xFF )
sta serial_string_send_low
lda #(ram_error_message >> 8 )
sta serial_string_send_high
jsr krnl_send_string_serial ; send ok message over serial
cli ; enable interrupts
; ********************************************************* init done falls through to augustiner monitor :)
ldy #-2
; CURRENT MONITOR PROGRAM
augustiner: ; for now takes arguments in accumulator
notcr:
cmp #ESC ; is it the escape key ?
beq getline
iny
bpl getchar ; auto esc if line longer than 127 characters
getline:
lda #$0A
jsr krnl_send_chr_serial
lda #CR
jsr krnl_send_chr_serial
escape:
lda #PROMPT
jsr krnl_send_chr_serial
ldy #0 ; reset y
getchar:
jsr krnl_rx_serial ; wait for character
sta line_buffer,Y
jsr krnl_send_chr_serial ; echo to serial
cmp #CR
bne notcr
;falls through if CR since we have a line
; parse the line since we have it
ldy #-1 ; reset index , -1 since we will pass through iny
lda #0 ; set default mode to single listing
tax
setmode:
sta MODE
skip:
iny
next_token:
lda line_buffer,Y
cmp #" " ; skip spaces
beq skip
cmp #CR
beq getline ; done restart
cmp #"." ; block listing mode
beq setmode ;set mode
cmp #":" ; set STOR mode
beq setmode
cmp #"S" ; check for load from serial mode
beq setmode
cmp #"R" ; check for run mode
bne notrun ; run program from last location XAML
jmp run
notrun:
stx L ; x = 0
stx H
sty YSAV ; save Y index for later
nexthex:
lda line_buffer,Y ; get next ascii byte
eor #$30 ; 0-9 : $30-$39
cmp #10 ; check if within range 0-9
bcc parse ;ok we are done
; if not check if it is within A-F
adc #$88 ; map letter from "A"-"F" to $FA-FF
; we need the $F before the first digit since it will be shifted out
cmp #$FA
bcs parse
; try to map "a" - "f" to $FA-$FF and check again
adc #$20 ; $20 difference between A and a
cmp #$FA
bcc not_hex
parse:
; shift nibble to MSD , since we want to use C bit thereafter
asl
asl
asl
asl
shift_store: ; shift hex nibble into L and H locations
ldx #4 ; we will shift 4 times
shift_again:
asl ; MSB to carry
rol L ; shift MSB into both L and H combined
rol H
dex
bne shift_again
iny ; increment index to a possible next hex digit
bne nexthex
not_hex:
; check if we have at least one hex digit
cpy YSAV
beq getline ; no restart
lda MODE
cmp #"." ; multiple listing mode
beq xamnext
cmp #"S" ; load from serial mode
bne not_serial
jmp load_serial
not_serial:
cmp #":" ; check if STOR mode
bne notstormode
stormode: ; ************* STORMODE
lda L
sta (STL,X)
inc STL
bne next_token
inc STH
hello:
jmp next_token
; fall through to print address and data
notstormode:
ldx #2
setadr:
lda L-1,X
sta STL-1,X
sta XAML-1,X
dex
bne setadr
nxtprnt:
bne prdata ; used for branch after MOD8 to print new line
lda #$0A ; new line feed
jsr krnl_send_chr_serial
lda #CR
jsr krnl_send_chr_serial
lda XAMH
jsr krnl_print_hex
lda XAML
jsr krnl_print_hex
lda #":"
jsr krnl_send_chr_serial
prdata:
lda #" "
jsr krnl_send_chr_serial
lda (XAML,X)
jsr krnl_print_hex
xamnext:
stx MODE ; x = 0 dunno why
; now check if (XAMH;XAML) still < (L;H)
lda XAML ;
cmp L
lda XAMH
sbc H
bcs hello ; we are not less , done ! , continue line parsing
inc XAML ; increment index of address
bne mod8 ; no carry from incrementing XAML
inc XAMH
mod8:
lda XAML
and #$07 ; if 0 then we have x % 8 == 0
bpl nxtprnt ; always taken
; *******************************
run : jmp (XAML)
; **********************************
; load bytes from serial, place them at address XAMH;XAML and load (H;L) bytes
load_serial:
ldx #0
load_next_byte:
cpx L
bne not_done
cpX H
beq load_done
not_done:
jsr krnl_rx_serial ; wait for byte
bit ACIA_ERROR ; check for errors
bmi acia_error
sta (XAML,X) ; store
jsr krnl_print_hex
lda #" "
jsr krnl_send_chr_serial
; decrement counter
dec L
bpl dec_ok ; no borrow
dec H
dec_ok:
; increment XAML
inc XAML
bne load_next_byte; no carry
inc XAMH
bne load_next_byte ; always taken
load_done:
jmp next_token ; goto to monitor
acia_error:
lda #(acia_error_message & 0xFF) ; lower byte first
sta serial_string_send_low
lda #(acia_error_message >> 8 ) ; higher byte first
sta serial_string_send_high
jsr krnl_send_string_serial ; send error message over serial
jmp next_token
;***********************************************************
;* *
;* KERNEL_FUNCTIONS *
;* *
;***********************************************************
krnl_print_hex: ; print the hex value that is in the accumulator
pha
pha
lsr
lsr
lsr
lsr
jsr krnl_convert_nibble_hex ; convert higher nibble first
jsr krnl_send_chr_serial
pla
jsr krnl_convert_nibble_hex
jsr krnl_send_chr_serial
pla
rts
krnl_print_lcd_hex: ; print the hex value that is in accumulator
pha
pha
lsr
lsr
lsr
lsr
jsr krnl_convert_nibble_hex ; convert higher nibble first
jsr krnl_write_lcd_charac
pla
jsr krnl_convert_nibble_hex
jsr krnl_write_lcd_charac
pla
rts
krnl_convert_nibble_hex ; converts the lower nibble in A to hex
and #$0f
ora #"0" ; add "0"
cmp #"9"+1 ; check if we are passed "0"-"9"
bcc krnl_convert_done
adc #6 ; put the character in the "A"-"F" offset
krnl_convert_done:
rts
krnl_send_string_serial: ; expects the string location to be null terminated and the location in the variable defined above
; string is limited to 127 characters
pha
phy
ldy #0
cont_sending:
lda (serial_string_send_low),Y
cmp #00
beq string_sending_done
jsr krnl_send_chr_serial
iny
jmp cont_sending
string_sending_done:
ply
pla
rts
krnl_send_chr_serial: ; expects input argument in accumulator
sta acia_data
jsr krnl_tx_byte_delay
rts
;***************************************************************************************
krnl_rx_serial: ; this routine reads from the queue, and waits for the data if none is available
phx
ldx q_front
wait:
cpx q_back ; check if back != front
beq wait ; data is available in the queue
lda serial_buffer,X
inx ; increment front buffer and store back
stx q_front
plx
rts
;***********************************************************************************
krnl_tx_byte_delay:
phx
phy
ldy #10
cnt1:
ldx #$80 ; TODO: calculate exact value we need
cnt:
dex
bne cnt
dey
bne cnt1
ply
plx
rts
krnl_init_lcd:
lda #$00
sta porta ; E set to 0
jsr delay ; wait for > 30 ms
lda #$30
jsr krnl_send_lcd_instr
jsr delay
jsr krnl_send_lcd_instr
jsr delay
jsr krnl_send_lcd_instr
jsr delay
lda #$38
jsr krnl_send_lcd_instr
lda #$10
jsr krnl_send_lcd_instr
lda #$0C
jsr krnl_send_lcd_instr
lda #$06
jsr krnl_send_lcd_instr
lda #$01
jsr krnl_send_lcd_instr ; clear display
;set dram address
lda #$80
jsr krnl_send_lcd_instr
rts
delay: ; used by lcd init
phx
phy
ldy #$80
loop3:
ldx #$ff
loop2: dex
bne loop2
dey
bne loop3
; restore the 3 registers
ply
plx
rts
fast_delay: ; used for sending lcd characters pulse width >= 300ns
pha
lda #200
dec_more:
dec ; 2 cycles
bne dec_more ; 3 cycles on average
pla
rts
krnl_send_lcd_instr:
sta portb
ldx #$01 ; instruction - write - E = 1
stx porta
jsr delay
ldx #$00
stx porta
rts
krnl_write_lcd_charac:
pha
phx
sta portb
ldx #$05 ; data - write - E = 1
stx porta
jsr fast_delay
ldx #$04 ; R/S still high
stx porta
plx
pla
rts
acia_routine: ; reads into line_buffer stored at $300 goes upto $3ff
pha
phx
ldx q_back
lda acia_stat ; just to clear
lda acia_data
; store the data in the circular queue
; store at back and increment back then pray for no buffer overflow
; back points to an empty location
sta serial_buffer,X
inx ; increment back pointer and store it back
stx q_back
plx
pla
rti
.org $b000
; **************************************
; PROGRAM STRINGS
serial_greeting: db "Welcome to the Robz6502 SBC project" , $A , $D , $00
ram_ok_message: db "memory: 0x000-0x3FFF OK..." , $A , $D, $00
ram_error_message : db "RAM TEST ERROR: could not R/W from RAM" , $A , $D , $00
monitor_help_message : db "Augustiner Monitor Program:" , $A , $D , "{h}: to display this message"
db $A , $D , "{m}: display zero page memory" , $A , $D , $00
acia_error_message: db "ACIA overrun!! exiting" , $A , $D , $00
.org $fffc ; 6502 reset vector
.word $8000
.word acia_routine ; no interrupt routine