Ignore:
Timestamp:
Mar 2, 2011, 3:27:41 PM (9 years ago)
Author:
ayache
Message:

Update of D2.2 from Paris.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Deliverables/D2.2/8051/src/ASM/IntelHex.ml

    r486 r619  
    33open Util;;
    44open Parser;;
     5open Printf;;
    56
    67exception WrongFormat of string
     
    3233    | '9' -> 9 | 'A' -> 10 | 'B' -> 11
    3334    | 'C' -> 12 | 'D' -> 13 | 'E' -> 14
    34     | 'F' -> 15 | _ -> assert false
     35    | 'F' -> 15 | 'a' -> 10 | 'b' -> 11
     36    | 'c' -> 12 | 'd' -> 13 | 'e' -> 14
     37    | 'f' -> 15 | _ -> assert false
    3538
    3639let intel_hex_entry_type_of_int =
     
    9396    aux (false, (vect_of_int 0 `Eight)) r
    9497
    95 let checksum_valid hex_entry =
     98let calculate_checksum hex_entry =
    9699 let ty = (flip vect_of_int $ `Eight) $ int_of_intel_hex_entry_type hex_entry.record_type in
    97100 let addr1,addr2 = from_word hex_entry.record_addr in
    98101 let _, total = add_bytes (hex_entry.record_length :: addr1 :: addr2 :: ty :: hex_entry.data_field) in
    99102 let _,total = half_add (vect_of_int 1 `Eight) $ complement total in
    100    hex_entry.data_checksum = total
    101 
     103  total
     104
     105let checksum_valid hex_entry =
     106  let total = calculate_checksum hex_entry in
     107    hex_entry.data_checksum = total
    102108
    103109let prs_intel_hex_record =
     
    135141let string_of_intel_hex_entry entry =
    136142  let length_string = hex_string_of_vect entry.record_length in
    137   let addr_string = hex_string_of_vect entry.record_addr in
    138   let checksum_string = hex_string_of_vect entry.data_checksum in
    139   let type_string = Printf.sprintf "%0 2d" (int_of_intel_hex_entry_type entry.record_type) in
     143  let addr_string = Printf.sprintf "%04X" (int_of_vect entry.record_addr) in
     144  let checksum_string = Printf.sprintf "%02X" (int_of_vect entry.data_checksum) in
     145  let type_string = Printf.sprintf "%02d" (int_of_intel_hex_entry_type entry.record_type) in
    140146  let data_string = String.concat "" (List.map hex_string_of_vect entry.data_field) in
    141147    ":" ^ length_string ^ addr_string ^ type_string ^ data_string ^ checksum_string
     
    188194  aux Physical.WordMap.empty
    189195;;
     196
     197(* DPM: this needs some comment:
     198     We aim to extract code memory into segmented lists of bytes, with a maximum
     199     length (chunk_size).  The code memory map has a fixed size (max_addressable)
     200     on the 8051.  Further, the chunks we extract get segmented when we find an
     201     unitialized zone in the code memory.
     202*)
     203let export_code_memory chunk_size max_addressable code_mem =
     204  let rec aux chunk address start_address rbuff lbuff =
     205    if address = max_addressable then
     206      (start_address, List.rev rbuff)::lbuff
     207    else if chunk = 0 then
     208      aux chunk_size address address [] ((start_address, List.rev rbuff)::lbuff)
     209    else
     210      let code = Physical.WordMap.find (vect_of_int address `Sixteen) code_mem in
     211        aux (chunk - 1) (address + 1) start_address (code::rbuff) lbuff
     212  in
     213    List.rev (aux chunk_size 0 0 [] [])
     214;;
     215
     216let clean_exported_code_memory = List.filter (fun x -> snd x <> [])
     217;;
     218
     219let calculate_data_checksum (record_length, record_addr, record_type, data_field) =
     220  let ty = (flip vect_of_int $ `Eight) $ int_of_intel_hex_entry_type record_type in
     221  let addr1,addr2 = from_word record_addr in
     222  let _, total = add_bytes (record_length :: addr1 :: addr2 :: ty :: data_field) in
     223  let _,total = half_add (vect_of_int 0 `Eight) $ complement total in
     224    total
     225;;
     226
     227let process_exported_code_memory =
     228  List.map (fun x ->
     229    let record_length = vect_of_int (List.length (snd x)) `Eight in
     230    let record_addr = vect_of_int (fst x) `Sixteen in
     231    let record_type = Data in
     232    let data_field = snd x in
     233    let temp_record =
     234      { record_length = record_length;
     235        record_addr = record_addr;
     236        record_type = record_type;
     237        data_field = data_field;
     238        data_checksum = zero `Eight
     239      } in
     240    { temp_record with data_checksum = calculate_checksum temp_record })
     241;;
     242
     243let pack_exported_code_memory chunk_size max_addressable code_mem =
     244  let export = export_code_memory chunk_size max_addressable code_mem in
     245  let cleaned = clean_exported_code_memory export in
     246  let processed = process_exported_code_memory cleaned in
     247  let end_buffer =
     248    [{ record_length = zero `Eight;
     249      record_addr = zero `Sixteen;
     250      record_type = End;
     251      data_field = [];
     252      data_checksum = vect_of_int 255 `Eight
     253    }] in
     254    processed @ end_buffer
     255;;
     256
     257let file_of_intel_hex path fmt =
     258  let str_fmt = string_of_intel_hex_format fmt in
     259  let channel = open_out path in
     260    fprintf channel "%s\n" str_fmt;
     261    close_out channel
     262;;
Note: See TracChangeset for help on using the changeset viewer.