Handle Memory movs, change displacement from union to enum
This commit is contained in:
parent
948960e8f5
commit
11ccfe78a9
@ -99,13 +99,13 @@ parse_operand :: proc(inst: InstructionInfo, opinfo: OperandInfo, data: []u8, pr
|
|||||||
op = (DirectAddress)(get_i16(data[2:]))
|
op = (DirectAddress)(get_i16(data[2:]))
|
||||||
processed^ += 2
|
processed^ += 2
|
||||||
} else {
|
} else {
|
||||||
op = MemoryAddr{ addr_id = rm , displacement = None{} }
|
op = MemoryAddr{ addr_id = rm }
|
||||||
}
|
}
|
||||||
} else if mod == 1 {
|
} else if mod == 1 {
|
||||||
op = MemoryAddr{ addr_id = rm , displacement = (i8)(data[2]) }
|
op = MemoryAddr{ addr_id = rm , displacement_value = (i16)(data[2]) , displacement_size = .Signed8 }
|
||||||
processed^ += 1
|
processed^ += 1
|
||||||
} else if mod == 2 {
|
} else if mod == 2 {
|
||||||
op = MemoryAddr{ addr_id = rm , displacement = get_i16(data[2:]) }
|
op = MemoryAddr{ addr_id = rm , displacement_value = get_i16(data[2:]) , displacement_size = .Signed16 }
|
||||||
processed^ += 2
|
processed^ += 2
|
||||||
} else if mod == 3 {
|
} else if mod == 3 {
|
||||||
if word {
|
if word {
|
||||||
|
@ -11,7 +11,6 @@ get_ip :: proc(cpu: ^Cpu) -> int { return int(cpu.registers[.ip].full) }
|
|||||||
get_operand_value :: proc(cpu: ^Cpu, operand: Operand) -> i16 {
|
get_operand_value :: proc(cpu: ^Cpu, operand: Operand) -> i16 {
|
||||||
#partial switch opr in operand {
|
#partial switch opr in operand {
|
||||||
case Immediate:
|
case Immediate:
|
||||||
// fmt.printfln("0x%4x %d", i16(opr.value), opr.value)
|
|
||||||
return i16(opr.value)
|
return i16(opr.value)
|
||||||
case RegisterId:
|
case RegisterId:
|
||||||
reg_val := cpu.registers[opr.name]
|
reg_val := cpu.registers[opr.name]
|
||||||
@ -21,6 +20,9 @@ get_operand_value :: proc(cpu: ^Cpu, operand: Operand) -> i16 {
|
|||||||
case .Full:
|
case .Full:
|
||||||
return i16(reg_val.full)
|
return i16(reg_val.full)
|
||||||
}
|
}
|
||||||
|
case DirectAddress:
|
||||||
|
value := i16(u16(cpu.memory[opr+1] << 8) | u16(cpu.memory[opr]))
|
||||||
|
return value
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -107,6 +109,32 @@ execute_instruction :: proc(cpu: ^Cpu, inst: Instruction) {
|
|||||||
check_auxiliary_carry_flag(cpu, dst_val, src_val, false)
|
check_auxiliary_carry_flag(cpu, dst_val, src_val, false)
|
||||||
check_carry_flag(cpu, dst_val, src_val, false)
|
check_carry_flag(cpu, dst_val, src_val, false)
|
||||||
}
|
}
|
||||||
|
} else if addr,ok := inst.dst.(DirectAddress); ok {
|
||||||
|
#partial switch inst.opname {
|
||||||
|
case .MOV:
|
||||||
|
if imm,ok := inst.src.(Immediate); ok {
|
||||||
|
if imm.size == .Signed16 {
|
||||||
|
cpu.memory[addr] = u8(imm.value & 0x00FF)
|
||||||
|
cpu.memory[addr+1] = u8((u16(imm.value) & 0xFF00) >> 8)
|
||||||
|
} else {
|
||||||
|
cpu.memory[addr] = u8(imm.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if mem_addr,ok := inst.dst.(MemoryAddr); ok {
|
||||||
|
#partial switch inst.opname {
|
||||||
|
case .MOV:
|
||||||
|
if imm,ok := inst.src.(Immediate); ok {
|
||||||
|
// TODO: We need a function that returns the registers to check out
|
||||||
|
addr := cpu.registers[.bx].full + mem_addr.displacement_value
|
||||||
|
if imm.size == .Signed16 {
|
||||||
|
cpu.memory[addr] = u8(imm.value & 0x00FF)
|
||||||
|
cpu.memory[addr+1] = u8((u16(imm.value) & 0xFF00) >> 8)
|
||||||
|
} else {
|
||||||
|
cpu.memory[addr] = u8(imm.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if jmp_offset,ok := inst.src.(Jump); ok {
|
} else if jmp_offset,ok := inst.src.(Jump); ok {
|
||||||
jump: bool
|
jump: bool
|
||||||
#partial switch inst.opname {
|
#partial switch inst.opname {
|
||||||
|
@ -39,18 +39,10 @@ calculate_effective_address :: proc(r_m: u8) -> string {
|
|||||||
|
|
||||||
get_memory_string :: proc(memoryAddr: MemoryAddr, has_segment: Maybe(RegisterId)) -> string {
|
get_memory_string :: proc(memoryAddr: MemoryAddr, has_segment: Maybe(RegisterId)) -> string {
|
||||||
disp: string
|
disp: string
|
||||||
switch value in memoryAddr.displacement {
|
value := memoryAddr.displacement_value
|
||||||
case None:
|
|
||||||
disp = ""
|
|
||||||
case Disp8:
|
|
||||||
if value != 0 {
|
if value != 0 {
|
||||||
disp = fmt.aprintf(" %s %d", value > 0 ? "+" : "-", math.abs(value))
|
disp = fmt.aprintf(" %s %d", value > 0 ? "+" : "-", math.abs(value))
|
||||||
}
|
}
|
||||||
case Disp16:
|
|
||||||
if value != 0 {
|
|
||||||
disp = fmt.aprintf(" %s %d", value > 0 ? "+" : "-", math.abs(value))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
seg_string: string
|
seg_string: string
|
||||||
if segreg, ok := has_segment.?; ok {
|
if segreg, ok := has_segment.?; ok {
|
||||||
seg_string = fmt.aprintf("%s:", get_register_name(segreg))
|
seg_string = fmt.aprintf("%s:", get_register_name(segreg))
|
||||||
@ -59,21 +51,6 @@ get_memory_string :: proc(memoryAddr: MemoryAddr, has_segment: Maybe(RegisterId)
|
|||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
get_displacement_string :: proc(displacement: Displacement) -> string {
|
|
||||||
disp := ""
|
|
||||||
#partial switch value in displacement {
|
|
||||||
case i8:
|
|
||||||
if value != 0 {
|
|
||||||
disp = fmt.aprintf(" %s %d", value > 0 ? "+" : "-", math.abs(value))
|
|
||||||
}
|
|
||||||
case i16:
|
|
||||||
if value != 0 {
|
|
||||||
disp = fmt.aprintf(" %s %d", value > 0 ? "+" : "-", math.abs(value))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return disp
|
|
||||||
}
|
|
||||||
|
|
||||||
get_register_name :: proc(reg_id: RegisterId) -> string {
|
get_register_name :: proc(reg_id: RegisterId) -> string {
|
||||||
low_names := [?]string{"al", "cl", "dl", "bl"}
|
low_names := [?]string{"al", "cl", "dl", "bl"}
|
||||||
high_names := [?]string{"ah", "ch", "dh", "bh"}
|
high_names := [?]string{"ah", "ch", "dh", "bh"}
|
||||||
|
@ -44,7 +44,7 @@ main :: proc() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cpu := Cpu {
|
cpu := Cpu {
|
||||||
memory = make([dynamic]u8, 1_048_576), // Exact num per the manual
|
memory = make([dynamic]u8, 1024 * 1024), // 1,048,576
|
||||||
}
|
}
|
||||||
|
|
||||||
execute_instructions(&cpu, decoded_insts)
|
execute_instructions(&cpu, decoded_insts)
|
||||||
|
10
types.odin
10
types.odin
@ -46,13 +46,6 @@ WordSize :: enum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
None :: struct {}
|
None :: struct {}
|
||||||
Disp8 :: i8
|
|
||||||
Disp16 :: i16
|
|
||||||
Displacement :: union {
|
|
||||||
None,
|
|
||||||
Disp8,
|
|
||||||
Disp16
|
|
||||||
}
|
|
||||||
|
|
||||||
RegisterAccess :: enum {
|
RegisterAccess :: enum {
|
||||||
Full,
|
Full,
|
||||||
@ -75,7 +68,8 @@ Immediate :: struct {
|
|||||||
}
|
}
|
||||||
MemoryAddr :: struct {
|
MemoryAddr :: struct {
|
||||||
addr_id: u8,
|
addr_id: u8,
|
||||||
displacement: Displacement,
|
displacement_value: i16,
|
||||||
|
displacement_size: ImmediateSize,
|
||||||
}
|
}
|
||||||
DirectAddress :: distinct i16
|
DirectAddress :: distinct i16
|
||||||
Jump :: distinct i8
|
Jump :: distinct i8
|
||||||
|
Loading…
x
Reference in New Issue
Block a user