changeset 100:2090bcb78f44

created functions to get and set register values
author Robert McIntyre <rlm@mit.edu>
date Mon, 12 Mar 2012 12:37:04 -0500
parents 9fad96094950
children 37711ff4a7bc
files clojure/com/aurellem/gb_driver.clj clojure/com/aurellem/items.clj java/src/com/aurellem/gb/Gb.java src/clojure/clojure.cpp src/gb/GB.cpp src/gb/GB.h
diffstat 6 files changed, 132 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/gb_driver.clj	Mon Mar 12 09:59:26 2012 -0500
     1.2 +++ b/clojure/com/aurellem/gb_driver.clj	Mon Mar 12 12:37:04 2012 -0500
     1.3 @@ -114,6 +114,13 @@
     1.4       (step state [:listen]))
     1.5    ([] (step (if @current-state @current-state (root)))))
     1.6  
     1.7 +(defn tick
     1.8 +  ([] (tick @current-state))
     1.9 +  ([state]
    1.10 +     (set-state! state)
    1.11 +     (Gb/tick)
    1.12 +     (update-state)))
    1.13 +
    1.14  (defn move
    1.15    [^Move move buttons]
    1.16    (Move. (step (:state move) buttons) buttons))
    1.17 @@ -142,6 +149,12 @@
    1.18        ([state]
    1.19           (set-state! state) (arr-fn store) store))))
    1.20  
    1.21 +(defn write-cpu-data [size store-fn]
    1.22 +  (fn [new-data]
    1.23 +    (let [store (int-array new-data)]
    1.24 +      (assert (= size (count new-data)))
    1.25 +      (store-fn store))))
    1.26 +
    1.27  (def memory
    1.28    (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))
    1.29  
    1.30 @@ -160,26 +173,56 @@
    1.31  (def registers
    1.32    (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
    1.33  
    1.34 +(def write-memory!
    1.35 +  (write-cpu-data Gb/GB_MEMORY #(Gb/writeMemory %)))
    1.36  
    1.37 -(defn write-memory! [new-memory]
    1.38 -  (let [store (int-array new-memory)]
    1.39 -    (assert (= Gb/GB_MEMORY) (count new-memory))
    1.40 -    (Gb/writeMemory new-memory)))
    1.41 +(def write-registers!
    1.42 +  (write-cpu-data Gb/NUM_REGISTERS #(Gb/writeRegisters %)))
    1.43 +
    1.44 +;; (defn write-memory! [new-memory]
    1.45 +;;   (let [store (int-array new-memory)]
    1.46 +;;     (assert (= Gb/GB_MEMORY) (count new-memory))
    1.47 +;;     (Gb/writeMemory new-memory)))
    1.48      
    1.49  
    1.50  ;; TODO add register names
    1.51  
    1.52  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    1.53  
    1.54 -(defn AF [state]
    1.55 -  (nth (registers state) 2))
    1.56 +(defmacro gen-get-set-register [name index]
    1.57 +  (let [name-bang (symbol (str name "!"))]
    1.58 +    `(do
    1.59 +       (defn ~name
    1.60 +         ~(str "Retrieve the " name " register from state, or "
    1.61 +               "from @current-state if state is absent.")
    1.62 +         ([state#]
    1.63 +            (nth (registers state#) ~index))
    1.64 +         ([]
    1.65 +            (~name @current-state)))
    1.66 +       (defn ~name-bang
    1.67 +         ~(str "Set the " name " register for state, or "
    1.68 +               "for @current-state if state is absent.")
    1.69 +         ([state# new-register#]
    1.70 +            (set-state! state#)
    1.71 +            (let [registers# (registers state#)]
    1.72 +              (aset registers# ~index new-register#)
    1.73 +              (Gb/writeRegisters registers#)
    1.74 +              (update-state)))
    1.75 +         ([new-register#]
    1.76 +            (~name-bang @current-state new-register#))))))
    1.77 +  
    1.78  
    1.79 -(defn BC [state]
    1.80 -  (nth (registers state) 3))
    1.81  
    1.82 -(defn DE [state]
    1.83 -  (nth (registers state) 4))
    1.84 -            
    1.85 +(gen-get-set-register PC 0)
    1.86 +(gen-get-set-register SP 1)
    1.87 +(gen-get-set-register AF 2)
    1.88 +(gen-get-set-register BC 3)
    1.89 +(gen-get-set-register DE 4)
    1.90 +(gen-get-set-register HL 5)
    1.91 +(gen-get-set-register IFF 6)
    1.92 +
    1.93 +
    1.94 +
    1.95  ;;;;;;;;;;;;;;;
    1.96  
    1.97  (defmacro defn-memo
     2.1 --- a/clojure/com/aurellem/items.clj	Mon Mar 12 09:59:26 2012 -0500
     2.2 +++ b/clojure/com/aurellem/items.clj	Mon Mar 12 12:37:04 2012 -0500
     2.3 @@ -12,7 +12,7 @@
     2.4    (subvec
     2.5     (vec (memory state))
     2.6     item-list-start
     2.7 -   (+ item-list-start 60)))
     2.8 +   (+ item-list-start 150)))
     2.9  
    2.10  (def item-hack-3 (read-state 77557))
    2.11  
     3.1 --- a/java/src/com/aurellem/gb/Gb.java	Mon Mar 12 09:59:26 2012 -0500
     3.2 +++ b/java/src/com/aurellem/gb/Gb.java	Mon Mar 12 12:37:04 2012 -0500
     3.3 @@ -98,7 +98,7 @@
     3.4      
     3.5      public static final int ROM_SIZE = 0x100000;
     3.6  
     3.7 -    public static final int NUM_REGISTERS = 27;
     3.8 +    public static final int NUM_REGISTERS = 28;
     3.9  
    3.10      public static final int GB_MEMORY = 0x10000;
    3.11  
    3.12 @@ -115,4 +115,7 @@
    3.13      public static native void getVRAM(int[] store);
    3.14      
    3.15      public static native void getRegisters(int[] store);
    3.16 +    
    3.17 +    public static native void writeRegisters(int[] newRegisters);
    3.18 +
    3.19  }
     4.1 --- a/src/clojure/clojure.cpp	Mon Mar 12 09:59:26 2012 -0500
     4.2 +++ b/src/clojure/clojure.cpp	Mon Mar 12 12:37:04 2012 -0500
     4.3 @@ -221,3 +221,15 @@
     4.4  }
     4.5  
     4.6  
     4.7 +/*
     4.8 + * Class:     com_aurellem_gb_Gb
     4.9 + * Method:    writeRegisters
    4.10 + * Signature: ([I)V
    4.11 + */
    4.12 +JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_writeRegisters
    4.13 +(JNIEnv *env, jclass clazz, jintArray arr){
    4.14 +  jint *new_registers = env->GetIntArrayElements(arr, 0);
    4.15 +  setRegisters(new_registers);
    4.16 +  env->ReleaseIntArrayElements(arr, new_registers, 0);
    4.17 +}
    4.18 +
     5.1 --- a/src/gb/GB.cpp	Mon Mar 12 09:59:26 2012 -0500
     5.2 +++ b/src/gb/GB.cpp	Mon Mar 12 12:37:04 2012 -0500
     5.3 @@ -3934,6 +3934,7 @@
     5.4    }
     5.5  }
     5.6  
     5.7 +
     5.8  void storeRegisters(int32* store){
     5.9    store[0] = (int32) PC.W;
    5.10    store[1] = (int32) SP.W;
    5.11 @@ -3941,28 +3942,64 @@
    5.12    store[3] = (int32) BC.W;
    5.13    store[4] = (int32) DE.W;
    5.14    store[5] = (int32) HL.W;
    5.15 -  store[6] = (int32) register_DIV;
    5.16 -  store[7] = (int32) register_TIMA;
    5.17 -  store[8] = (int32) register_TMA;
    5.18 -  store[9] = (int32) register_TAC;
    5.19 -  store[10] = (int32) register_IF;
    5.20 -  store[11] = (int32) register_LCDC;
    5.21 -  store[12] = (int32) register_STAT;
    5.22 -  store[13] = (int32) register_SCY;
    5.23 -  store[14] = (int32) register_SCX;
    5.24 -  store[15] = (int32) register_LY;
    5.25 -  store[16] = (int32) register_LYC;
    5.26 -  store[17] = (int32) register_DMA;
    5.27 -  store[18] = (int32) register_WY;
    5.28 -  store[19] = (int32) register_WX;
    5.29 -  store[20] = (int32) register_VBK;
    5.30 -  store[21] = (int32) register_HDMA1;
    5.31 -  store[22] = (int32) register_HDMA2;
    5.32 -  store[23] = (int32) register_HDMA3;
    5.33 -  store[24] = (int32) register_HDMA4;
    5.34 -  store[25] = (int32) register_HDMA5;
    5.35 -  store[26] = (int32) register_SVBK;
    5.36 -  store[27] = (int32) register_IE;
    5.37 +
    5.38 +  store[6] = (int32) IFF;
    5.39 +
    5.40 +  store[7] = (int32) register_DIV;
    5.41 +  store[8] = (int32) register_TIMA;
    5.42 +  store[9] = (int32) register_TMA;
    5.43 +  store[10] = (int32) register_TAC;
    5.44 +  store[11] = (int32) register_IF;
    5.45 +  store[12] = (int32) register_LCDC;
    5.46 +  store[13] = (int32) register_STAT;
    5.47 +  store[14] = (int32) register_SCY;
    5.48 +  store[15] = (int32) register_SCX;
    5.49 +  store[16] = (int32) register_LY;
    5.50 +  store[17] = (int32) register_LYC;
    5.51 +  store[18] = (int32) register_DMA;
    5.52 +  store[19] = (int32) register_WY;
    5.53 +  store[20] = (int32) register_WX;
    5.54 +  store[21] = (int32) register_VBK;
    5.55 +  store[22] = (int32) register_HDMA1;
    5.56 +  store[23] = (int32) register_HDMA2;
    5.57 +  store[24] = (int32) register_HDMA3;
    5.58 +  store[25] = (int32) register_HDMA4;
    5.59 +  store[26] = (int32) register_HDMA5;
    5.60 +  store[27] = (int32) register_SVBK;
    5.61 +  store[28] = (int32) register_IE;
    5.62 +}
    5.63 +
    5.64 +void setRegisters(int32* registers){
    5.65 +  PC.W = (u16) (0xFFFF & registers[0]);
    5.66 +  SP.W = (u16) (0xFFFF & registers[1]);
    5.67 +  AF.W = (u16) (0xFFFF & registers[2]);
    5.68 +  BC.W = (u16) (0xFFFF & registers[3]);
    5.69 +  DE.W = (u16) (0xFFFF & registers[4]);
    5.70 +  HL.W = (u16) (0xFFFF & registers[5]);
    5.71 +  IFF  = (u16) (0xFFFF & registers[6]);
    5.72 +
    5.73 +  register_DIV = (u8) (0xFF & registers[7]);
    5.74 +  register_TIMA = (u8) (0xFF & registers[8]);
    5.75 +  register_TMA = (u8) (0xFF & registers[9]);
    5.76 +  register_TAC = (u8) (0xFF & registers[10]);
    5.77 +  register_IF = (u8) (0xFF & registers[11]);
    5.78 +  register_LCDC = (u8) (0xFF & registers[12]);
    5.79 +  register_STAT = (u8) (0xFF & registers[13]);
    5.80 +  register_SCY = (u8) (0xFF & registers[14]);
    5.81 +  register_SCX = (u8) (0xFF & registers[15]);
    5.82 +  register_LY = (u8) (0xFF & registers[16]);
    5.83 +  register_LYC = (u8) (0xFF & registers[17]);
    5.84 +  register_DMA = (u8) (0xFF & registers[18]);
    5.85 +  register_WY = (u8) (0xFF & registers[19]);
    5.86 +  register_WX = (u8) (0xFF & registers[20]);
    5.87 +  register_VBK = (u8) (0xFF & registers[21]);
    5.88 +  register_HDMA1 = (u8) (0xFF & registers[22]);
    5.89 +  register_HDMA2 = (u8) (0xFF & registers[23]);
    5.90 +  register_HDMA3 = (u8) (0xFF & registers[24]);
    5.91 +  register_HDMA4 = (u8) (0xFF & registers[25]);
    5.92 +  register_HDMA5 = (u8) (0xFF & registers[26]);
    5.93 +  register_SVBK = (u8) (0xFF & registers[27]);
    5.94 +  register_IE = (u8) (0xFF & registers[28]);
    5.95  }
    5.96  
    5.97  struct EmulatedSystem GBSystem =
     6.1 --- a/src/gb/GB.h	Mon Mar 12 09:59:26 2012 -0500
     6.2 +++ b/src/gb/GB.h	Mon Mar 12 12:37:04 2012 -0500
     6.3 @@ -55,6 +55,8 @@
     6.4  extern void storeWRam(int32 *);
     6.5  extern void storeVRam(int32 *);
     6.6  extern void storeRegisters(int32 *);
     6.7 +extern void setRegisters(int32 *);
     6.8 +
     6.9  extern long gbWriteMemSaveStatePos(char *, int);
    6.10  
    6.11  extern struct EmulatedSystem GBSystem;