8

我是球拍/方案的新手,所以我决定通过为DCPU-16(一个简单的 16 位处理器)实现一个模拟器来学习。

因此,我的问题是:实施我的解决方案的更好方法是什么?

这是我一起破解来控制 CPU 寄存器的解决方案。要点是允许将修改寄存器的函数链接在一起。例如:

; Increment value stored in register r-id
; returns the updated register
;
; Reg - the register structure 
; (reg-inc Reg 'SP)
(define (reg-inc reg r-id)
    (reg-write reg r-id (+ (reg-read reg r-id) 1 )))

; chain them together
;(reg-inc (reg-inc Reg 'SP)
;         'PC)
;
; returns structure with both 'SP and 'PC incremented

我的注册解决方案的全文如下。我的完整程序也在 github 上。有这么多重复的逻辑,我知道一定有一个更简单的方法:

(struct registers (A B C X Y Z I J SP PC O Pa Pb Paadr Pbadr CLK)
  #:transparent)

(define Reg (registers 0 0 0 0 0 0 0 0 #x10000 0 0 0 0 0 0 0))

(define (reg-name n)
  (case n
    [(0) 'A]
    [(1) 'B]
    [(2) 'C]
    [(3) 'X]
    [(4) 'Y]
    [(5) 'Z]
    [(6) 'I]
    [(7) 'J]
    [(8) 'SP]
    [(9) 'PC]
    [(10) 'O]
    [(11) 'Pa]
    [(12) 'Pb]
    [(13) 'Paadr]
    [(14) 'Pbadr]
    [(15) 'CLK]
    [else (error "Invalid register")]))

(define (reg-id s)
  (cond
    [(eq? 'A s) 0]
    [(eq? 'B s) 1]
    [(eq? 'C s) 2]
    [(eq? 'X s) 3]
    [(eq? 'Y s) 4]
    [(eq? 'Z s) 5]
    [(eq? 'I s) 6]
    [(eq? 'J s) 7]
    [(eq? 'SP s) 8]
    [(eq? 'PC s) 9]
    [(eq? 'O s) 10]
    [(eq? 'Pa s) 11]
    [(eq? 'Pb s) 12]
    [(eq? 'Paadr s) 13]
    [(eq? 'Pbadr s) 14]
    [(eq? 'CLK s) 15]))

(define (reg-read reg r)
  (if (symbol? r)
      (reg-read reg (reg-id r))
      (case r
        [(0) (registers-A reg)]
        [(1) (registers-B reg)]
        [(2) (registers-C reg)]
        [(3) (registers-X reg)]
        [(4) (registers-Y reg)]
        [(5) (registers-Z reg)]
        [(6) (registers-I reg)]
        [(7) (registers-J reg)]
        [(8) (registers-SP reg)]
        [(9) (registers-PC reg)]
        [(10) (registers-O reg)]
        [(11) (registers-Pa reg)]
        [(12) (registers-Pb reg)]
        [(13) (registers-Paadr reg)]
        [(14) (registers-Pbadr reg)]
        [(15) (registers-CLK reg)]
        [else (error "Invalid register")])))

(define (reg-write reg r val)
  (if (symbol? r)
      (reg-write reg (reg-id r) val)
      (let ([mask-val (bitwise-and val #xffff)])
        (case r
          [(0) (struct-copy registers reg [A mask-val])]
          [(1) (struct-copy registers reg [B mask-val])]
          [(2) (struct-copy registers reg [C mask-val])]
          [(3) (struct-copy registers reg [X mask-val])]
          [(4) (struct-copy registers reg [Y mask-val])]
          [(5) (struct-copy registers reg [Z mask-val])]
          [(6) (struct-copy registers reg [I mask-val])]
          [(7) (struct-copy registers reg [J mask-val])]
          [(8) (struct-copy registers reg [SP mask-val])]
          [(9) (struct-copy registers reg [PC mask-val])]
          [(10) (struct-copy registers reg [O mask-val])]
          [(11) (struct-copy registers reg [Pa mask-val])]
          [(12) (struct-copy registers reg [Pb mask-val])]
          [(13) (struct-copy registers reg [Paadr mask-val])]
          [(14) (struct-copy registers reg [Pbadr mask-val])]
          [(15) (struct-copy registers reg [CLK mask-val])]
          [else (error "Invalid register")]))))

更新:

感谢 oobviat 的建议,我使用列表进行了重构。唯一棘手的部分是更新列表中的值。我为 map 编写了一个程序,它将更新所需的寄存器并让其他寄存器保持原始值:

;; a-list of registers and initial values
(define (build-reg)
  '((A . 0)  (B . 0)     (C . 0)      (X . 0)
    (Y . 0)  (Z . 0)     (I . 0)      (J . 0)
    (SP . 0) (PC . 0)    (O . 0)      (Pa . 0)
    (Pb . 0) (Paadr . 0) (Pbadr . 0)  (CLK . 0)))

(define *REF-REG* (build-reg)) ; used to determine structure

(define (reg-name n)
  (if (symbol? n)
      n
      (car (list-ref *REF-REG* n))))

(define (reg-id s)
  (- (length *REF-REG*)
     (length (memf (lambda (arg)
                     (eq? s (car arg)))
                   *REF-REG*))))

(define (reg-write reg r val)
  (let ([r-name (reg-name r)])
    (define (reg-write-helper entry)
      (if (eq? r-name
               (car entry))
          (cons r-name val)
          entry))
    (map reg-write-helper reg)))

(define (reg-read reg r)
  (cdr (assoc (reg-name r) reg)))
4

1 回答 1

2

这不是用 Racket 编写的,因此它可能无法按原样运行。如果它抛出错误,请尝试在文件顶部指定 R5RS 代码类型。为简单起见,我会使用 a-list 而不是结构来做这样的事情。

;; a-list of registers and initial values
(define *reg*
  '((A . 0) (B . 0) (C . 0) (X . 0) (Y . 0) (Z . 0)
    (I . 0) (J . 0) (SP . #X10000) (PC . 0) (O . 0)
    (Pa . 0) (Pb . 0) (Paadr . 0) (Pbadr . 0) (CLK . 0)))

(define (reg-write register val)
  (set-cdr! (assoc register *reg*) val) ;write new value to register
  val) ; return newly written value

(define (reg-read register)
  (cdr (assoc register *reg*)))

(define (reg-inc register)
  (reg-write register (+ 1 (reg-read register))))

;; to do many operations
;; input:  a list of registers
;;    EX:  '(a b x)
(define (do-incs registers)
  (if (null? registers)
      'done       ; return something when the incs are done
      (begin      ; lets you evaluate multiple expressions since `if` doesn't          
        (reg-inc (car registers))
        (do-incs (cdr registers)))))

我假设 Racket 有一个类似的内置函数,可以assoc从 a-list 中返回正确的列表。另外,请注意,*reg*在这种情况下,它被定义为全局变量,这样我们就可以定义一次,然后使用它set-cdr!来向其写入值。

最后,这可能会对您的SP寄存器产生奇怪的影响。我的方案将其视为 65536.. 如果这不正确,您可能必须添加一个iftoreg-writereg-read确保您在那里获得正确的值。

<EDIT> 因此,我阅读了一些关于 Racket 程序的内容,并且这段代码几乎肯定不会在正常的 Racket 中运行,因为它们显然同时具有可变和非可变对。如果您想在 Racket 而不是 R5RS 下运行它,您必须进行的更改如下:

您可能需要使用可变列表/对构造函数来制作寄存器列表,而不仅仅是使用带引号的列表(define *reg* (mlist (mcons 'A 0) (mcons 'B 0) ... )

而不是使用set-cdr!Racket 版本,set-mcdr!并且仅适用于可变对。</EDIT>

于 2012-04-06T16:11:19.487 回答