summaryrefslogtreecommitdiff
path: root/6502.lisp
blob: a7982d8094a603ec4d276349b676a8dea07ed25b (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
324
325
326
327
328
;; -*- mode: common-lisp -*-
#|
clasm-6502: An assembler for the 6502 written in Common Lisp.
Copyright (C) 2024  Aleksei Eaves

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
|#

;; Every instruction, its decimal opcode, and the
;; usable addressing modes.
(setf
 *instructions*
 ;; Load & Store
 '((LDA 169 (immediate
			 absolute
			 zero-page
			 absolute-indexed-x
			 absolute-indexed-y
			 zero-page-indexed-x
			 indexed-indirect
			 indirect-indexed))
   (LDX 162 (immediate
			 zero-page
			 zero-page-indexed-y
			 absolute
			 absolute-indexed-y))
   (LDY 160 (immediate
			 zero-page
			 zero-page-indexed-x
			 absolute
			 absolute-indexed-x))
   (STA 137 (absolute
			 zero-page
			 absolute-indexed-x
			 absolute-indexed-y
			 zero-page-indexed-x
			 indexed-indirect
			 indirect-indexed))
   (STX 130 (zero-page
			 zero-page-indexed-y
			 absolute))
   (STY 128 (zero-page
			 zero-page-indexed-x
			 absolute))
   ;;Arithmetic
   (ADC 105 (immediate
			 absolute
			 zero-page
			 absolute-indexed-x
			 absolute-indexed-y
			 zero-page-indexed-x
			 indexed-indirect
			 indirect-indexed))
   (SBC 233 (immediate
			 absolute
			 zero-page
			 absolute-indexed-x
			 absolute-indexed-y
			 zero-page-indexed-x
			 indexed-indirect
			 indirect-indexed))
   ;;Increment & Decrement
   (INC 226 (zero-page
			 zero-page-indexed-x
			 absolute
			 absolute-indexed-x))
   (INX 232 (implied))
   (INY 200 (implied))
   (DEC 194 (zero-page
			 zero-page-indexed-x
			 absolute
			 absolute-indexed-x))
   (DEX 202 (implied))
   (DEY 136 (implied))
   ;; Logical
   (AND  41 (immediate
			 absolute
			 zero-page
			 absolute-indexed-x
			 absolute-indexed-y
			 zero-page-indexed-x
			 indexed-indirect
			 indirect-indexed))
   (ORA   9 (immediate
			 absolute
			 zero-page
			 absolute-indexed-x
			 absolute-indexed-y
			 zero-page-indexed-x
			 indexed-indirect
			 indirect-indexed))
   (EOR  73 (immediate
			 absolute
			 zero-page
			 absolute-indexed-x
			 absolute-indexed-y
			 zero-page-indexed-x
			 indexed-indirect
			 indirect-indexed))
   ;; Jump, Branch, Compare
   (JMP  72 (absolute
			 indirect-absolute))
   (BCC 144 (relative))
   (BCS 176 (relative))
   (BEQ 240 (relative))
   (BNE 208 (relative))
   (BMI  48 (relative))
   (BPL  16 (relative))
   (BVS 112 (relative))
   (BVC  80 (relative))
   (CMP 201 (immediate
			 absolute
			 zero-page
			 absolute-indexed-x
			 absolute-indexed-y
			 zero-page-indexed-x
			 indexed-indirect
			 indirect-indexed))
   (CPX 224 (immediate
			 zero-page
			 absolute))
   (CPY 192 (immediate
			 zero-page
			 absolute))
   (BIT  32 (zero-page
			 absolute))
   ;; Shift & Rotate
   (ASL  10 (accumulator
			 zero-page
			 zero-page-indexed-x
			 absolute
			 absolute-indexed-x))
   (LSR  74 (accumulator
			 zero-page
			 zero-page-indexed-x
			 absolute
			 absolute-indexed-x))
   (ROL  42 (accumulator
			 zero-page
			 zero-page-indexed-x
			 absolute
			 absolute-indexed-x))
   (ROR 106 (accumulator
			 zero-page
			 zero-page-indexed-x
			 absolute
			 absolute-indexed-x))
   ;; Transfer
   (TAX 170 (implied))
   (TAY 168 (implied))
   (TXA 138 (implied))
   (TYA 152 (implied))
   ;; Stack
   (TSX 186 (implied))
   (TXS 154 (implied))
   (PHA  72 (implied))
   (PHP   8 (implied))
   (PLA 104 (implied))
   (PLP  40 (implied))
   ;; Subroutine
   (JSR  32 (implied))
   (RTI  64 (implied))
   (RTS  96 (implied))
   ;; Set & Reset
   (CLC  24 (implied))
   (CLD 216 (implied))
   (CLI  88 (implied))
   (CLV 184 (implied))
   (SEC  56 (implied))
   (SED 248 (implied))
   (SEI 120 (implied))
   ;; Other
   (NOP 234 (implied))
   (BRK   0 (implied))))

;; Predicate: is a combination of instruction
;; and addressing mode correct?
(defun valid-instruction? (instruction addressing-mode)
  (dolist (x *instructions* nil)
	(when
		(and
		 (equal (car x) instruction)
		 (member addressing-mode (caddr x)))
	  (return T))))

;; Is string hexadecimal?
(defun hexd? (string)
  (let ((stack ()))
	(dotimes (i (length string))
	  (push
	   (or (and (char-not-lessp
				 (char string i) #\0)
				(char-not-greaterp
				 (char string i) #\9))
		   (and (char-not-lessp
				 (char string i) #\A)
				(char-not-greaterp
				 (char string i) #\F)))
	   stack))
	(push 'and stack)
	(eval stack)))

;; Convert an arbitrarily sized hexadecimal number as
;; string, to a positive decimal integer.
(defun hex2dec (string)
  (flet ((hex (c)
		   (cond
			 ((and (char-not-lessp c #\0)
				   (char-not-greaterp c #\9))
			  (- (char-code c)
				 (char-code #\0)))
			 ((and (char-not-lessp c #\A)
				   (char-not-greaterp c #\F))
			  (+ (- (char-code (char-downcase c))
					(char-code #\a))
				 10)))))
	(let ((ret 0))
	  (do ((i 0 (incf i))
		   (j (- (length string) 1) (decf j)))
		  ((minusp j) ())
		(setf ret
			  (+ ret
				 (* (expt 16 j)
					(hex (char string i))))))
	  ret)))

;;(define-compiler-macro (list)
;; A list with with the respective rules of some
;; addressing mode syntax.
;; ... ... ... could definitely macro most of them.
(setf
 *addressing-modes-syntax*
 '((immediate ; #?? ... more complex syntax rules for later
	(lambda (s)
	  (eq "#" (subseq s 0 1))))
   (absolute ;"$????"
	(lambda (s)
	  (and
	   (equal (length s) 5)
	   (equal "$" (subseq s 0 1))
	   (hexd? (subseq s 1 5)))))
   (zero-page ;"$??"
	(lambda (s)
	  (and
	   (equal (length s) 3)
	   (equal "$" (subseq s 0 1))
	   (hexd? (subseq s 1 3)))))
   (implied nil)
   (indirect-absolute ;($????)
	(lambda (s)
	  (and
	   (equal (length s) 7)
	   (equal "($" (subseq s 0 2))
	   (hexd? (subseq s 1 5))
	   (equal ")" (subseq s 5 6)))))
   (absolute-indexed-x ;"$????,X"
	(lambda (s)
	  (and
	   (equal (length s) 7)
	   (equal "$" (subseq s 0 1))
	   (hexd? (subseq s 1 5))
	   (equal ",X" (subseq s 5 7)))))
   (absolute-indexed-y ;"$????,Y"
	(lambda (s)
	  (and
	   (equal (length s) 7)
	   (equal "$" (subseq s 0 1))
	   (hexd? (subseq s 1 5))
	   (equal ",Y" (subseq s 5 7)))))
   (zero-page-indexed-x ;"$??,X"
	(lambda (s)
	  (and
	   (equal (length s) 5)
	   (equal (subseq s 0 1) "$")
	   (hexd? (subseq s 1 3))
	   (equal (subseq s 3 5) ",X"))))
   (zero-page-indexed-y ;"$??,Y"
	(lambda (s)
	  (and
	   (equal (length s) 5)
	   (equal (subseq s 0 1) "$")
	   (hexd? (subseq s 1 3))
	   (equal (subseq s 3 5) ",Y"))))
   (indexed-indirect ;"($??,X)"
	(lambda (s)
	  (and
	   (equal (length s) 7)
	   (equal (subseq s 0 2) "($")
	   (hexd? (subseq s 2 4))
	   (equal (subseq s 4 7) ",X)"))))
   (indirect-indexed ;"($??),Y"
	(lambda (s)
	  (and
	   (equal (length s) 7)
	   (equal (subseq s 0 2) "($")
	   (hexd? (subseq s 2 4))
	   (equal (subseq s 4 7) "),Y"))))
   ;;How to fix that relative and absolute are the same rule?
   ;;A check upstream would suffice.
   (relative ;"$????"
	(lambda (s)
	  (and
	   (equal (length s) 5)
	   (equal (subseq s 0 1) "$")
	   (hexd? (subseq s 1 5)))))
   (accumulator ;"A"
	(lambda (s)
	  (and
	   (equal (length s) 1)
	   (equal "A" (subseq s 0 1)))))))

;; EXAMPLE
;; Evaluate the second syntax rule on a string
(funcall
 (eval (cadar (cdr *addressing-modes-syntax*)))
 "$A6AF")