Skip to content
Newer
Older
100644 73 lines (62 sloc) 3.58 KB
c2ba881 @ahefner 6502 assembler.
authored
1 ;;;; Cycle-counting mode for 6502 assembler
2
3 (in-package :asm6502)
4
5 (defparameter *cycle-count*
6 #(7 6 NIL NIL NIL 3 5 NIL 3 2 2 NIL NIL 4 6 NIL 4 5 NIL NIL NIL 4 6 NIL 2
7 4 NIL NIL NIL NIL 7 NIL 6 6 NIL NIL 3 3 5 NIL 4 2 2 NIL 4 4 6 NIL 2 5
8 NIL NIL NIL 4 6 NIL 2 4 NIL NIL NIL 4 7 NIL 4 6 NIL NIL NIL 3 5 NIL 3 2
9 2 NIL 3 6 6 NIL 2 5 NIL NIL NIL 4 6 NIL 2 4 NIL NIL NIL 4 7 NIL 6 6 NIL
10 NIL NIL 3 5 NIL 4 2 2 NIL 5 NIL 6 NIL 2 5 NIL NIL NIL 4 6 NIL 2 4 NIL
11 NIL NIL 4 7 NIL NIL 6 NIL NIL 3 3 3 NIL 2 NIL 2 NIL 4 4 4 NIL 2 6 NIL
12 NIL 4 4 4 NIL 2 5 2 NIL NIL 5 NIL NIL 2 6 2 NIL 3 3 3 NIL 2 2 2 NIL 4 4
13 4 NIL 2 5 NIL NIL 4 4 4 NIL 2 4 2 NIL 4 4 4 NIL 2 6 NIL NIL 3 3 5 NIL 2
14 2 2 NIL 4 4 6 NIL 2 5 NIL NIL NIL 4 6 NIL 2 4 NIL NIL NIL 4 7 NIL 2 6
15 NIL NIL 3 3 5 NIL 2 2 2 NIL 4 4 6 NIL 2 5 NIL NIL NIL 4 6 NIL 2 4 NIL
16 NIL NIL 4 7 NIL))
17
18 (defparameter *variable-timing*
19 #(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL T NIL
20 NIL NIL NIL NIL NIL NIL NIL T NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL
21 NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL T NIL NIL NIL NIL NIL
22 NIL NIL NIL T NIL NIL NIL T NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL
23 NIL NIL NIL NIL NIL NIL NIL T T NIL NIL NIL NIL NIL NIL NIL T NIL NIL
24 NIL T NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL
25 NIL NIL T T NIL NIL NIL NIL NIL NIL NIL T NIL NIL NIL T NIL NIL NIL NIL
26 NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL T NIL NIL NIL
27 NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL
28 NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL T T NIL NIL NIL NIL NIL NIL NIL
29 T NIL NIL T T T NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL
30 NIL NIL NIL T T NIL NIL NIL NIL NIL NIL NIL T NIL NIL NIL T NIL NIL NIL
31 NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL T NIL NIL
32 NIL NIL NIL NIL NIL NIL T NIL NIL NIL T NIL NIL) )
33
34 (defun opcode-cycles (opcode)
35 "Determine the number of cycles required to execute an
36 opcode. Returns two values: the number of cycles, and a boolean value
37 which is T if the opcode may take a variable number of cycles to
38 execute. The cycles table is not complete; if the number of cycles is
39 unknown, the first value is NIL and the second T."
40 (if (aref *cycle-count* opcode)
41 (values (aref *cycle-count* opcode)
42 (aref *variable-timing* opcode))
43 (values nil t)))
44
45 ;;; You'd think this would be sufficient to let you count cycles even
46 ;;; in nested non-cycle-counting context, but sorry, no.
47 (defgeneric context-note-cycles (context num-cycles)
48 (:method (c n) (declare (ignore c n)))
e1e2e60 @ahefner Simplify delayed evaluation and resolution (no need for multiple pass…
authored
49 (:method ((context delegate-context) num-cycles)
c2ba881 @ahefner 6502 assembler.
authored
50 (context-note-cycles (context-parent context) num-cycles)))
51
e1e2e60 @ahefner Simplify delayed evaluation and resolution (no need for multiple pass…
authored
52 (defclass cycle-counting-context (delegate-code-vector
53 delegate-symbol-lookup)
c2ba881 @ahefner 6502 assembler.
authored
54 ((cycle-count :initform 0 :accessor cycle-count :initarg :cycle-count)
85a331f @ahefner Formatting changes.
authored
55 (precise-p :initform t :accessor precise-p :initarg :precise-p)))
c2ba881 @ahefner 6502 assembler.
authored
56
57 (defmethod context-note-cycles ((context cycle-counting-context) num-cycles)
58 (incf (cycle-count context) num-cycles))
59
60 (defmethod context-emit-instruction ((context cycle-counting-context) vector)
61 (multiple-value-bind (cycles variable) (opcode-cycles (aref vector 0))
62 (when variable (setf (precise-p context) nil))
63 (if cycles
64 (context-note-cycles context cycles)
65 (warn "Don't know number of cycles for opcode ~X" (aref vector 0)))
66 (call-next-method)))
67
68 (defmacro counting-cycles (&body body)
69 `(let ((*context* (make-instance 'cycle-counting-context :parent *context*)))
70 ,@body
71 (cycle-count *context*)))
72
Something went wrong with that request. Please try again.