-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdp-11.lisp
162 lines (133 loc) · 4.09 KB
/
pdp-11.lisp
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
(defparameter *r0* 0)
(defparameter *r1* 0)
(defparameter *pc* 0)
(defmacro while (test &body body)
`(do ()
((not ,test))
,@body))
(defun load-memory (path)
(with-open-file (s path :element-type '(unsigned-byte 8))
(let ((mem (make-array (file-length s) :element-type '(unsigned-byte 8))))
(read-sequence mem s)
mem)
))
(defun myhd (vec)
(loop for x across vec
for i = 0 then (+ i 1)
do (when (= (rem i 16) 0)
(format t "~%~8,'0x " i))
(format t "~2,'0x " x)))
(defun read16 (mem i)
(logior (elt mem i) (ash (elt mem (+ i 1)) 8)))
(defun write16 (mem r n)
(setf (elt mem r) (logand n #b0000000011111111))
(setf (elt mem (+ r 1)) (ash n -8)))
(defun reasem (mem)
(let ((i 0))
(while (< i (length mem))
(format t "~4,'0x: " i)
(case (read16 mem i)
(#x15c0 (format t "~4,'0x ~4,'0x mov $~x, r0~%"
(read16 mem i) (read16 mem (+ i 2)) (read16 mem (+ i 2)))
(incf i 4))
(#x8901 (format t "~4,'0x sys 1 ; exit~%" (read16 mem i))
(incf i 2))
(#x8904 (format t "~4,'0x sys 4 ; write~%" (read16 mem i))
(incf i 2)
(format t "~4,'0x: ~4,'0x ; arg~%" i (read16 mem i))
(incf i 2)
(format t "~4,'0x: ~4,'0x ; arg~%" i (read16 mem i))
(incf i 2))
(t (format t "~4,'0x ???~%" (read16 mem i))
(incf i 2))
)
)))
(defun dump-mov-n-rn (mem n)
(format t "~4,'0x ~4,'0x mov $~x, r~x~%"
(read16 mem *pc*) (read16 mem (+ *pc* 2)) (read16 mem (+ *pc* 2)) n)
(incf *pc* 4))
(defun dump-mov-n-*rn (mem n)
(format t "~4,'0x ~4,'0x mov $~x, (r~x)~%"
(read16 mem *pc*) (read16 mem (+ *pc* 2)) (read16 mem (+ *pc* 2)) n)
(incf *pc* 4))
(defun dump-mov-n-*r+n (mem n)
(format t "~4,'0x ~4,'0x ~4,'0x mov $~x, ~x(r~x)~%"
(read16 mem *pc*) (read16 mem (+ *pc* 2)) (read16 mem (+ *pc* 4))
(read16 mem (+ *pc* 2)) (read16 mem (+ *pc* 4)) n)
(incf *pc* 6))
(defun dump-sys-exit (mem)
(format t "~4,'0x sys 1 ; exit~%" (read16 mem *pc*))
(incf *pc* 4))
(defun dump-sys-write (mem)
(format t "~4,'0x sys 4 ; write~%" (read16 mem *pc*))
(incf *pc* 2)
(format t "~4,'0x: ~4,'0x ; arg~%" *pc* (read16 mem *pc*))
(incf *pc* 2)
(format t "~4,'0x: ~4,'0x ; arg~%" *pc* (read16 mem *pc*))
(incf *pc* 2))
(defun reasem (mem)
(let ((*pc* 0))
(while (< *pc* (length mem))
(format t "~4,'0x: " *pc*)
(case (read16 mem *pc*)
(#x15c0 (dump-mov-n-rn mem 0))
(#x15c1 (dump-mov-n-rn mem 1))
(#x15c9 (dump-mov-n-*rn mem 1))
(#x15f1 (dump-mov-n-*r+n mem 1))
(#x8901 (dump-sys-exit mem))
(#x8904 (dump-sys-write mem))
(t (format t "~4,'0x ???~%" (read16 mem *pc*))
(incf *pc* 2))
)
)))
(defun run-d (path)
(let* ((mem (load-memory path))
(tsize (read16 mem 2))
(dsize (read16 mem 4)))
(reasem (subseq mem 16 (+ 16 tsize) ))))
(defun mov-n-r0 (mem)
(setf *r0* (read16 mem (+ *pc* 2)))
(incf *pc* 4))
(defun mov-n-r1 (mem)
(setf *r1* (read16 mem (+ *pc* 2)))
(incf *pc* 4))
(defun mov-n-*r1 (mem)
(write16 mem *r1* (read16 mem (+ *pc* 2)))
(incf *pc* 4))
(defun mov-n-*r1+n (mem)
(write16 mem (+ *r1* (read16 mem (+ *pc* 4))) (read16 mem (+ *pc* 2)))
(incf *pc* 6))
(defun sys-exit (mem)
(setf *pc* -1))
(defun sys-write (mem)
(let* ((s (read16 mem (+ *pc* 2)))
(len (read16 mem (+ *pc* 4)))
(str (map 'string #'character (subseq mem s (+ s len)))))
(case *r0*
(1 (format t str))
)
(incf *pc* 6)
))
(defun run1 (mem tsize)
(let ((*pc* 0)
(*r0* 0))
(while (and (< *pc* tsize) (> *pc* -1))
(case (read16 mem *pc*)
(#x15c0 (mov-n-r0 mem))
(#x15c1 (mov-n-r1 mem))
(#x15c9 (mov-n-*r1 mem))
(#x15f1 (mov-n-*r1+n mem))
(#x8901 (sys-exit mem))
(#x8904 (sys-write mem))
(t (format t "~4,'0x: ~4,'0x ???~%" *pc* (read16 mem *pc*))
(setf *pc* -1))
)
)))
(defun run (path)
(let* ((mem (load-memory path))
(tsize (read16 mem 2))
(dsize (read16 mem 4)))
(run1 (subseq mem 16) tsize)))
;(defvar *vec* (make-array 5 :fill-pointer 0 :adjustable t))
(defparameter *testmem* (vector #xc0 #x15 #x01 #x00
#x04 #x89 #x10 #x00 #x06 #x00 ))