/
6502.asm
374 lines (316 loc) · 6.42 KB
/
6502.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
;;;;
; Operating system for a custom 6502 computer.
; Nick Bild - nick.bild@gmail.com
; https://github.com/nickbild/6502_os
;
; Reserved memory:
; $0000 - LCD enable
; $0001 - Unused -- read it to disable any IC (except RAM).
; $0002 - Current LCD cursor position.
; $0003 - Flag to indicate if next scan code should be skipped.
; $0100-$01FF - 6502 stack
; $7FBE-$7FBF - Temporary location for LCD data manipulation.
; $7FC0-$7FFF - Data to write to LCD.
; Each character (16 x 2 lines) is represented by
; 2 consecutive bytes (4-bit mode).
; Most sig. 4 bits are for LCD data.
; Least sig. 4 bits - only bit 3 used (tied to RS pin).
;
; $FFF8 - Clock keyboard shift register and enable line buffer.
; $FFF9 - Reset binary counter (counts bits received from PS/2 keyboard packets).
;
; $FFFA - NMI IRQ Vector
; $FFFB - NMI IRQ Vector
; $FFFC - Reset Vector - Stores start address of this ROM.
; $FFFD - Reset Vector
; $FFFE - IRQ Vector - Keyboard ISR address.
; $FFFF - IRQ Vector
;;;;
processor 6502
; Named variables in RAM.
ORG $0002
CursorPosition
.byte #$00
SkipNextScanCodeFlag
.byte #$00
; Start at beginning of ROM.
StartExe ORG $8000
sei
jsr InitLcd
jsr ZeroLCDRam
jsr ResetKeyboardCounter
; Initialize LCD cursor position to 0.
lda #$00
sta CursorPosition
; Do not skip the first scan code.
lda #$00
sta SkipNextScanCodeFlag
cli
MainLoop
jmp MainLoop
;;;
; Long Delay
;;;
Delay ldx #$FF
DelayLoop1 ldy #$FF
DelayLoop2 dey
bne DelayLoop2
dex
bne DelayLoop1
rts
;;;
; Short Delay
;;;
DelayShort ldx #$80
DelayShortLoop1 dex
bne DelayShortLoop1
rts
;;;
; Send high pulse to LCD enable pin.
;;;
LcdCePulse sta $01
jsr DelayShort
sta $00
jsr DelayShort
sta $01
jsr DelayShort
rts
;;;
; LCD initialization sequence.
;;;
InitLcd jsr Delay
lda #$30 ; 00110000 - data 0011, RS 0
jsr LcdCePulse
jsr Delay
lda #$30
jsr LcdCePulse
jsr Delay
lda #$30
jsr LcdCePulse
lda #$20
jsr LcdCePulse
jsr DelayShort
; Set 8 bit, 2 line, 5x8.
lda #$20
jsr LcdCePulse
lda #$80
jsr LcdCePulse
; Display on.
lda #$00
jsr LcdCePulse
lda #$C0
jsr LcdCePulse
; Clear display.
lda #$00
jsr LcdCePulse
lda #$10
jsr LcdCePulse
jsr Delay
; Entry mode.
lda #$00
jsr LcdCePulse
lda #$60
jsr LcdCePulse
rts
;;;
; Write LCD-reserved RAM addresses to LCD.
;;;
WriteLCD lda #$80 ; Line 1 : 1000 (line1) 0000 (RS 0)
jsr LcdCePulse
lda #$00 ; Position 0 : 0000 (position 0) 0000 (RS 0)
jsr LcdCePulse
ldy #$00
Line1Loop lda $7FC0,y
jsr LcdCePulse
iny
cpy #$20
bcc Line1Loop
lda #$C0 ; Line 2 : 1100 (line2) 0000 (RS 0)
jsr LcdCePulse
lda #$00 ; Position 0 : 0000 (position 0) 0000 (RS 0)
jsr LcdCePulse
ldy #$00
Line2Loop lda $7FE0,y
jsr LcdCePulse
iny
cpy #$20
bcc Line2Loop
rts
;;;
; Zero out LCD reserved RAM (set all positions to space character).
;;;
ZeroLCDRam ldx #$00
ZeroLoop lda #$28
sta $7FC0,x
inx
lda #$08
sta $7FC0,x
inx
cpx #$40
bcc ZeroLoop
rts
;;;
; Reset binary counter tracking receipt of complete packets from keyboard.
;;;
ResetKeyboardCounter
lda $0001
lda $FFF9
lda $0001
rts
;;;
; Read in all of the initialization data from the keyboard and discard it.
;;;
SkipKeyboardInit
ldy #$00
KBInitLoop jsr ResetKeyboardCounter
jsr DelayShort
iny
cpy #$20
bcc KBInitLoop
rts
;;;
; Keyboard Interrupt Service Routine.
;;;
KbIsr
pha
.byte #$DA ; phx - mnemonic unknown to DASM.
; Is this scan code flagged to be skipped?
ldx SkipNextScanCodeFlag
cpx #$01
beq SkipScanCodeAndResetSkip
; Display shift register contents on output pins,
; and enable line buffer.
; Read shift register contents into accumulator.
lda $FFF8
; Skip key up scan codes.
; Remeber to skip the next scan code after them.
cmp #$F0
bne DoNotSkipScanCode
ldx #$01
stx SkipNextScanCodeFlag
jmp SkipScanCode
DoNotSkipScanCode
; Convert scan code into LCD code.
sbc #$14
tax
lda ScanCodeLookup,x
; Store data in memory location read by LCD.
sta $7FBE
sta $7FBF
; Make sure RS (bit 3) is set to 1.
lda #$0F
ora $7FBE
; If CursorPosition >= 64, reset it to 0.
ldx CursorPosition
cpx #$40
bcc CursorPositionLessThan32
ldx #$00
stx CursorPosition
CursorPositionLessThan32
ldx CursorPosition
sta $7FC0,x
inc CursorPosition
; Move least sig. nibble to most sig. position, then make sure RS is 1.
rol $7FBF
rol $7FBF
rol $7FBF
rol $7FBF
lda #$0F
ora $7FBF
ldx CursorPosition
sta $7FC0,x
inc CursorPosition
jsr WriteLCD
SkipScanCodeAndResetSkip
; Do not skip the next scan code.
ldx #$00
stx SkipNextScanCodeFlag
; Finished ISR, reset binary counter.
SkipScanCode
lda $FFF9
lda $0001
.byte #$FA ; plx
pla
rti
; PS/2 Code Set 2 Scan Code Lookup Table
; Subtract $14 from scan code to determine
; byte offset into this table.
; Value at lookup location is character
; LCD data corresponding to scan code.
ScanCodeLookup
.byte #$51 ; Q
.byte #$31 ; 1
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$5A ; Z
.byte #$53 ; S
.byte #$41 ; A
.byte #$57 ; W
.byte #$32 ; 2
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$43 ; C
.byte #$58 ; X
.byte #$44 ; D
.byte #$45 ; E
.byte #$34 ; 4
.byte #$33 ; 3
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$20 ; <space>
.byte #$56 ; V
.byte #$46 ; F
.byte #$54 ; T
.byte #$52 ; R
.byte #$35 ; 5
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$4E ; N
.byte #$42 ; B
.byte #$48 ; H
.byte #$47 ; G
.byte #$59 ; Y
.byte #$36 ; 6
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$4d ; M
.byte #$4a ; J
.byte #$55 ; U
.byte #$37 ; 7
.byte #$38 ; 8
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$2C ; ,
.byte #$4B ; K
.byte #$49 ; I
.byte #$4F ; O
.byte #$30 ; 0
.byte #$39 ; 9
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$2E ; .
.byte #$2F ; /
.byte #$4C ; L
.byte #$3B ; ;
.byte #$50 ; P
.byte #$2D ; -
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$27 ; '
.byte #$20 ; unused
.byte #$5B ; [
.byte #$3D ; =
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$20 ; unused
.byte #$20 ; <enter>
; Store the location of key program sections.
ORG $FFFC
ResetVector
.word StartExe ; Start of execution.
IrqVector
.word KbIsr ; Interrupt service routine.