From 095cacc7a00eb42fc0a5c5dc9754480dfc56c6a2 Mon Sep 17 00:00:00 2001 From: aleksei Date: Fri, 12 Apr 2024 11:26:27 +1000 Subject: Self contained program parsing --- 6502.lisp | 45 ++++++++++++++++++++++++++++----------------- main.lisp | 45 ++++++++++++++++++++++++++++++++++++++------- notes.lisp | 24 ------------------------ 3 files changed, 66 insertions(+), 48 deletions(-) delete mode 100644 notes.lisp diff --git a/6502.lisp b/6502.lisp index a7982d8..1e9c7d4 100644 --- a/6502.lisp +++ b/6502.lisp @@ -18,8 +18,25 @@ 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. +;;; List of addressing modes. +(setf + *addressing-modes* + '(immediate + absolute + zero-page + implied + indirect-absolute + absolute-indexed-x + absolute-indexed-y + zero-page-indexed-x + zero-page-indexed-y + indexed-indirect + indirect-indexed + relative + accumulator)) + +;;; Instructions, with decimal opcode and +;;; addressing modes. (setf *instructions* ;; Load & Store @@ -185,18 +202,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA (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 instruction and addressing mode combination correct?" + (cond + ((member addressing-mode + (caddr (assoc instruction *instructions*))) t) + (t nil))) -;; Is string hexadecimal? (defun hexd? (string) + "Is a string a hexd number?" (let ((stack ())) (dotimes (i (length string)) (push @@ -212,9 +226,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA (push 'and stack) (eval stack))) -;; Convert an arbitrarily sized hexadecimal number as -;; string, to a positive decimal integer. (defun hex2dec (string) + "Convert an arbitrarily sized hexd number (as string) to a positive decimal." (flet ((hex (c) (cond ((and (char-not-lessp c #\0) @@ -236,10 +249,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA (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 @@ -321,8 +332,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA (equal (length s) 1) (equal "A" (subseq s 0 1))))))) -;; EXAMPLE ;; Evaluate the second syntax rule on a string +;; temporary (funcall - (eval (cadar (cdr *addressing-modes-syntax*))) + (eval (cadr (assoc 'absolute *addressing-modes-syntax*))) "$A6AF") diff --git a/main.lisp b/main.lisp index 2888f89..4ec4440 100644 --- a/main.lisp +++ b/main.lisp @@ -18,13 +18,6 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |# -(setf - *source-file* - (open "/home/aleksei/lisp/wozmon.s")) - -(setf - *source-line-number* - 0) (defun interpret-character (s x) "Interpret meanings of characters." @@ -79,6 +72,44 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA (length word)))))))) +;; Working read-program +(defun read-program (source-filename) + (let ((line (read-line source-filename nil 'EOF))) + (if (equal line 'EOF) + nil + (let ((line-list (extract-words line))) + (if (equal line-list nil) + (progn + (incf *source-line-number*) + (read-program)) + (cons + (list (incf *source-line-number*) + (extract-words line)) + (read-program))))))) + + + +;; Self contained version +(defun program (source-filename) + "Turns an assembly source file into a list, prepended with line numbers." + (let ((source-file (open source-filename)) + (source-line-number 0)) + (labels + ((recurse () + (let ((line (read-line source-file nil 'EOF))) + (if (equal line 'EOF) + nil + (let ((line-list (extract-words line))) + (if (equal line-list nil) + (progn + (incf source-line-number) + (recurse)) + (cons + (list (incf source-line-number) + (extract-words line)) + (recurse)))))))) + (recurse)))) + (defun last-char (s) diff --git a/notes.lisp b/notes.lisp deleted file mode 100644 index 5ea7ef4..0000000 --- a/notes.lisp +++ /dev/null @@ -1,24 +0,0 @@ -;;;;; -*- mode: common-lisp -*- - -;; Addressing Mode Symbols -'(immediate - absolute - zero-page - implied - indirect-absolute - absolute-indexed-x - absolute-indexed-y - zero-page-indexed-x - zero-page-indexed-y - indexed-indirect - indirect-indexed - relative - accumulator) - -;; Applying a syntax rule defined -;; in *addressing-modes-syntax* -(funcall - (eval - (cadar - *addressing-modes-syntax*)) - "arg") -- cgit v1.2.3