Ignore:
Timestamp:
Sep 29, 2010, 12:25:28 PM (9 years ago)
Author:
sacerdot
Message:

Several bug fixes and code clean-up.
New main file: test.ml to parse and execute an HEX file.
Usage: ./test.native foo.hex

File:
1 edited

Legend:

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

    r130 r138  
    1313type line = [`P0 | `P1 ];; (* ??? *)
    1414type continuation =
    15  time ->
    16    [`In of line * byte * continuation
    17    |`Out of (line -> byte -> continuation) ]
     15unit (*
     16 [`In of time * line * byte * continuation] option *
     17 [`Out of (time -> line -> byte -> continuation) ]
     18*)
    1819
    1920(* no differentiation between internal and external code memory *)
     
    9091  timer2 = zero `Sixteen;
    9192
    92   io = (fun _ -> assert false)
     93  io = ()
    9394}
    9495
     
    114115let fetch pmem pc =
    115116 let next pc =
    116    let (carry, res) = half_add pc (vect_of_int 1 `Sixteen) in
     117   let _carry, res = half_add pc (vect_of_int 1 `Sixteen) in
    117118     res, WordMap.find pc pmem
    118119 in
    119  let instr = WordMap.find pc pmem in
    120  let cy, pc = half_add pc (vect_of_int 1 `Sixteen) in
    121  let (un, ln) = from_byte instr in
     120 let pc,instr = next pc in
     121 let un, ln = from_byte instr in
    122122 let bits = (from_nibble un, from_nibble ln) in
    123123  match bits with
     
    667667let load_code_memory = fold_lefti (fun i mem v -> WordMap.add (vect_of_int i `Sixteen) v mem) WordMap.empty
    668668
    669 let load l status = { status with code_memory = load_code_memory l }
     669let load_mem mem status = { status with code_memory = mem }
     670let load l = load_mem (load_code_memory l)
    670671
    671672module StringMap = Map.Make(String);;
     673module IntMap = Map.Make(struct type t = int let compare = compare end);;
    672674
    673675let assembly l =
     
    677679     match i with
    678680        `Label s -> pc, StringMap.add s pc labels, costs
    679       | `Cost s -> pc, labels, StringMap.add s pc costs
    680       | `Jmp s
    681       | `Call s -> pc + 3, labels, costs  (*CSC: very stupid: always expand to worst opcode *)
     681      | `Cost s -> pc, labels, IntMap.add pc s costs
     682      | `Jmp _
     683      | `Call _ -> pc + 3, labels, costs  (*CSC: very stupid: always expand to worst opcode *)
    682684      | #instruction as i ->
    683685        let i',pc',_ = fetch (load_code_memory (assembly1 i)) (vect_of_int 0 `Sixteen) in
    684686         assert (i = i');
    685687         (pc + int_of_vect pc',labels, costs)
    686    ) (0,StringMap.empty,StringMap.empty) l
     688   ) (0,StringMap.empty,IntMap.empty) l
    687689 in
    688690  if pc >= 65536 then
     
    691693      List.flatten (List.map
    692694         (function
    693             `Label s -> []
    694           | `Cost s -> []
     695            `Label _
     696          | `Cost _ -> []
    695697          | `Jmp s ->
    696698              let pc_offset = StringMap.find s labels in
     
    699701              let pc_offset = StringMap.find s labels in
    700702                assembly1 (`LCALL (`ADDR16 (vect_of_int pc_offset `Sixteen)))
    701           | #instruction as i -> assembly1 i) l)
     703          | #instruction as i -> assembly1 i) l), costs
    702704;;
    703705
    704706let get_address_of_register status (b1,b2,b3) =
    705  let bu,bl = from_byte status.psw in
     707 let bu,_bl = from_byte status.psw in
    706708 let (_,_,rs1,rs0) = from_nibble bu in
    707709 let base =
     
    763765;;
    764766
    765 let get_arg_16 status =
    766   function
    767                 `DATA16 w -> w
     767let get_arg_16 _status = function `DATA16 w -> w
    768768
    769769let get_arg_1 status =
     
    820820           (*CSC: SFR access, TO BE IMPLEMENTED *)
    821821           (* assert false for now. Try to understand what DEC really does *)
    822            assert false)
     822prerr_endline ("!!! SFR USED !!!");
     823           status (*assert false*))
    823824  | `INDIRECT b ->
    824825     let (b1, b2) = from_byte (get_register status (false,false,b)) in
     
    956957   | `CLR `A -> set_arg_8 status (zero `Eight) `A
    957958   | `CLR `C -> set_arg_1 status false `C
    958    | `CLR ((`BIT b) as a) -> set_arg_1 status false a
     959   | `CLR ((`BIT _) as a) -> set_arg_1 status false a
    959960   | `CPL `A -> { status with acc = complement status.acc }
    960961   | `CPL `C -> set_arg_1 status (not $ get_arg_1 status `C) `C
     
    10901091       let status = { status with low_internal_ram = lower_mem } in
    10911092       let n1, n2 = from_byte pc_upper_byte in
    1092        let (b1,b2,b3,b) = from_word11 a in
     1093       let (b1,b2,b3,_) = from_word11 a in
    10931094       let (p1,p2,p3,p4),(p5,_,_,_) = from_nibble n1, from_nibble n2 in
    10941095       let addr = mk_word (mk_byte (mk_nibble p1 p2 p3 p4) (mk_nibble p5 b1 b2 b3)) pc_lower_byte in
     
    11691170
    11701171let rec execute f s =
    1171  let s = execute1 s in
    11721172 let cont =
    11731173  try f s; true
    11741174  with Halt -> false
    11751175 in
    1176   if cont then execute f s
     1176  if cont then execute f (execute1 s)
    11771177  else s
    11781178;;
Note: See TracChangeset for help on using the changeset viewer.