62 lines
1.7 KiB
Odin
62 lines
1.7 KiB
Odin
package sim_8086
|
|
|
|
import "core:os"
|
|
import "core:fmt"
|
|
import "core:math"
|
|
import "core:strings"
|
|
import "core:strconv"
|
|
import "core:reflect"
|
|
|
|
get_cpu_register_by_name :: proc(cpu: ^Cpu, name: string) -> ^RegisterValue {
|
|
reg,_ := reflect.enum_from_name(Register, name)
|
|
return &cpu.registers[reg]
|
|
}
|
|
|
|
parse_reference_cpu_state :: proc(filename: string) -> (Cpu, bool) {
|
|
cpu: Cpu
|
|
|
|
data,ok := os.read_entire_file(filename)
|
|
if !ok {
|
|
return cpu, false
|
|
}
|
|
defer delete(data)
|
|
|
|
content := string(data)
|
|
lines := strings.split(content, "\n")
|
|
defer delete(lines)
|
|
|
|
for line in lines {
|
|
space_count := 0
|
|
for c,i in line {
|
|
if space_count == 0 && c != ' ' {
|
|
break
|
|
} else if c == ' ' {
|
|
space_count += 1
|
|
} else {
|
|
if line[i:i+5] == "flags" {
|
|
idx := i + 7
|
|
for idx < len(line) {
|
|
flag_name := fmt.tprintf("%cF", line[idx])
|
|
if flag,ok := reflect.enum_from_name(Flag, flag_name); ok {
|
|
cpu.flags[flag] = true
|
|
} else {
|
|
fmt.eprintfln("Error parsing flag enum %s", flag_name)
|
|
}
|
|
idx += 1
|
|
}
|
|
} else {
|
|
reg_name := line[i:i+2]
|
|
reg_value := get_cpu_register_by_name(&cpu, reg_name)
|
|
hex_string := line[i+6:i+10]
|
|
if hex_num,ok := strconv.parse_int(hex_string, 16); ok {
|
|
reg_value^.full = i16(hex_num)
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return cpu, true
|
|
}
|