summaryrefslogtreecommitdiff
path: root/programs/subasm-2.s
blob: f10dac8db8305627d33ac4f3074ddf26cf59f7cc (plain)
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
; SuBAsm
; The Sux Bootstrapped Assembler.
;
; by mr b0nk 500 <b0nk@b0nk.xyz>

.org incl
; String Constants.
prg_name:
	.byte "SuBAsm"
ver_txt:
	.byte ", version "
ver_num:
	.byte "0.1"

; Directives.
dir:
	.byte "org"
	.byte "byte"
	.byte "word"
	.byte "dword"
	.byte "qword"
	.byte "include"

; Short form Commands.
sh_cmds:
	.byte "vlahirs"

; Commands.
cmds:
	.byte "viewmem"
	.byte "list"
	.byte "asm"
	.byte "help"
	.byte "inst"
	.byte "run"
	.byte "set"

; Instruction Mnemonics.
mne:
	.byte "AAB"
	.byte "ABA"
	.byte "ADC"
	.byte "AND"
	.byte "ARB"
	.byte "ASR"
	.byte "BCC"
	.byte "BCS"
	.byte "BEQ"
	.byte "BNE"
	.byte "BNG"
	.byte "BPO"
	.byte "BRK"
	.byte "BVC"
	.byte "BVS"
	.byte "CAB"
	.byte "CLC"
	.byte "CLI"
	.byte "CLV"
	.byte "CMP"
	.byte "CPB"
	.byte "CPS"
	.byte "CPX"
	.byte "CPY"
	.byte "DAB"
	.byte "DEB"
	.byte "DEC"
	.byte "DEX"
	.byte "DEY"
	.byte "DIV"
	.byte "ENT"
	.byte "INB"
	.byte "INC"
	.byte "INX"
	.byte "INY"
	.byte "JMP"
	.byte "JSL"
	.byte "JSR"
	.byte "LDA"
	.byte "LDB"
	.byte "LDX"
	.byte "LDY"
	.byte "LLB"
	.byte "LRB"
	.byte "LSL"
	.byte "LSR"
	.byte "MAB"
	.byte "MUL"
	.byte "NOP"
	.byte "OAB"
	.byte "ORA"
	.byte "PHA"
	.byte "PHB"
	.byte "PHP"
	.byte "PHX"
	.byte "PHY"
	.byte "PLA"
	.byte "PLB"
	.byte "PLP"
	.byte "PLX"
	.byte "PLY"
	.byte "RLB"
	.byte "ROL"
	.byte "ROR"
	.byte "RRB"
	.byte "RTI"
	.byte "RTL"
	.byte "RTS"
	.byte "SAB"
	.byte "SBC"
	.byte "SEC"
	.byte "SEI"
	.byte "STA"
	.byte "STB"
	.byte "STT"
	.byte "STX"
	.byte "STY"
	.byte "TAB"
	.byte "TAX"
	.byte "TAY"
	.byte "TBA"
	.byte "TSX"
	.byte "TXA"
	.byte "TXS"
	.byte "TXY"
	.byte "TYA"
	.byte "TYX"
	.byte "WAI"
	.byte "XAB"
	.byte "XOR"

; Command subroutine table.
cmd_srt:
	.word viewmem
	.word list
	.word asm
	.word help
	.word inst
	.word run
	.word set

; Hex character table.
hex_char:
	.byte "0123456789ABCDEF"


.org $4400
; Subroutine pointer.
sub_ptr:
	.word 0

; Indecies.
idx0:
	.word 0
idx1:
	.word 0
idx2:
	.word 0

; Program Counter.
prg_cnt:
	.qword 0

; Start of program code.
.org parser
subasm:
	ldb #0		; Set the first pointer
	lda.w #cmd_buf	; to the command buffer.
	jsl set_ptr	;
	tba		; Reset A.
	tax		; Reset X.
	jsl chk_shcmd	; Did we get a shortend command?
	bne parse_cmd	; Yes, so skip everything else.
	jsl chk_cmd	; No, but did we get a full command?
	bne parse_cmd	; Yes, so skip everything else.
	jsl lexer	; No, so start lexing this line.
subasm_end:
	rtl		; End of subasm.

parse_cmd:
	inb		; Set the second pointer
	lda.w #cmd_srt	; to the command subroutine table.
	jsl set_ptr	;
	deb		; Reset B.
	tba		; Reset A.
	lda f		; Get the command ID.
	cmp #8		; Is the command ID greater than the command count?
	bcs subasm_end	; Yes, so we're done.
	lsl #1		; No, so multiply the command ID by two.
	phy #2		; Preserve the screen buffer position.
	tay		; Set the index to the offset that we just calculated.
	lda.w (ptr2), y	; Get the command subroutine, from the command subroutine table.
	ply #2		; Get back the screen buffer position.
	ldb #2		; Save it in the third pointer.
	jsl set_ptr	;
	ldb #0		; Reset B.
	jsr (ptr3)	; Run the command's subroutine.
	jmp subasm_end	; We are done.

chk_shcmd:
	inb		; Set the second pointer
	lda.w #sh_cmds	; to the shortend command table.
	jsl set_ptr	;
	deb		; Reset B.
	tba		; Reset A.
	phy #2		; Preserve the screen buffer position.
	txy		; Set our index to zero.
	lda (ptr), y	; Is there nothing in the command buffer?
	beq shcmd_fail	; Yes, so return that we failed.
	cmp #' '	; No, but is this character, a space?
	beq shcmd_fail	; Yes, so return that we failed.
shcmd_loop:
	ldb (ptr2), y	; Are we at the end of the table?
	beq shcmd_fail	; Yes, so return that we failed.
	cab		; No, so did the character match?
	beq shcmd_fnd	; Yes, so check if there are any arguments.
	iny		; No, so check the next command.
	jmp shcmd_loop	; Keep looping.
shcmd_fnd:
	sty f		; Save the command ID.
	ldy #1		; Check the next character in the command buffer.
	lda (ptr), y	; Is this the end of the buffer?
	beq shcmd_true	; Yes, so return that we succeded.
	cmp #' '	; No, but is this a space?
	beq shcmd_true	; Yes, so return that we succeded.
	jmp shcmd_fail	; No, so return that we failed.
shcmd_true:
	lda #1		; Return true.
	jmp shcmd_end	; We are done.
shcmd_fail:
	tba		; Return false.
	tax		; Reset X.
shcmd_end:
	ldb #0		; Reset B.
	ply #2		; Get back the screen buffer position.
	rtl		; End of chk_shcmd.

print_hex:
	pha #8		; Preserve the hex value.
	ldb #1		; Set the second pointer
	lda.w hex_char	; to the start of hex character table.
	jsl set_ptr	;
	ldb #0		; Reset B.
	pla #8		; Get the hex value back.
pnthex_lp:
	pha #8		; Preserve the hex value.
	and #$F		; Mask the lowest nibble.
	phx #2		; Preserve the digit count.
	phy #2		; Preserve the screen buffer position.
	tay		; Get the index for the hex digit.
	lda (ptr2), y	; Get the hex digit.
	ply #2		; Get back the screen buffer position.
	jsl print_char	; Print the hex digit.
	plx #2		; Get the digit count back.
	pla #8		; Get the hex value back.
pnthex_lp1:
	cpx #1		; Is the digit count less than one?
	bcc pnthex_lp2	; Yes, so don't decrement the digit count.
	dex		; No, but was the digit count zero, when decremented?
	beq pnthex_end	; Yes, so we're done.
	jmp pnthex_lp3	; No, so get the next nibble.
pnthex_lp2:
	ldb #1		; Enable auto digit count.
pnthex_lp3:
	lsr #4		; No, but is the next nibble, a zero?
	beq pnthex_lp4	; Yes, so check if auto digit count is enabled.
	jmp pnthex_lp	; No, so print the next digit.
pnthex_lp4:
	cpb #1		; Is auto digit count enabled?
	beq pnthex_end	; Yes, so we're done.
	jmp pnthex_lp	; No, so keep printing more digits.
pnthex_end:
	ldb #0		; Reset B.
	rtl		; End of print_hex.

print_space:
	lda #' '	; Set the character to a space.
	phb #1		; Preserve the spacing count.
	ldb #1		; Enable replace mode.
	stb b		;
	jsl print_char	; Print the space.
	plb #1		; Get the spacing count back.
	deb		; Have we printed all of the spacing?
	beq pntsp_end	; Yes, so we're done.
	jmp print_space	; No, so keep printing spaces.
pntsp_end:
	rtl		; End of print_space.

viewmem:
	pha #8		; Preserve the address.
	lda #'\n'	; Print a newline.
	jsl print_char	;
	ldb #19		; Print 19 spaces.
	jsl print_space	;
vmem_lp:
	pla #8		; Get the address back.
	nop		;
vmem_end:
	rts		; End of viewmem.

list:
	nop		;
list_end:
	rts		; End of list.
asm:
	nop		;
asm_end:
	rts		; End of asm.
help:
	nop		;
help_end:
	rts		; End of help.
inst:
	nop		;
inst_end:
	rts		; End of inst.
run:
	nop		;
run_end:
	rts		; End of run.
set:
	nop		;
set_end:
	rts		; End of set.