From 4dcfab61473de9f90551bfd95898ac1c5a8d9f62 Mon Sep 17 00:00:00 2001 From: Lynn Leichtle Date: Tue, 17 Sep 2024 22:18:25 +0200 Subject: [PATCH] CPU and instruction declarations. --- src/cpu.zig | 24 ++++++++++++++++++++++++ src/instructions.zig | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/cpu.zig create mode 100644 src/instructions.zig diff --git a/src/cpu.zig b/src/cpu.zig new file mode 100644 index 0000000..30c9e50 --- /dev/null +++ b/src/cpu.zig @@ -0,0 +1,24 @@ +const std = @import("std"); +const mem = @import("memory.zig"); + +pub const Register = enum { + EAX, + ECX, + EDX, + EBX, + // + ESP, + EBP, + ESI, + EDI, +}; + +const Opcode = enum {}; + +pub const Cpu = struct { + registers: std.EnumArray(Register, u32), + instruction_pointer: u32, //EIP + pub fn execute(self: *Cpu, memory: *mem.Memory, opcode: u8, reg1: Register, reg2: Register) void { + switch (opcode) {} + } +}; diff --git a/src/instructions.zig b/src/instructions.zig new file mode 100644 index 0000000..11ba755 --- /dev/null +++ b/src/instructions.zig @@ -0,0 +1,28 @@ +pub const Prefix = struct { + ///Prefix byte guarantees that instruction will have exclusive + ///use of all shared memory, until the instruction completes execution. + lock: bool = false, + ///Repeats instruction the number of times specified by iteration count ECX. + rep: bool = false, + ///Prefix causes memory access to use specified segment + ///instead of default segment designated for instruction operand. + segmentOverride: u8, + ///Changes size of data expected by default mode of the + ///instruction e.g. 16-bit to 32-bit and vice versa. + operandOverride: bool = false, + ///Changes size of address expected by the instruction. + ///32-bit address could switch to 16-bit and vice versa. + addressOverride: bool = false, +}; + +pub const Instruction = struct { + prefix: Prefix, + opcode: u8, + ///The MOD-REG-R/M byte specifies instruction operands and their addressing mode. + modrm: ?u8 = null, + ///Scaled indexed addressing mode uses the second byte (namely, SIB byte) + ///that follows the MOD-REG-R/M byte in the instruction format. + sib: ?u8 = null, + displacement: ?u8 = null, + immediate: ?u8 = null, +};