package sim_8086 import "core:os" import path "core:path/filepath" import "core:fmt" import "core:math" import "core:strings" import "core:reflect" import "core:strconv" main :: proc() { f,err := os.open(os.args[1]) if err != os.ERROR_NONE { fmt.eprintln("ERROR:", err) os.exit(1) } defer os.close(f) what_to_print := len(os.args) >= 3 ? os.args[2] : "" data := make([]u8, 1024) bytes_read, err2 := os.read(f, data) if err2 != nil { // ... os.exit(1) } // asdf :u16 = 0b00000110_11011101 // asdf2 :i16 = (i16)(asdf) // fmt.printfln("%d", asdf2) print_at_end := false line_count := 0 instruction_list := make([dynamic]string, 0, 512) instructions_list := make([dynamic]Instruction, 0, 512) total_bytes := decode_data(&instructions_list, data[:], bytes_read) inst_map := make(map[int]Instruction) for inst in instructions_list { inst_map[inst.segment_offset] = inst } decoded_insts := DecodedInstructions { inst_list = instructions_list, inst_map = inst_map, total_bytes_decoded = total_bytes } cpu := Cpu { memory = make([dynamic]u8, 1024 * 1024), // 1,048,576 } num_idx := strings.last_index(os.args[1], "list-") + 5 if listing_num,ok := strconv.parse_int(os.args[1][num_idx:num_idx+4]); ok { if listing_num > 42 { execute_instructions(&cpu, decoded_insts) } } if what_to_print == "registers" || what_to_print == "all" { fmt.println("Registers") for reg_val,name in cpu.registers { full := fmt.aprintf("%s: %d ", name, i16(reg_val.full)) hex := fmt.aprintf("0x%04x ", u16(reg_val.full)) fmt.printf("%s %*[1]s %s %*[4]s %08b %08b", full, 18 - len(full), "|", hex, 10 - len(hex), "|", reg_val.high, reg_val.low) fmt.println() } fmt.println("\nFlags") for state,flag in cpu.flags { fmt.printf("%c:%d ",reflect.enum_string(flag)[0], state ? 1 : 0) } fmt.println() failed_ref: bool path,ok := strings.replace(os.args[1], ".bin", ".txt", 1) ref_cpu,_ := parse_reference_cpu_state(path) for reg_val,name in ref_cpu.registers { if name == .ip && reg_val.full == 0 do continue if cpu.registers[name].full != reg_val.full { msg := "%s register does not match reference - Expected 0x%04x | Actual 0x%04x" fmt.printfln(msg, name, reg_val.full, cpu.registers[name].full) failed_ref = true } } for f in Flag { if ref_cpu.flags[f] != cpu.flags[f] { msg := "%s flag does not match reference - Expected %d | Actual %d" fmt.printfln(msg, f, ref_cpu.flags[f] ? 1 : 0, cpu.flags[f] ? 1 : 0) failed_ref = true } } if failed_ref { os.exit(1) } } if what_to_print == "instructions" || what_to_print == "all" { print_instructions_stdout(instructions_list[:]) } }