public
Last active

DCPU-16 Lisp implementation

  • Download Gist
gistfile1.lisp
Common 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 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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
(defpackage #:dcpu-16
(:use #:cl)
(:export
#:make-ram #:ram-read #:ram-write #:ram-size
#:execute #:debug
#:load-program #:assemble-program
#:set #:add #:sub #:mul
#:div #:mod #:shl #:shr
#:and #:bor #:xor #:ife
#:ifn #:ifg #:ifb #:jsr
#:+))
 
(in-package #:dcpu-16)
 
;;;; DCPU-16
 
;;; Implementation of http://0x10c.com/doc/dcpu-16.txt
 
(deftype word () '(unsigned-byte 16))
 
;;; Memory
 
(defun make-ram (&optional (size #x10000))
"Return a new memory object with SIZE words. All words are
initially set to zero."
(make-array size
:element-type 'word
:initial-element 0))
 
(defun ram-read (n ram)
"Read the Nth word in RAM and return it."
(aref ram n))
 
(defun ram-write (value n ram)
"Write the VALUE to the Nth word in RAM."
(setf (aref ram n)
(logand value #xFFFF)))
 
(defun ram-size (ram)
"Return the size of the RAM in words."
(array-dimension ram 0))
 
;;; Registers
 
(defun make-register-file ()
"Return a new register file, i.e. storage for the DCPU-16's
registers. Registers are all initially set to zero."
(make-array 11
:element-type 'word
:initial-element 0))
 
(defun reg-index (name)
"Return the index of the register named NAME in the register file."
(position name '(a b c x y z i j pc sp o)))
 
(defun reg-read (name file)
"Read the value of the register named NAME in FILE."
(aref file (reg-index name)))
 
(defun reg-write (value name file)
"Write VALUE to the register named NAME in FILE."
(setf (aref file (reg-index name))
(logand value #xFFFF)))
 
;;; Instruction execution and debugging
 
(defvar *register-file* nil)
(defvar *ram* nil)
(defvar *cycles-count* nil)
(defvar *skip-instruction* nil)
 
(defun set-o (value)
(reg-write value 'o *register-file*))
 
(defun get-pc ()
(reg-read 'pc *register-file*))
 
(defun set-pc (value)
(reg-write value 'pc *register-file*))
 
(defmacro saving-pc (&body forms)
(let ((pc (gensym)))
`(let ((,pc (get-pc)))
(multiple-value-prog1
(progn ,@forms)
(set-pc ,pc)))))
 
(defun get-sp ()
(reg-read 'sp *register-file*))
 
(defun set-sp (value)
(reg-write value 'sp *register-file*))
 
(defun skip-next-instruction ()
(incf *cycles-count*)
(setf *skip-instruction* t))
 
(defun reset-skip-instruction ()
(setf *skip-instruction* nil))
 
(defun consume-word ()
(let ((pc (get-pc)))
(set-pc (1+ pc))
(ram-read pc *ram*)))
 
(defun execute-set (operand-1 operand-2)
(operand-write (operand-read operand-2) operand-1))
 
(defun execute-add (operand-1 operand-2)
(let ((sum (+ (operand-read operand-1) (operand-read operand-2))))
(set-o (if (> sum #xFFFF) 1 0))
(operand-write sum operand-1)))
 
(defun execute-sub (operand-1 operand-2)
(let ((diff (- (operand-read operand-1) (operand-read operand-2))))
(set-o (if (< diff 0) #xFFFF 0))
(operand-write diff operand-1)))
 
(defun execute-mul (operand-1 operand-2)
(let ((prod (* (operand-read operand-1) (operand-read operand-2))))
(set-o (ash prod -16))
(operand-write prod operand-1)))
 
(defun execute-div (operand-1 operand-2)
(let ((a (operand-read operand-1))
(b (operand-read operand-2)))
(cond ((zerop b)
(set-o 0)
(operand-write 0 operand-1))
(t
(set-o (floor (ash a 16) b))
(operand-write (floor a b) operand-1)))))
 
(defun execute-mod (operand-1 operand-2)
(let ((a (operand-read operand-1))
(b (operand-read operand-2)))
(if (zerop b)
(operand-write 0 operand-1)
(operand-write (mod a b) operand-1))))
 
(defun execute-shl (operand-1 operand-2)
(let ((shifted (ash (operand-read operand-1) (operand-read operand-2))))
(set-o (ash shifted -16))
(operand-write shifted operand-1)))
 
(defun execute-shr (operand-1 operand-2)
(let ((a (operand-read operand-1))
(b (operand-read operand-2)))
(set-o (ash (ash a 16) (- b)))
(operand-write (ash a (- b)) operand-1)))
 
(defun execute-and (operand-1 operand-2)
(operand-write (logand (operand-read operand-1) (operand-read operand-2))
operand-1))
 
(defun execute-bor (operand-1 operand-2)
(operand-write (logior (operand-read operand-1) (operand-read operand-2))
operand-1))
 
(defun execute-xor (operand-1 operand-2)
(operand-write (logxor (operand-read operand-1) (operand-read operand-2))
operand-1))
 
(defun execute-ife (operand-1 operand-2)
(when (/= (operand-read operand-1) (operand-read operand-2))
(skip-next-instruction)))
 
(defun execute-ifn (operand-1 operand-2)
(when (= (operand-read operand-1) (operand-read operand-2))
(skip-next-instruction)))
 
(defun execute-ifg (operand-1 operand-2)
(when (<= (operand-read operand-1) (operand-read operand-2))
(skip-next-instruction)))
 
(defun execute-ifb (operand-1 operand-2)
(when (zerop (logand (operand-read operand-1) (operand-read operand-2)))
(skip-next-instruction)))
 
(defun execute-jsr (operand-1)
(let ((sp (get-sp)))
(set-sp (1- sp))
(ram-write (get-pc) (1- sp) *ram*)
(set-pc (operand-read operand-1))))
 
(defun process-instruction (basic-function non-basic-function)
(let* ((word (consume-word))
(bits (extract-basic-opcode-bits word)))
(if (zerop bits)
(funcall non-basic-function
(find-non-basic-opcode-by-bits
(extract-non-basic-opcode-bits word))
(read-operand word 10))
(funcall basic-function
(find-basic-opcode-by-bits bits)
(read-operand word 4)
(read-operand word 10)))))
 
(defun extract-basic-opcode-bits (word)
(logand word #x0F))
 
(defun extract-non-basic-opcode-bits (word)
(logand (ash word -4) #x3F))
 
(defun extract-operand-bits (word base)
(logand (ash word (- base)) #x3F))
 
(defun execute-instruction ()
(process-instruction #'execute-basic-instruction
#'execute-non-basic-instruction))
 
(defun execute-basic-instruction (opcode operand-1 operand-2)
(cond (*skip-instruction*
(reset-skip-instruction))
(t (funcall (opcode-execute-function opcode) operand-1 operand-2)
(incf *cycles-count* (opcode-cycles-count opcode))
(incf *cycles-count* (operand-cycles-count (car operand-1)))
(incf *cycles-count* (operand-cycles-count (car operand-2))))))
 
(defun execute-non-basic-instruction (opcode operand-1)
(cond (*skip-instruction*
(reset-skip-instruction))
(t (funcall (opcode-execute-function opcode) operand-1)
(incf *cycles-count* (opcode-cycles-count opcode))
(incf *cycles-count* (operand-cycles-count (car operand-1))))))
 
(defun read-operand (word base)
(let* ((bits (extract-operand-bits word base))
(operand (find-operand-by-bits bits)))
(cons operand
(ecase (operand-length operand)
(0 nil)
(1 (consume-word))))))
 
(defun operand-read (operand-and-word)
(operand-read-1 (car operand-and-word) (cdr operand-and-word)))
 
(defun operand-write (value operand-and-word)
(operand-write-1 value (car operand-and-word) (cdr operand-and-word)))
 
(defun operand-format (operand-and-word)
(operand-format-1 (car operand-and-word) (cdr operand-and-word)))
 
(defun execute (ram)
(let ((*register-file* (make-register-file))
(*ram* ram)
(*cycles-count* 0)
(*skip-instruction* nil)
(last-pc -1))
(set-sp #xFFFF)
(loop
(execute-instruction)
(if (= last-pc (get-pc))
(return)
(setf last-pc (get-pc))))))
 
(defun debug (ram)
(let ((*register-file* (make-register-file))
(*ram* ram)
(*cycles-count* 0)
(*skip-instruction* nil))
(set-sp #xFFFF)
(catch 'debug-exit
(loop
(print-debug-info)
(let ((command (prompt-debug-command)))
(when (consp command)
(apply (command-function (car command))
(cdr command))))))))
 
(defun command-function (command)
(or (get command 'debug-function)
(unknown-command command)))
 
(defun unknown-command (command)
(lambda (&rest args)
(format t "Unknown command ~S (with args ~S).~%" command args)))
 
(defmacro define-debug-command ((&rest aliases) (&rest params) &body forms)
`(let ((fn (lambda (,@params) ,@forms)))
(dolist (alias ',aliases)
(setf (get alias 'debug-function) fn))
',aliases))
 
(define-debug-command (p step) ()
(execute-instruction))
 
(define-debug-command (d words) (offset)
(print-words (ram-words offset *ram*) offset))
 
(define-debug-command (g step-until) (&optional offset)
(loop for pc = (get-pc)
and last-pc = -1 then pc
until (or (= pc last-pc) (and offset (= pc offset)))
do (execute-instruction)))
 
(define-debug-command (u disasm) (&optional (n 2))
(saving-pc
(loop repeat n
do (format t "~4,'0X: ~A~%" (get-pc) (current-instruction)))))
 
(define-debug-command (q bye quit) ()
(throw 'debug-exit nil))
 
(defun print-debug-info ()
(print-register-file *register-file*)
(format t "Cycles: ~D Instruction: ~A ~:[~;SKIPPING~]~%"
*cycles-count*
(saving-pc (current-instruction))
*skip-instruction*))
(defun print-words (words &optional (base-offset 0))
(loop for i from 0
for word = (pop words)
while word
do (when (zerop (mod i 8))
(format t "~&~4,'0X: " (+ base-offset i)))
(format t "~4,'0X " word)
finally (terpri)))
 
(defun ram-words (offset ram)
(loop repeat 16
for i from offset
when (< i (ram-size ram))
collect (ram-read i ram)))
 
(defun print-register-file (file)
(loop for reg in '(a b c x y z i j pc sp o)
do (format t "~A: ~4,'0X " reg (reg-read reg file))))
 
(defun prompt-debug-command ()
(format t "> ")
(read (make-concatenated-stream
(make-string-input-stream "(")
(make-string-input-stream (read-line))
(make-string-input-stream ")"))))
 
(defun current-instruction ()
(process-instruction #'current-basic-instruction
#'current-non-basic-instruction))
 
(defun current-basic-instruction (opcode operand-1 operand-2)
(list (opcode-mnemonic opcode)
(operand-format operand-1)
(operand-format operand-2)))
 
(defun current-non-basic-instruction (opcode operand-1)
(list (opcode-mnemonic opcode)
(operand-format operand-1)))
 
(defun load-program (words ram &optional (offset 0))
(dolist (word words)
(ram-write word offset ram)
(incf offset))
ram)
 
;;; Assembler
 
(defun make-labels-map ()
"Return an empty labels-map. A labels-map maps labels to
offsets."
'())
 
(defun extend-labels-map (label offset labels-map)
"Extend a LABELS-MAP with a LABEL-OFFSET mapping."
(acons label offset labels-map))
 
(defun lookup-label-offset (label labels-map)
"Map a LABEL to its offset. If no such mapping exists, return NIL."
(cdr (assoc label labels-map)))
 
(defun subst-offsets-for-labels (instructions labels-map)
"Replace the labels referenced in the list of INSTRUCTIONS with
their corresponding offsets in accordance with LABELS-MAP."
(sublis labels-map instructions))
 
(defun instruction-op (instruction)
"Return the instruction's operator."
(car instruction))
 
(defun instruction-a (instruction)
"Return the instruction's first operand."
(cadr instruction))
 
(defun instruction-b (instruction)
"Return the instruction's second operand."
(caddr instruction))
 
(defun assemble-instruction (instruction)
"Assemble a single instruction, returning the corresponding list of
words."
(if (basic-instruction-p instruction)
(assemble-basic-instruction instruction)
(assemble-non-basic-instruction instruction)))
 
(defun assemble-basic-instruction (instruction)
"Assemble a basic instruction, returning a list of words."
(append (list (basic-instruction-word instruction))
(instruction-operand-words (instruction-a instruction))
(instruction-operand-words (instruction-b instruction))))
 
(defun basic-instruction-word (instruction)
"Return the basic instruction's first word."
(logior (ash (basic-instruction-opcode-bits (instruction-op instruction)) 0)
(ash (instruction-operand-bits (instruction-a instruction)) 4)
(ash (instruction-operand-bits (instruction-b instruction)) 10)))
 
(defun assemble-non-basic-instruction (instruction)
"Assemble a non-basic instruction, returning a list of words."
(append (list (non-basic-instruction-word instruction))
(instruction-operand-words (instruction-a instruction))))
 
(defun non-basic-instruction-word (instruction)
"Return the non-basic instruction's first word."
(logior (ash 0 0)
(ash (non-basic-instruction-opcode-bits (instruction-op instruction)) 4)
(ash (instruction-operand-bits (instruction-a instruction)) 10)))
 
(defclass opcode ()
((mnemonic :initarg :mnemonic :reader opcode-mnemonic)
(bits :initarg :bits :reader opcode-bits)
(cycles-count :initarg :cycles-count :reader opcode-cycles-count)
(execute-function :initarg :execute-function :reader opcode-execute-function)))
 
(defclass basic-opcode (opcode)
())
 
(defclass non-basic-opcode (opcode)
())
 
(defclass opcode-index ()
((by-mnemonic :initarg :by-mnemonic :reader opcode-index-by-mnemonic)
(basic-by-bits :initarg :basic-by-bits :reader opcode-index-basic-by-bits)
(non-basic-by-bits :initarg :non-basic-by-bits :reader opcode-index-non-basic-by-bits)))
 
(defun make-opcode-index (opcodes)
(let ((by-mnemonic (make-hash-table))
(basic-by-bits (make-hash-table))
(non-basic-by-bits (make-hash-table)))
(dolist (opcode opcodes)
(setf (gethash (opcode-mnemonic opcode) by-mnemonic) opcode)
(setf (gethash (opcode-bits opcode)
(etypecase opcode
(basic-opcode basic-by-bits)
(non-basic-opcode non-basic-by-bits)))
opcode))
(make-instance 'opcode-index
:by-mnemonic by-mnemonic
:basic-by-bits basic-by-bits
:non-basic-by-bits non-basic-by-bits)))
 
(defvar *opcode-index*
(make-opcode-index
(list (make-instance 'basic-opcode :mnemonic 'set :bits #x01 :cycles-count 1 :execute-function 'execute-set)
(make-instance 'basic-opcode :mnemonic 'add :bits #x02 :cycles-count 2 :execute-function 'execute-add)
(make-instance 'basic-opcode :mnemonic 'sub :bits #x03 :cycles-count 2 :execute-function 'execute-sub)
(make-instance 'basic-opcode :mnemonic 'mul :bits #x04 :cycles-count 2 :execute-function 'execute-mul)
(make-instance 'basic-opcode :mnemonic 'div :bits #x05 :cycles-count 3 :execute-function 'execute-div)
(make-instance 'basic-opcode :mnemonic 'mod :bits #x06 :cycles-count 3 :execute-function 'execute-mod)
(make-instance 'basic-opcode :mnemonic 'shl :bits #x07 :cycles-count 2 :execute-function 'execute-shl)
(make-instance 'basic-opcode :mnemonic 'shr :bits #x08 :cycles-count 2 :execute-function 'execute-shr)
(make-instance 'basic-opcode :mnemonic 'and :bits #x09 :cycles-count 1 :execute-function 'execute-and)
(make-instance 'basic-opcode :mnemonic 'bor :bits #x0A :cycles-count 1 :execute-function 'execute-bor)
(make-instance 'basic-opcode :mnemonic 'xor :bits #x0B :cycles-count 1 :execute-function 'execute-xor)
(make-instance 'basic-opcode :mnemonic 'ife :bits #x0C :cycles-count 2 :execute-function 'execute-ife)
(make-instance 'basic-opcode :mnemonic 'ifn :bits #x0D :cycles-count 2 :execute-function 'execute-ifn)
(make-instance 'basic-opcode :mnemonic 'ifg :bits #x0E :cycles-count 2 :execute-function 'execute-ifg)
(make-instance 'basic-opcode :mnemonic 'ifb :bits #x0F :cycles-count 2 :execute-function 'execute-ifb)
(make-instance 'non-basic-opcode :mnemonic 'jsr :bits #x01 :cycles-count 2 :execute-function 'execute-jsr))))
 
(defun find-opcode-by-mnemonic (mnemonic)
(gethash mnemonic (opcode-index-by-mnemonic *opcode-index*)))
 
(defun find-basic-opcode-by-bits (bits)
(gethash bits (opcode-index-basic-by-bits *opcode-index*)))
 
(defun find-non-basic-opcode-by-bits (bits)
(gethash bits (opcode-index-non-basic-by-bits *opcode-index*)))
 
(defun basic-instruction-p (instruction)
"Return true if the instruction is a basic one, and false
otherwise."
(let ((opcode (find-opcode-by-mnemonic (instruction-op instruction))))
(typep opcode 'basic-opcode)))
 
(defun basic-instruction-opcode-bits (op)
"Return opcode bits for a basic instruction's operator."
(let ((opcode (find-opcode-by-mnemonic op)))
(check-type opcode basic-opcode)
(opcode-bits opcode)))
 
(defun non-basic-instruction-opcode-bits (op)
"Return the opcode bits for a non-basic instruction's operator."
(let ((opcode (find-opcode-by-mnemonic op)))
(check-type opcode non-basic-opcode)
(opcode-bits opcode)))
 
(defclass operand ()
((cycles-count :initarg :cycles-count :reader operand-cycles-count)
(bits :initarg :bits :reader operand-bits)))
 
(defgeneric operand-test-p (expr operand))
(defgeneric operand-words (expr operand))
(defgeneric operand-read-1 (operand word))
(defgeneric operand-write-1 (value operand word))
(defgeneric operand-length (operand))
(defgeneric operand-format-1 (operand word))
 
(defclass operand-no-words-mixin ()
())
 
(defmethod operand-words (expr (operand operand-no-words-mixin))
(declare (ignore expr))
'())
 
(defmethod operand-length ((operand operand-no-words-mixin))
0)
 
(defclass operand-symbol-mixin ()
((symbol :initarg :symbol :reader operand-symbol)))
 
(defmethod operand-test-p (expr (operand operand-symbol-mixin))
(eq expr (operand-symbol operand)))
 
(defmethod operand-format-1 ((operand operand-symbol-mixin) word)
(declare (ignore word))
(operand-symbol operand))
 
(defclass register-operand (operand-no-words-mixin operand-symbol-mixin operand)
()
(:default-initargs :cycles-count 0))
 
(defmethod operand-read-1 ((operand register-operand) word)
(declare (ignore word))
(reg-read (operand-symbol operand) *register-file*))
 
(defmethod operand-write-1 (value (operand register-operand) word)
(declare (ignore word))
(reg-write value (operand-symbol operand) *register-file*))
 
(defclass reg-ref-operand (operand-no-words-mixin operand)
((symbol :initarg :symbol :reader operand-symbol))
(:default-initargs :cycles-count 0))
 
(defmethod operand-test-p (expr (operand reg-ref-operand))
(and (consp expr)
(eq (car expr) (operand-symbol operand))))
 
(defmethod operand-read-1 ((operand reg-ref-operand) word)
(declare (ignore word))
(let ((offset (reg-read (operand-symbol operand) *register-file*)))
(ram-read offset *ram*)))
 
(defmethod operand-write-1 (value (operand reg-ref-operand) word)
(declare (ignore word))
(let ((offset (reg-read (operand-symbol operand) *register-file*)))
(ram-write value offset *ram*)))
 
(defmethod operand-format-1 ((operand reg-ref-operand) word)
(declare (ignore word))
(list (operand-symbol operand)))
 
(defclass add-reg-ref-operand (operand)
((symbol :initarg :symbol :reader operand-symbol))
(:default-initargs :cycles-count 1))
 
(defmethod operand-test-p (expr (operand add-reg-ref-operand))
(and (consp expr)
(eq (car expr) '+)
(or (eq (cadr expr) (operand-symbol operand))
(eq (caddr expr) (operand-symbol operand)))))
 
(defmethod operand-words (expr (operand add-reg-ref-operand))
(let ((a (cadr expr))
(b (caddr expr)))
(if (integerp a)
(list a)
(list b))))
 
(defun add-reg-ref-offset (operand word)
(let ((reg (reg-read (operand-symbol operand) *register-file*)))
(logand (+ reg word) #xFFFF)))
 
(defmethod operand-read-1 ((operand add-reg-ref-operand) word)
(ram-read (add-reg-ref-offset operand word) *ram*))
 
(defmethod operand-write-1 (value (operand add-reg-ref-operand) word)
(ram-write value (add-reg-ref-offset operand word) *ram*))
 
(defmethod operand-length ((operand add-reg-ref-operand))
1)
 
(defmethod operand-format-1 ((operand add-reg-ref-operand) word)
(list '+ (operand-symbol operand) word))
 
(defclass pop-operand (operand-no-words-mixin operand-symbol-mixin operand)
()
(:default-initargs :cycles-count 0))
 
(defmethod operand-read-1 ((operand pop-operand) word)
(declare (ignore word))
(let ((sp (get-sp)))
(set-sp (1+ sp))
(ram-read sp *ram*)))
 
(defmethod operand-write-1 (value (operand pop-operand) word)
(declare (ignore word))
(let ((sp (get-sp)))
(set-sp (1+ sp))
(ram-write value sp *ram*)))
 
(defclass peek-operand (operand-no-words-mixin operand-symbol-mixin operand)
()
(:default-initargs :cycles-count 0))
 
(defmethod operand-read-1 ((operand peek-operand) word)
(declare (ignore word))
(let ((sp (get-sp)))
(ram-read sp *ram*)))
 
(defmethod operand-write-1 (value (operand peek-operand) word)
(declare (ignore word))
(let ((sp (get-sp)))
(ram-write value sp *ram*)))
 
(defclass push-operand (operand-no-words-mixin operand-symbol-mixin operand)
()
(:default-initargs :cycles-count 0))
 
(defmethod operand-read-1 ((operand push-operand) word)
(declare (ignore word))
(let ((sp (get-sp)))
(set-sp (1- sp))
(ram-read (1- sp) *ram*)))
 
(defmethod operand-write-1 (value (operand push-operand) word)
(declare (ignore word))
(let ((sp (get-sp)))
(set-sp (1- sp))
(ram-write value (1- sp) *ram*)))
 
(defclass ref-operand (operand)
()
(:default-initargs :cycles-count 1))
 
(defmethod operand-test-p (expr (operand ref-operand))
(consp expr))
 
(defmethod operand-words (expr (operand ref-operand))
expr)
 
(defmethod operand-read-1 ((operand ref-operand) word)
(ram-read word *ram*))
 
(defmethod operand-write-1 (value (operand ref-operand) word)
(ram-write value word *ram*))
 
(defmethod operand-length ((operand ref-operand))
1)
 
(defmethod operand-format-1 ((operand ref-operand) word)
(list word))
 
(defclass literal-operand (operand)
()
(:default-initargs :cycles-count 1))
 
(defmethod operand-test-p (expr (operand literal-operand))
(integerp expr))
 
(defmethod operand-words (expr (operand literal-operand))
(list expr))
 
(defmethod operand-read-1 ((operand literal-operand) word)
word)
 
(defmethod operand-write-1 (value (operand literal-operand) word)
(declare (ignore value word)))
 
(defmethod operand-length ((operand literal-operand))
1)
 
(defmethod operand-format-1 ((operand literal-operand) word)
word)
 
;;; Small literal operands are no good at the moment because they
;;; confuse labels mapping.
 
(defclass small-literal-operand (operand-no-words-mixin operand)
()
(:default-initargs :cycles-count 0))
 
(defmethod operand-test-p (expr (operand small-literal-operand))
(and (integerp expr)
(= expr (- (operand-bits operand) #x20))))
 
(defmethod operand-read-1 ((operand small-literal-operand) word)
(declare (ignore word))
(- (operand-bits operand) #x20))
 
(defmethod operand-write-1 (value (operand small-literal-operand) word)
(declare (ignore value word)))
 
(defmethod operand-format-1 ((operand small-literal-operand) word)
(declare (ignore word))
(- (operand-bits operand) #x20))
 
(defclass operand-index ()
((list :initarg :list :reader operand-index-list)
(by-bits :initarg :by-bits :reader operand-index-by-bits)))
 
(defun make-operand-index (operands)
(let ((by-bits (make-hash-table)))
(dolist (operand operands)
(setf (gethash (operand-bits operand) by-bits) operand))
(make-instance 'operand-index
:list operands
:by-bits by-bits)))
(defvar *operand-index*
(make-operand-index
(list (make-instance 'register-operand :symbol 'a :bits #x00)
(make-instance 'register-operand :symbol 'b :bits #x01)
(make-instance 'register-operand :symbol 'c :bits #x02)
(make-instance 'register-operand :symbol 'x :bits #x03)
(make-instance 'register-operand :symbol 'y :bits #x04)
(make-instance 'register-operand :symbol 'z :bits #x05)
(make-instance 'register-operand :symbol 'i :bits #x06)
(make-instance 'register-operand :symbol 'j :bits #x07)
(make-instance 'reg-ref-operand :symbol 'a :bits #x08)
(make-instance 'reg-ref-operand :symbol 'b :bits #x09)
(make-instance 'reg-ref-operand :symbol 'c :bits #x0A)
(make-instance 'reg-ref-operand :symbol 'x :bits #x0B)
(make-instance 'reg-ref-operand :symbol 'y :bits #x0C)
(make-instance 'reg-ref-operand :symbol 'z :bits #x0D)
(make-instance 'reg-ref-operand :symbol 'i :bits #x0E)
(make-instance 'reg-ref-operand :symbol 'j :bits #x0F)
(make-instance 'add-reg-ref-operand :symbol 'a :bits #x10)
(make-instance 'add-reg-ref-operand :symbol 'b :bits #x11)
(make-instance 'add-reg-ref-operand :symbol 'c :bits #x12)
(make-instance 'add-reg-ref-operand :symbol 'x :bits #x13)
(make-instance 'add-reg-ref-operand :symbol 'y :bits #x14)
(make-instance 'add-reg-ref-operand :symbol 'z :bits #x15)
(make-instance 'add-reg-ref-operand :symbol 'i :bits #x16)
(make-instance 'add-reg-ref-operand :symbol 'j :bits #x17)
(make-instance 'pop-operand :symbol 'pop :bits #x18)
(make-instance 'peek-operand :symbol 'peek :bits #x19)
(make-instance 'push-operand :symbol 'push :bits #x1A)
(make-instance 'register-operand :symbol 'sp :bits #x1B)
(make-instance 'register-operand :symbol 'pc :bits #x1C)
(make-instance 'register-operand :symbol 'o :bits #x1D)
(make-instance 'ref-operand :bits #x1E)
(make-instance 'literal-operand :bits #x1F))))
 
(defun find-operand-for-expression (expr)
(let ((list (operand-index-list *operand-index*)))
(or (find-if (lambda (operand) (operand-test-p expr operand)) list)
(find-if (lambda (operand) (typep operand 'literal-operand)) list))))
 
(defun find-operand-by-bits (bits)
(or (gethash bits (operand-index-by-bits *operand-index*))
(error "Can't find operand with bits ~S." bits)))
 
(defun instruction-operand-words (arg)
"Return the list of additional words for an operand."
(operand-words arg (find-operand-for-expression arg)))
 
(defun instruction-operand-bits (arg)
"Return the bits representing an operand."
(operand-bits (find-operand-for-expression arg)))
 
(defun instruction-operand-length (arg)
"Return the length of an instruction's operand."
(operand-length (find-operand-for-expression arg)))
 
(defun instruction-length (instruction)
"Return the length of an instruction, in words."
(if (basic-instruction-p instruction)
(+ (instruction-operand-length (instruction-a instruction))
(instruction-operand-length (instruction-b instruction))
1)
(+ (instruction-operand-length (instruction-a instruction))
1)))
 
(defun assemble-program (program &optional (base-offset 0))
"Assemble a sequence of instructions and labels, returning the
corresponding list of words."
(mappend #'assemble-instruction (program-instructions program base-offset)))
 
(defun program-instructions (program base-offset)
(subst-offsets-for-labels (remove-labels program)
(build-labels-map program base-offset)))
 
(defun mappend (function list)
(if (null list)
'()
(append (funcall function (car list))
(mappend function (cdr list)))))
 
(defun build-labels-map (program base-offset)
"Build up a labels-map for the supplied PROGRAM, with starting
offset of BASE-OFFSET."
(labels ((iter (subprogram labels-map offset)
(cond ((null subprogram) labels-map)
((symbolp (car subprogram))
(iter (cdr subprogram)
(extend-labels-map (car subprogram) offset labels-map)
offset))
(t
(iter (cdr subprogram)
labels-map
(+ offset (instruction-length (car subprogram))))))))
(iter program (make-labels-map) base-offset)))
 
(defun remove-labels (program)
"Remove labels from a program, returning a list of instructions."
(remove-if #'symbolp program))
 
;;;; Test vector
 
(defvar *program*
'((set a #x30)
(set (#x1000) #x20)
(sub a (#x1000))
(ifn a #x10)
(set pc crash)
(set i 10)
(set a #x2000)
loop
(set (+ #x2000 i) (a))
(sub i 1)
(ifn i 0)
(set pc loop)
(set x #x04)
(jsr test-sub)
(set pc crash)
test-sub
(shl x 4)
(set pc pop)
crash
(set pc crash)))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.