clean up case statements and math

main
Clyne 1 week ago
parent d53fdff709
commit ef5ab9aee5
Signed by: clyne
GPG Key ID: 7BA5A2980566A649

@ -50,11 +50,10 @@ procedure Ada_Chip is
case ins.Value is case ins.Value is
when ISA.Clear_Screen => Video.Clear_Screen; when ISA.Clear_Screen => Video.Clear_Screen;
when ISA.Ret => CPU.Ret (State); when ISA.Ret => CPU.Ret (State);
when others => begin when others =>
Ada.Text_IO.Put_Line ("Machine code calls are unsupported!"); Ada.Text_IO.Put_Line ("Machine code calls are unsupported!");
delay 1.0; delay 1.0;
Video.Finish; Video.Finish;
end;
end case; end case;
end Run_Flow; end Run_Flow;
@ -90,14 +89,13 @@ procedure Ada_Chip is
State.Address_Register + Address (State.Registers (X)); State.Address_Register + Address (State.Registers (X));
when Get_Font => when Get_Font =>
State.Address_Register := Address (State.Registers (X) mod 16) * 5; State.Address_Register := Address (State.Registers (X) mod 16) * 5;
when Get_BCD => begin when Get_BCD =>
State.Memory (State.Address_Register) := State.Memory (State.Address_Register) :=
State.Registers (X) / 100; State.Registers (X) / 100;
State.Memory (State.Address_Register + 1) := State.Memory (State.Address_Register + 1) :=
State.Registers (X) / 10 mod 10; State.Registers (X) / 10 mod 10;
State.Memory (State.Address_Register + 2) := State.Memory (State.Address_Register + 2) :=
State.Registers (X) mod 10; State.Registers (X) mod 10;
end;
end case; end case;
end Run_Misc; end Run_Misc;

@ -1,5 +1,4 @@
with Ada.Sequential_IO; with Ada.Sequential_IO;
with Ada.Text_IO;
with Bit_Ops; with Bit_Ops;
package body CPU is package body CPU is
@ -52,47 +51,32 @@ package body CPU is
end Skip; end Skip;
procedure Math (Inst : in out Instance; VX, VY : Register_Index; N : Byte) procedure Math (Inst : in out Instance; VX, VY : Register_Index; N : Byte)
is begin is
case N is X : constant Byte := Inst.Registers (VX);
when 0 => Y : constant Byte := Inst.Registers (VY);
Inst.Registers (VX) := Inst.Registers (VY); begin
when 1 => case Math_Class'Enum_Val (N) is
Inst.Registers (VX) := Bit_Ops.Bitwise_Or when Assign => Inst.Registers (VX) := Y;
(Inst.Registers (VX), Inst.Registers (VY)); when Bit_Or => Inst.Registers (VX) := Bit_Ops.Bitwise_Or (X, Y);
when 2 => when Bit_And => Inst.Registers (VX) := Bit_Ops.Bitwise_And (X, Y);
Inst.Registers (VX) := Bit_Ops.Bitwise_And when Bit_Xor => Inst.Registers (VX) := Bit_Ops.Bitwise_Xor (X, Y);
(Inst.Registers (VX), Inst.Registers (VY)); when Add =>
when 3 => Inst.Registers (VX) := X + Y;
Inst.Registers (VX) := Bit_Ops.Bitwise_Xor Inst.Registers (15) :=
(Inst.Registers (VX), Inst.Registers (VY)); (if Integer (X) + Integer (Y) > Integer (X + Y)
when 4 => then 1 else 0);
declare when Sub_Y =>
X : constant Byte := Inst.Registers (VX); Inst.Registers (VX) := X - Y;
Y : constant Byte := Inst.Registers (VY); Inst.Registers (15) := (if X >= Y then 1 else 0);
begin when Shift_Right =>
Inst.Registers (VX) := X + Y; Inst.Registers (15) := X mod 2;
Inst.Registers (15) := Inst.Registers (VX) := X / 2;
(if Integer (X) + Integer (Y) > Integer (X + Y) when Sub_X =>
then 1 else 0); Inst.Registers (VX) := Y - X;
end; Inst.Registers (15) := (if Y >= X then 1 else 0);
when 5 => when Shift_Left =>
declare Inst.Registers (15) := X / (2 ** 7);
X : constant Byte := Inst.Registers (VX); Inst.Registers (VX) := X * 2;
Y : constant Byte := Inst.Registers (VY);
begin
Inst.Registers (VX) := X - Y;
Inst.Registers (15) := (if X >= Y then 1 else 0);
end;
when 6 =>
Inst.Registers (15) := Inst.Registers (VX) mod 2;
Inst.Registers (VX) := Inst.Registers (VX) / 2;
when 14 =>
Inst.Registers (15) := Inst.Registers (VX) / (2 ** 7);
Inst.Registers (VX) := Inst.Registers (VX) * 2;
when others => begin
Ada.Text_IO.Put_Line ("Uh oh!");
Ada.Text_IO.Put_Line (Byte'Image (N));
end;
end case; end case;
end Math; end Math;

@ -46,6 +46,23 @@ package ISA is
Clear_Screen : constant Opcode_Value := 16#E0#; Clear_Screen : constant Opcode_Value := 16#E0#;
Ret : constant Opcode_Value := 16#EE#; Ret : constant Opcode_Value := 16#EE#;
type Math_Class is (
Assign, Bit_Or, Bit_And, Bit_Xor, Add, Sub_Y, Shift_Right, Sub_X,
Shift_Left
);
for Math_Class use (
Assign => 0,
Bit_Or => 1,
Bit_And => 2,
Bit_Xor => 3,
Add => 4,
Sub_Y => 5,
Shift_Right => 6,
Sub_X => 7,
Shift_Left => 14
);
type Input_Class is (Key_Down, Key_Up); type Input_Class is (Key_Down, Key_Up);
for Input_Class use ( for Input_Class use (

Loading…
Cancel
Save