Changeset 147


Ignore:
Timestamp:
Sep 30, 2010, 6:46:44 PM (9 years ago)
Author:
mulligan
Message:

Changes made today: we now have an emulator that is able to emulate two
small C programs.

Location:
Deliverables/D4.1
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • Deliverables/D4.1/ASMInterpret.ml

    r145 r147  
    387387       let pc,b1 = next pc in
    388388       let pc,b2 = next pc in
    389          `MOV (`U3 (`DIRECT b1, `DATA b2)), pc, 2
     389         `MOV (`U3 (`DIRECT b1, `DATA b2)), pc, 3
    390390   | (true,true,true,true),(false,true,true,i1) ->
    391391         `MOV (`U2 (`INDIRECT i1, `A)), pc, 1
     
    992992   | `INC ((`A | `REG _ | `DIRECT _ | `INDIRECT _) as d) ->
    993993       let b = get_arg_8 status d in
    994        let cry, res = half_add b (vect_of_int 0 `Eight) in
     994       let cry, res = half_add b (vect_of_int 1 `Eight) in
    995995         set_arg_8 status res d
    996996   | `DEC d ->
     
    11321132  | `JC (`REL rel) ->
    11331133       if get_cy_flag status then
    1134          let cry, new_pc = half_add status.pc (mk_word (zero `Eight) rel) in
     1134         let cry, new_pc = half_add status.pc (sign_extension rel) in
    11351135           { status with pc = new_pc }
    11361136       else
     
    11381138  | `JNC (`REL rel) ->
    11391139       if not $ get_cy_flag status then
    1140          let cry, new_pc = half_add status.pc (mk_word (zero `Eight) rel) in
     1140         let cry, new_pc = half_add status.pc (sign_extension rel) in
    11411141           { status with pc = new_pc }
    11421142       else
     
    11441144  | `JB (b, (`REL rel)) ->
    11451145       if get_arg_1 status b then
    1146          let cry, new_pc = half_add status.pc (mk_word (zero `Eight) rel) in
     1146         let cry, new_pc = half_add status.pc (sign_extension rel) in
    11471147           { status with pc = new_pc }
    11481148       else
     
    11501150  | `JNB (b, (`REL rel)) ->
    11511151       if not $ get_arg_1 status b then
    1152          let cry, new_pc = half_add status.pc (mk_word (zero `Eight) rel) in
     1152         let cry, new_pc = half_add status.pc (sign_extension rel) in
    11531153           { status with pc = new_pc }
    11541154       else
     
    11571157       let status = set_arg_1 status false b in
    11581158         if get_arg_1 status b then
    1159            let cry, new_pc = half_add status.pc (mk_word (zero `Eight) rel) in
     1159           let cry, new_pc = half_add status.pc (sign_extension rel) in
    11601160             { status with pc = new_pc }
    11611161         else
     
    11791179         { status with pc = mk_word high_bits low_bits }
    11801180  | `ACALL (`ADDR11 a) ->
    1181        let cry, new_pc = half_add status.pc (vect_of_int 2 `Sixteen) in
    1182        let status = { status with pc = new_pc } in
    11831181       let cry, new_sp = half_add status.sp (vect_of_int 1 `Eight) in
    11841182       let status = { status with sp = new_sp } in
     
    11941192         { status with pc = addr }
    11951193  | `LCALL (`ADDR16 addr) ->
    1196        let cry, new_pc = half_add status.pc (vect_of_int 3 `Sixteen) in
    1197        let status = { status with pc = new_pc } in
    11981194       let cry, new_sp = half_add status.sp (vect_of_int 1 `Eight) in
    11991195       let status = { status with sp = new_sp } in
     
    12051201         { status with pc = addr }
    12061202  | `AJMP (`ADDR11 a) ->
    1207        let cry, new_pc = half_add status.pc (vect_of_int 2 `Sixteen) in
    1208        let status = { status with pc = new_pc } in
    12091203       let pc_upper_byte, pc_lower_byte = from_word status.pc in
    12101204       let n1, n2 = from_byte pc_upper_byte in
     
    12171211       { status with pc = a }
    12181212  | `SJMP (`REL rel) ->
    1219        let cry, new_pc = half_add status.pc (mk_word (zero `Eight) rel) in
     1213       let cry, new_pc = half_add status.pc (sign_extension rel) in
    12201214         { status with pc = new_pc }
    12211215  | `JMP `IND_DPTR ->
     
    12271221  | `JZ (`REL rel) ->
    12281222       if status.acc = zero `Eight then
    1229          let cry, new_pc = half_add status.pc (mk_word (zero `Eight) rel) in
     1223         let cry, new_pc = half_add status.pc (sign_extension rel) in
    12301224           { status with pc = new_pc }
    12311225       else
     
    12331227  | `JNZ (`REL rel) ->
    12341228       if status.acc <> zero `Eight then
    1235          let cry, new_pc = half_add status.pc (mk_word (zero `Eight) rel) in
     1229         let cry, new_pc = half_add status.pc (sign_extension rel) in
    12361230                           { status with pc = new_pc }
    12371231       else
     
    12401234       let new_carry = status.acc < get_arg_8 status ag in
    12411235         if get_arg_8 status ag <> status.acc then
    1242            let cry, new_pc = half_add status.pc (mk_word (zero `Eight) rel) in
     1236           let cry, new_pc = half_add status.pc (sign_extension rel) in
    12431237           let status = set_flags status new_carry None (get_ov_flag status) in
    12441238             { status with pc = new_pc;  }
     
    12481242     let new_carry = get_arg_8 status ag < d in
    12491243       if get_arg_8 status ag <> d then
    1250          let cry, new_pc = half_add status.pc (mk_word (zero `Eight) rel) in
     1244         let cry, new_pc = half_add status.pc (sign_extension rel) in
    12511245         let status = { status with pc = new_pc } in
    12521246           set_flags status new_carry None (get_ov_flag status)
     
    12571251       let status = set_arg_8 status new_ag ag in
    12581252         if new_ag <> zero `Eight then
    1259            let cry, new_pc = half_add status.pc (mk_word (zero `Eight) rel) in
     1253           let cry, new_pc = half_add status.pc (sign_extension rel) in
    12601254             { status with pc = new_pc }
    12611255         else
  • Deliverables/D4.1/BitVectors.ml

    r142 r147  
    6767let full_add l r c = List.fold_right2 (fun b1 b2 (c,r) -> b1 & b2 || c & (b1 || b2),xor (xor b1 b2) c::r) l r (c,[])
    6868let half_add l r = full_add l r false
     69
     70let sign_extension =
     71 function
     72    [] -> assert false
     73  | (he::_) as l ->
     74      [he;he;he;he;he;he;he;he] @ l
     75;;
     76 
    6977
    7078let rec split_last =
  • Deliverables/D4.1/BitVectors.mli

    r140 r147  
    3333val hex_string_of_vect: 'a vect -> string
    3434
    35 
    3635val (-&-): 'a vect -> 'a vect -> 'a vect
    3736val (-|-): 'a vect -> 'a vect -> 'a vect
     
    4948val half_add: 'a vect -> 'a vect -> bit * 'a vect
    5049val full_add: 'a vect -> 'a vect -> bit -> bit * 'a vect
     50val sign_extension: byte -> word
    5151
    5252val rotate_left : 'a vect -> 'a vect
  • Deliverables/D4.1/Pretty.ml

    r146 r147  
    88  | `C -> "C"
    99  | `DPTR -> "DPTR"
    10   | `ADDR11 x -> "addr11"
    11   | `ADDR16 x -> "addr16"
    12   | `DATA x -> "data"
    13   | `DATA16 x -> "data16"
    14   | `BIT x -> "bit"
    15   | `NBIT x -> "nbit"
    16   | `REG x -> "reg"
    17   | `REL x -> "rel"
     10  | `ADDR11 x -> hex_string_of_vect x
     11  | `ADDR16 x -> hex_string_of_vect x
     12  | `DATA x -> "#" ^ hex_string_of_vect x
     13  | `DATA16 x -> "#" ^ hex_string_of_vect x
     14  | `BIT x -> "bit " ^ hex_string_of_vect (x: byte)
     15  | `NBIT x -> "nbit " ^ hex_string_of_vect (x: byte)
     16  | `REG (r1, r2, r3) -> "R" ^ string_of_int (int_of_vect (mk_nibble false r1 r2 r3))
     17  | `REL x -> hex_string_of_vect x
    1818  | `A_DPTR -> "@DPTR"
    1919  | `A_PC -> "@PC"
    20   | `DIRECT x -> "direct"
    21   | `EXT_INDIRECT x -> "ext_indirect"
     20  | `DIRECT x -> hex_string_of_vect (x: byte)
     21  | `EXT_INDIRECT x -> "ext_indirect " ^ string_of_bool x
    2222  | `EXT_IND_DPTR -> "ext_indirect_dptr"
    23   | `INDIRECT x -> "indirect"
    24   | `IND_DPTR -> "ind_dptr"
     23  | `INDIRECT x -> if x then "@R0" else "@R1"
     24  | `IND_DPTR -> "@DPTR"
    2525;;
    2626
  • Deliverables/D4.1/test.ml

    r145 r147  
     1open BitVectors;;
     2open Util;;
     3open Pretty;;
     4
    15let hex = IntelHex.intel_hex_of_file "Test.hex" in
    26let mem = IntelHex.process_intel_hex hex in
     
    610 let instr,_,_ = ASMInterpret.fetch status.ASMInterpret.code_memory pc in
    711   prerr_string (BitVectors.hex_string_of_vect pc) ;
    8    prerr_endline (": " ^ Pretty.pp_instruction instr)
     12   prerr_endline (": " ^ pp_instruction instr)
    913in
     14(* DPM: for debugging! *)
     15let mem_pretty_print status =
     16  let rec aux status pc =
     17    let instr, pc', cost = ASMInterpret.fetch status.ASMInterpret.code_memory pc in
     18      prerr_string $ ((hex_string_of_vect pc) ^ ": ");
     19      prerr_string $ (pp_instruction instr) ^ " ";
     20      prerr_endline $ string_of_int cost;
     21      if int_of_vect pc' > 105 then (* DPM: hardcoded on a case-by-case basis *)
     22        ()
     23      else
     24        aux status pc'
     25  in
     26    aux status status.ASMInterpret.pc;
     27    prerr_endline "-------------------------------------"
     28in
     29 (* mem_pretty_print status; *)
    1030 ASMInterpret.execute observe status
    1131;;
Note: See TracChangeset for help on using the changeset viewer.