2022-02-11 05:33:33 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2022-02-04 08:00:48 +00:00
|
|
|
const Bus = @import("../../Bus.zig");
|
|
|
|
const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi;
|
2022-07-27 16:10:58 +00:00
|
|
|
const InstrFn = @import("../../cpu.zig").thumb.InstrFn;
|
2022-02-04 08:00:48 +00:00
|
|
|
const shifter = @import("../barrel_shifter.zig");
|
|
|
|
|
|
|
|
const add = @import("../arm/data_processing.zig").add;
|
|
|
|
const sub = @import("../arm/data_processing.zig").sub;
|
|
|
|
const cmp = @import("../arm/data_processing.zig").cmp;
|
|
|
|
const setLogicOpFlags = @import("../arm/data_processing.zig").setLogicOpFlags;
|
|
|
|
|
2022-07-27 16:10:58 +00:00
|
|
|
pub fn fmt1(comptime op: u2, comptime offset: u5) InstrFn {
|
2022-02-04 08:00:48 +00:00
|
|
|
return struct {
|
|
|
|
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
|
|
|
|
const rs = opcode >> 3 & 0x7;
|
|
|
|
const rd = opcode & 0x7;
|
|
|
|
|
|
|
|
const result = switch (op) {
|
|
|
|
0b00 => blk: {
|
|
|
|
// LSL
|
|
|
|
if (offset == 0) {
|
|
|
|
break :blk cpu.r[rs];
|
|
|
|
} else {
|
|
|
|
break :blk shifter.logicalLeft(true, &cpu.cpsr, cpu.r[rs], offset);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
0b01 => blk: {
|
|
|
|
// LSR
|
|
|
|
if (offset == 0) {
|
|
|
|
cpu.cpsr.c.write(cpu.r[rs] >> 31 & 1 == 1);
|
|
|
|
break :blk @as(u32, 0);
|
|
|
|
} else {
|
|
|
|
break :blk shifter.logicalRight(true, &cpu.cpsr, cpu.r[rs], offset);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
0b10 => blk: {
|
|
|
|
// ASR
|
|
|
|
if (offset == 0) {
|
|
|
|
cpu.cpsr.c.write(cpu.r[rs] >> 31 & 1 == 1);
|
|
|
|
break :blk @bitCast(u32, @bitCast(i32, cpu.r[rs]) >> 31);
|
|
|
|
} else {
|
|
|
|
break :blk shifter.arithmeticRight(true, &cpu.cpsr, cpu.r[rs], offset);
|
|
|
|
}
|
|
|
|
},
|
2022-02-17 05:23:35 +00:00
|
|
|
else => cpu.panic("[CPU/THUMB.1] 0b{b:0>2} is not a valid op", .{op}),
|
2022-02-04 08:00:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Equivalent to an ARM MOVS
|
|
|
|
cpu.r[rd] = result;
|
|
|
|
setLogicOpFlags(true, cpu, result);
|
|
|
|
}
|
|
|
|
}.inner;
|
|
|
|
}
|
|
|
|
|
2022-07-27 16:10:58 +00:00
|
|
|
pub fn fmt5(comptime op: u2, comptime h1: u1, comptime h2: u1) InstrFn {
|
|
|
|
return struct {
|
|
|
|
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
|
2022-08-27 01:44:26 +00:00
|
|
|
const rs = @as(u4, h2) << 3 | (opcode >> 3 & 0x7);
|
|
|
|
const rd = @as(u4, h1) << 3 | (opcode & 0x7);
|
2022-07-27 16:10:58 +00:00
|
|
|
|
2022-08-27 01:44:26 +00:00
|
|
|
const rs_value = if (rs == 0xF) cpu.r[rs] & ~@as(u32, 1) else cpu.r[rs];
|
|
|
|
const rd_value = if (rd == 0xF) cpu.r[rd] & ~@as(u32, 1) else cpu.r[rd];
|
2022-07-27 16:10:58 +00:00
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
0b00 => {
|
|
|
|
// ADD
|
2022-08-27 01:44:26 +00:00
|
|
|
const sum = add(false, cpu, rd_value, rs_value);
|
|
|
|
cpu.r[rd] = if (rd == 0xF) sum & ~@as(u32, 1) else sum;
|
2022-07-27 16:10:58 +00:00
|
|
|
},
|
2022-08-27 01:44:26 +00:00
|
|
|
0b01 => cmp(cpu, rd_value, rs_value), // CMP
|
2022-07-27 16:10:58 +00:00
|
|
|
0b10 => {
|
|
|
|
// MOV
|
2022-08-27 01:44:26 +00:00
|
|
|
cpu.r[rd] = if (rd == 0xF) rs_value & ~@as(u32, 1) else rs_value;
|
2022-07-27 16:10:58 +00:00
|
|
|
},
|
|
|
|
0b11 => {
|
|
|
|
// BX
|
2022-08-27 01:44:26 +00:00
|
|
|
const thumb = rs_value & 1 == 1;
|
|
|
|
cpu.r[15] = rs_value & ~@as(u32, 1);
|
2022-08-18 22:51:46 +00:00
|
|
|
|
2022-08-27 01:44:26 +00:00
|
|
|
cpu.cpsr.t.write(thumb);
|
2022-08-18 22:51:46 +00:00
|
|
|
if (thumb) cpu.pipe.reload(u16, cpu) else cpu.pipe.reload(u32, cpu);
|
|
|
|
|
2022-08-27 01:44:26 +00:00
|
|
|
// TODO: We shouldn't need to worry about the if statement
|
|
|
|
// below, because in BX, rd SBZ (and H1 is guaranteed to be 0)
|
|
|
|
return;
|
2022-07-27 16:10:58 +00:00
|
|
|
},
|
|
|
|
}
|
2022-08-18 22:51:46 +00:00
|
|
|
|
2022-08-27 01:44:26 +00:00
|
|
|
if (rd == 0xF) cpu.pipe.reload(u16, cpu);
|
2022-07-27 16:10:58 +00:00
|
|
|
}
|
|
|
|
}.inner;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fmt2(comptime I: bool, is_sub: bool, rn: u3) InstrFn {
|
2022-02-04 08:00:48 +00:00
|
|
|
return struct {
|
|
|
|
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
|
|
|
|
const rs = opcode >> 3 & 0x7;
|
|
|
|
const rd = @truncate(u3, opcode);
|
|
|
|
|
|
|
|
if (is_sub) {
|
|
|
|
// SUB
|
|
|
|
cpu.r[rd] = if (I) blk: {
|
2022-02-06 01:01:39 +00:00
|
|
|
break :blk sub(true, cpu, cpu.r[rs], rn);
|
2022-02-04 08:00:48 +00:00
|
|
|
} else blk: {
|
|
|
|
break :blk sub(true, cpu, cpu.r[rs], cpu.r[rn]);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// ADD
|
|
|
|
cpu.r[rd] = if (I) blk: {
|
2022-02-06 01:01:39 +00:00
|
|
|
break :blk add(true, cpu, cpu.r[rs], rn);
|
2022-02-04 08:00:48 +00:00
|
|
|
} else blk: {
|
|
|
|
break :blk add(true, cpu, cpu.r[rs], cpu.r[rn]);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.inner;
|
|
|
|
}
|
|
|
|
|
2022-07-27 16:10:58 +00:00
|
|
|
pub fn fmt3(comptime op: u2, comptime rd: u3) InstrFn {
|
2022-02-04 08:00:48 +00:00
|
|
|
return struct {
|
|
|
|
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
|
|
|
|
const offset = @truncate(u8, opcode);
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
0b00 => {
|
|
|
|
// MOV
|
|
|
|
cpu.r[rd] = offset;
|
|
|
|
setLogicOpFlags(true, cpu, offset);
|
|
|
|
},
|
|
|
|
0b01 => cmp(cpu, cpu.r[rd], offset), // CMP
|
|
|
|
0b10 => cpu.r[rd] = add(true, cpu, cpu.r[rd], offset), // ADD
|
|
|
|
0b11 => cpu.r[rd] = sub(true, cpu, cpu.r[rd], offset), // SUB
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.inner;
|
|
|
|
}
|
|
|
|
|
2022-07-27 16:10:58 +00:00
|
|
|
pub fn fmt12(comptime isSP: bool, comptime rd: u3) InstrFn {
|
2022-02-04 08:00:48 +00:00
|
|
|
return struct {
|
|
|
|
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
|
|
|
|
// ADD
|
2022-06-29 09:33:17 +00:00
|
|
|
const left = if (isSP) cpu.r[13] else cpu.r[15] & ~@as(u32, 2);
|
2022-02-04 08:00:48 +00:00
|
|
|
const right = (opcode & 0xFF) << 2;
|
2022-06-29 09:33:17 +00:00
|
|
|
cpu.r[rd] = left + right;
|
2022-02-04 08:00:48 +00:00
|
|
|
}
|
|
|
|
}.inner;
|
|
|
|
}
|
|
|
|
|
2022-07-27 16:10:58 +00:00
|
|
|
pub fn fmt13(comptime S: bool) InstrFn {
|
2022-02-04 08:00:48 +00:00
|
|
|
return struct {
|
|
|
|
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
|
|
|
|
// ADD
|
|
|
|
const offset = (opcode & 0x7F) << 2;
|
|
|
|
cpu.r[13] = if (S) cpu.r[13] - offset else cpu.r[13] + offset;
|
|
|
|
}
|
|
|
|
}.inner;
|
|
|
|
}
|