-
Notifications
You must be signed in to change notification settings - Fork 4
/
Primatives.65s
472 lines (414 loc) · 19.8 KB
/
Primatives.65s
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
; LIBRARY FILE - PRIMATIVES
; version 2.20
; two pass symbolic assembler, dissembler and tracer
; REGB bits 0-3 are connected to LED's
; tested in Michael Kowalski 6502 Simulator - using 65C02 Code
; Programmed by Joe DiMeglio
; https://github.com/jdimeglio/6502-Monitor
; bug fixes
;------------------------------------------------------------------------
;prints a string ending with EOS - checkign for packed strings
;looks for the string after teh JSR statement
;------------------------------------------------------------------------
STRING PHP ;print stringz to screen
PHA
PHX
PHY
LDA SRCE ;Save SRCE
PHA
LDA SRCE+1
PHA
TSX
LDA $0107,x ;get return address-1
STA SRCE ;from the stack
LDA $0108,x
STA SRCE+1
.NEXTCHAR INC SRCE
BNE .SKIPBUMP ;edge of page
INC SRCE+1
.SKIPBUMP LDY #$01 ;trick to save some bytes
LDA (SRCE)
BEQ .ADJUST_RETURN ;EOS Marker
BPL .NOTPACK ;is it packed?
JSR UNPACK_DECODE ;return character AC and count YR
.NOTPACK JSR WRCHAR ;write character
DEY
BNE .NOTPACK
BRA .NEXTCHAR
.ADJUST_RETURN LDA SRCE
STA $0107,x
LDA SRCE+1
STA $0108,x
PLA ;Restore SRCE variable
STA SRCE+1
PLA
STA SRCE
PLY
PLX
PLA
PLP
RTS
;------------------------------------------------------------------------
; ANSI GOTO x & y
;------------------------------------------------------------------------
GOTOXY
.IF MYWYM
PHA ;goto [ <X> ; <Y> H
LDA #ESC ;Escape codes
JSR WRCHAR
LDA #'['
JSR WRCHAR
TYA
JSR WRDECI ;write in decimal
LDA #';'
JSR WRCHAR
TXA
JSR WRDECI
LDA #'H'
JSR WRCHAR
PLA
RTS
.ELSE
STX io_posx ;use simlulator
STY io_posy
RTS
.ENDIF
;------------------------------------------------------------------------
; ANSI - CSI n x See https://en.wikipedia.org/wiki/ANSI_escape_code
; low level
;------------------------------------------------------------------------
WRCSI PHA ;save value
LDA #ESC
JSR WRCHAR
LDA #'['
JSR WRCHAR
TXA
JSR WRCHAR ;write in decimal
PLA
JSR WRCHAR ;write ANSI Mode
RTS
;------------------------------------------------------------------------
; ANSI - Clear Screen; ;.BYTE ESC,"[2J",EOS
; low level
;------------------------------------------------------------------------
CLS
.IF MYWYM
LDX #'2'
LDA #'J' ;Clrscrn escape code
JSR WRCSI ;
RTS
.ELSE
STA io_cls ;use simlulator
RTS
.ENDIF
;------------------------------------------------------------------------
; ANSI - Bring Cursor Home
;------------------------------------------------------------------------
HOME
.IF MYWYM
JSR STRING
.ASCII ESC
.ASCIS "[1;1H"
RTS
.ELSE
LDX #00
LDY #00
JMP GOTOXY
.ENDIF
;------------------------------------------------------------------------
; ANSI - Save Cursor Position
;------------------------------------------------------------------------
SAVECUR JSR STRING
.ASCII ESC
.ASCIS "[s"
RTS
;------------------------------------------------------------------------
; ANSI - Restore Cursor Position
;------------------------------------------------------------------------
RESTCUR JSR STRING
.ASCIS ESC,"[u"
RTS
;------------------------------------------------------------------------
; Print a dot
;------------------------------------------------------------------------
WRDOT LDA #'.' ;Print a dot
BRA WRCHAR
;------------------------------------------------------------------------
; Check Tabs
;------------------------------------------------------------------------
TABS3
.IF MYWYM
LDA #'C'
LDX #20
JSR WRCSI
.ELSE
PHA
LDA io_posx
LDA #20
;CLC
;ADC #06
STA io_posx
PLA
.ENDIF
RTS
;------------------------------------------------------------------------
; Print a space
;------------------------------------------------------------------------
WRSPACE LDA #SP ;Print a space
BRA WRCHAR
;------------------------------------------------------------------------
; Print a CR
;------------------------------------------------------------------------
WRCRLF LDA #CR ;Print a CR & LF
JSR WRCHAR
WRLF LDA #LF ;fall through to WRCHAR
;------------------------------------------------------------------------
;WRCHAR Out charater to the screen
; Low level
;------------------------------------------------------------------------
WRCHAR
.IF LCD_ROUTINES
BBR #01, WR2LCD,.SERIAL
JMP LCD_WRCHAR
.SERIAL
.ENDIF
.IF MYWYM
PHP ;save flags
PHA
.WAITTX LDA #%00010000
BIT ASTAT
BEQ .WAITTX
PLA
STA ADATA
PLP
RTS
.ELSE
STA io_putc ;Else use simulator
RTS
.ENDIF
;------------------------------------------------------------------------
;Print Blank Lines & print N Blank lines
; x= times
;------------------------------------------------------------------------
WRBLNK LDX #$03 ;Blank count x3
WRBL2 LDA #SP
BRA REPWRCHAR ;repeat WR
;------------------------------------------------------------------------
;REPWRCHAR Out charater to the screen
; A= Char x= times
;------------------------------------------------------------------------
DRAWLINECR JSR WRCRLF
DRAWLINE LDA #DASH
LDX #78 ;columns on the screen 80-2
JSR REPWRCHAR
JMP WRCRLF
REPWRCHAR JSR WRCHAR
DEX
BNE REPWRCHAR
RTS
;------------------------------------------------------------------------
;convert binary into a ASCII characters
;write to screen eg xx
;------------------------------------------------------------------------
WR2HEX PHA
LSR ;write 2 hex digits
LSR
LSR
LSR
JSR WR1HEX
PLA
WR1HEX CLD ;write 1 character HEX eg x
AND #$0f
CMP #$0a
BCC .z09
ADC #$06 ;carry is set
.z09 ADC #$30
BRA WRCHAR
;------------------------------------------------------------------------
; Print word in hex
; A holds MSB, Y holds LSB, eg $xxxx
;------------------------------------------------------------------------
WRWORD PHA ;Save MSB
LDA #'$' ;Print dollar symbol
JSR WRCHAR
PLA
WRWORDAY JSR WR2HEX ;Print MSB
TYA
BRA WR2HEX ;Print LSB
;------------------------------------------------------------------------
; SCAN_ESC if key is pressed
; Low level
;------------------------------------------------------------------------
SCAN_ESC
.IF MYWYM
LDA #%00001000
AND ACIA+1 ; ACIA Status register
BEQ .NOTHING ; Anything in the buffer
JSR READACIA
.NOTHING CMP #ESC
RTS
.ELSE
LDA KBDCR ;Was a key pressed?
CMP #ESC ;Make Z=1 if ESC
RTS
.ENDIF
;------------------------------------------------------------------------
; Wait for keypressed
; Low level
;------------------------------------------------------------------------
RDCHAR
.IF MYWYM
;CLC ;Read ASCII byte into AC
.WAITRX LDA ACIA+1
AND #%00001000 ;ACIA Status register
BEQ .WAITRX
READACIA LDA ACIA
PHA
LDA ACIA+1 ;clear status register
PLA
RTS
.ELSE
LDA KBDCR ;Else use simulator
BEQ RDCHAR
RTS
.ENDIF
;------------------------------------------------------------------------
;Read 1 character and convert it to HEX
;------------------------------------------------------------------------
RD1HEX JSR RDCHAR
BRA HEX2BIN
;------------------------------------------------------------------------
;Read 2 HEX character and convert it to BIN
;------------------------------------------------------------------------
RD2HEX JSR RD1HEX
ASL
ASL
ASL
ASL
STA TEMP1
JSR RD1HEX
ORA TEMP1
RTS
;------------------------------------------------------------------------
;Convert AC in HEX out BIN
;------------------------------------------------------------------------
HEX2BIN EOR #'0' ;Ignore ASCII part for decimals
CMP #9+1
BCC .okhex ;It was a decimal digit!
ADC #$88 ;Map letters A - F to $FA-FF
CMP #$fa
BCC .invalid ;No hex digit anymore!
AND #$0F
.okhex RTS
.invalid LDA #FAIL
RTS
;--------------------------------------------------------------
;Restore saved registers - used for Tracing
;--------------------------------------------------------------
RESTORE LDA SAVP ;Restore 6502 Register Contents
PHA ;Used by tracing software
LDA SAVA
LDX SAVX
LDY SAVY
PLP
RTS
;------------------------------------------------------------------------
; Save HW registers
;------------------------------------------------------------------------
SAVE STA SAVA ;Save 6502 HW registers
STX SAVX
STY SAVY
PHP
PLA
STA SAVP ;svae the flags
TSX
STX SAVS ;stack pointer
RTS
;------------------------------------------------------------------------
;Convert keyboard buffer to uppercase
;------------------------------------------------------------------------
UPPERCASE LDA IN,x
CMP #'z'+1
BCS .NEXTDIGIT
CMP #'a'
BCC .NEXTDIGIT
SBC #SP
STA IN,x
.NEXTDIGIT DEX
BPL UPPERCASE ;end of string
RTS
.IF LCD_ROUTINES
;------------------------------------------------------------------------
; *** LCD initialisation
;------------------------------------------------------------------------
LCD_LINIT LDX #$04 ;do function set 4 times
.LINIT0 LDA #%00111000 ;function set: 8 bit, 2 lines, 5x7
STA LCD0
JSR LCD_BUSY ;wait for busy flag to clear
DEX
BNE .LINIT0
LDA #%00000110 ;entry mode set: increment, no shift
STA LCD0
JSR LCD_BUSY
LDA #%00001110 ;display on, cursor on, blink off
STA LCD0
JSR LCD_BUSY
LDA #$01 ;clear display
STA LCD0
JSR LCD_BUSY
LDA #$80 ;DDRAM address set: $00
STA LCD0
JSR LCD_BUSY
JSR LCD_CLR
INC WR2LCD
JSR STRING
.ASCII "LCD initiated ",EOS
RTS
;------------------------------------------------------------------------
;LCD Scroll
;------------------------------------------------------------------------
LCD_SCROLL LDA #%0000001
;------------------------------------------------------------------------
;LCD Clear screen
;------------------------------------------------------------------------
LCD_CLR LDA #%00000001
STA LCD0
JSR LCD_BUSY
RTS
;------------------------------------------------------------------------
;LCD_home
;------------------------------------------------------------------------
LCD_HOME LDA #%00000011
STA LCD0
JSR LCD_BUSY
RTS
;------------------------------------------------------------------------
;Wait for LCD busy bit to clear
;------------------------------------------------------------------------
LCD_BUSY PHA
.LCD_BUSY0 LDA LCD0 ;read from LCD register 0
AND #%10000000 ;check bit 7 (busy)
BNE .LCD_BUSY0
PLA
RTS
;------------------------------------------------------------------------
; *** Print character on LCD
;------------------------------------------------------------------------
LCD_WRCHAR STA LCD1
JSR LCD_BUSY
LDA LCD0 ;get current DDRAM address
AND #%01111111 ;everthing but busy flag
CMP #20 ;wrap from pos $13 (line 1 char 20)...
BNE .LCDPRINT0
LDA #$C0 ;...to $40 (line 2 char 1)
STA LCD0
JSR LCD_BUSY
.LCDPRINT0 RTS
;;------------------------------------------------------------------------
;prints a string ending with EOS
;------------------------------------------------------------------------
;LCD_STRING INC WR2LCD
; JSR STRING
; STZ WR2LCD
; RTS
.ENDIF ;End of LCD Routines