diff --git a/README.md b/README.md index a8bc1e0..eaf7298 100644 --- a/README.md +++ b/README.md @@ -5,17 +5,20 @@ very early days for now. in the racket repl (comments for reader comprehension, remove before running): ``` -(run-vm #x150 #x150 #x16 ; start-address, end-memory-view-addr, number-bytes-view +(run-vm #x150 #x150 #x19 ; start-address, end-memory-view-addr, number-bytes-view '(#x3E #x69 ; LD A, $69 #x26 #x01 ; LD H, $01 - #x2E #x62 ; LD L, $62 + #x2E #x67 ; LD L, $67 #x77 ; LD (HL), A #x2E #x5B ; LD L, $5B #x36 #xE6 ; LD (HL), $E6 ($E6 is the opcode for XOR $xx) #x10 ; STOP (this instruction @ address $015B) #xF0 ; (non-instruction, will be arg for previous byte) - #x2E #x63 ; LD L, $63 + #x2E #x68 ; LD L, $68 #x77 ; LD (HL), A + #xC3 #x64 #x01 ; JP $0164 + #x10 ; STOP + #x7D ; LD A, L (address $0164) #x10 ; STOP )) ``` @@ -23,18 +26,19 @@ in the racket repl (comments for reader comprehension, remove before running): will evaluate to: ``` -PC: $0161, SP: $0000, Flags: %00000000 +PC: $0166, SP: $0000, Flags: %00000000 BC: $0000, DE: $0000 -HL: $0163, AF: $6000 +HL: $0168, AF: $6800 (HL): $60 -$0150 > $3e $69 $26 $01 $2e $62 $77 $2e $5b $36 $e6 $e6 $f0 $2e $63 $77 $10 $00 $69 $60 $00 $00 < $0165 +$0150 > $3e $69 $26 $01 $2e $67 $77 $2e $5b $36 $e6 $e6 $f0 $2e $68 $77 $c3 $64 $01 $10 $7d $10 $00 $69 $60 < $0168 ``` -here we can see the program loaded starting from address `$0150`, including the `XOR` instruction loaded to `$015B` during execution, and the `$69` we loaded to memory, along with the result of `XOR $F0` we loaded to memory, at `$0162` and `$0163` respectively. +here we can see the program loaded starting from address `$0150`, including the `XOR` instruction loaded to `$015B` during execution, and the `$69` we loaded to memory, along with the result of `XOR $F0` we loaded to memory, at `$0167` and `$0168` respectively. we can also see that the `A` register is `$68`, the same as the `L` register, as the `JP` instruction executed properly and we skipped the `STOP` at address `$0163`. ## notes currently supported instructions are: + * `JP #addr` * `LD [reg], #imm` * `LD [reg], [reg]` * `XOR #imm` diff --git a/vm.rkt b/vm.rkt index cf88bc0..507b1c3 100644 --- a/vm.rkt +++ b/vm.rkt @@ -103,6 +103,14 @@ (cons (8b-ld-reg-imm y x) (set-pc (add1pc pc) m)))) +(define (make-jp cc m) + (letrec ([pc (mem-pc m)] + [y (get-byte pc m)] + [npc (add1pc pc)] + [x (get-byte npc m)] + [addr (make-16b-addr x y)]) + (cons (jp cc addr) (set-pc npc m)))) + (define (make-8b-ld-reg-reg y z m) (cons (8b-ld-reg-reg y z) m)) @@ -123,6 +131,11 @@ (define (make-stop m) (cons 'STOP m)) +(define (jp cc addr) + (case cc + [(uncond) (jp-uncond addr)] + [else (nop)])) + (define (8b-ld-reg-imm reg imm) (lambda (m) (set-reg reg imm m))) @@ -133,6 +146,10 @@ (get-reg src m) m))) +(define (jp-uncond addr) + (lambda (m) + (set-pc addr m))) + (define (8b-xor-imm imm) (lambda (m) (let ([a (bitwise-xor (get-reg 7 m) @@ -225,6 +242,7 @@ (cond [(= #x00 op) (make-nop (set-pc npc m))] [(= #x10 op) (make-stop (set-pc npc m))] + [(= #xC3 op) (make-jp 'uncond (set-pc npc m))] [(= 1 x) (make-8b-ld-reg-reg y z (set-pc npc m))] [(and (= 3 x) (within z 4 6))