From ffecebbda13f7b6186bac90330e8ea33755281ef Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 11 Jan 2025 18:13:04 -0500 Subject: [PATCH] clean up magic numbers --- src/ada_chip.adb | 45 +++++++++++++++++++++++++-------------------- src/isa.ads | 14 ++++++++++++++ 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/ada_chip.adb b/src/ada_chip.adb index ed495ee..5b648a5 100644 --- a/src/ada_chip.adb +++ b/src/ada_chip.adb @@ -6,28 +6,29 @@ with Sf.Audio; with Sf.Audio.Sound; with Sf.Audio.SoundBuffer; -with ISA; use ISA; +with ISA; with CPU; with Video; procedure Ada_Chip is use Sf.Audio; - package Random_Byte is new Ada.Numerics.Discrete_Random (Byte); + package Random_Byte is new Ada.Numerics.Discrete_Random (ISA.Byte); - Steps_Per_Frame : constant := 8; + Steps_Per_Frame : constant := 12; State : CPU.Instance; Random_Generator : Random_Byte.Generator; - Delay_Timer : Byte := 0; - Sound_Timer : Byte := 0; + Delay_Timer : ISA.Byte := 0; + Sound_Timer : ISA.Byte := 0; Beep_Sound_Buffer : constant sfSoundBuffer_Ptr := SoundBuffer.createFromFile ("beep.ogg"); Beep_Sound : constant sfSound_Ptr := Sound.create; - procedure Draw_Sprite (VX, VY : Register_Index; N : Byte) is + procedure Draw_Sprite (VX, VY : ISA.Register_Index; N : ISA.Byte) is use Sf; + use ISA; X, Y : sfUint32; Row : aliased Byte; @@ -56,13 +57,15 @@ procedure Ada_Chip is end Draw_Sprite; procedure Run_Step is + use ISA; + ins : Opcode; begin ins := CPU.Get_Opcode (State); case ins.Class is - when Flow => case ins.Value is - when 16#E0# => Video.Clear_Screen; - when 16#EE# => CPU.Ret (State); + when Flow => case Byte (ins.Value) is + when ISA.Clear_Screen => Video.Clear_Screen; + when ISA.Ret => CPU.Ret (State); when others => begin Ada.Text_IO.Put_Line ("Unknown flow instruction!"); Ada.Text_IO.Put_Line (Opcode_Value'Image (ins.Value)); @@ -109,13 +112,13 @@ procedure Ada_Chip is Draw_Sprite (X_Register (ins), Y_Register (ins), To_Byte (ins) mod 16); when Input => case To_Byte (ins) is - when 16#9E# => + when ISA.Key_Down => if Video.Key_Down (Video.Key (State.Registers (X_Register (ins)) mod 16)) then CPU.Skip (State); end if; - when 16#A1# => + when ISA.Key_Up => if Video.Key_Up (Video.Key (State.Registers (X_Register (ins)) mod 16)) then @@ -124,21 +127,21 @@ procedure Ada_Chip is when others => null; end case; when Misc => case To_Byte (ins) is - when 16#07# => + when ISA.Get_Delay => State.Registers (X_Register (ins)) := Delay_Timer; - when 16#0A# => + when ISA.Get_Key => State.Registers (X_Register (ins)) := Byte (Video.Next_Key); - when 16#15# => + when ISA.Set_Delay => Delay_Timer := State.Registers (X_Register (ins)); - when 16#18# => + when ISA.Set_Sound => Sound_Timer := State.Registers (X_Register (ins)); - when 16#1E# => + when ISA.Add_Address => State.Address_Register := State.Address_Register + Address (State.Registers (X_Register (ins))); - when 16#29# => + when ISA.Get_Font => State.Address_Register := Address (State.Registers (X_Register (ins)) mod 16) * 5; - when 16#33# => begin + when ISA.Get_BCD => begin State.Memory (State.Address_Register) := State.Registers (X_Register (ins)) / 100; State.Memory (State.Address_Register + 1) := @@ -146,9 +149,9 @@ procedure Ada_Chip is State.Memory (State.Address_Register + 2) := State.Registers (X_Register (ins)) mod 10; end; - when 16#55# => + when ISA.Reg_Store => CPU.Reg_Store (State, X_Register (ins)); - when 16#65# => + when ISA.Reg_Load => CPU.Reg_Load (State, X_Register (ins)); when others => begin Ada.Text_IO.Put_Line ("Unknown misc instruction!"); @@ -165,6 +168,8 @@ procedure Ada_Chip is end; end case; end Run_Step; + + use ISA; begin if Ada.Command_Line.Argument_Count /= 1 then Ada.Text_IO.Put_Line ("usage: adachip <.c8 file>"); diff --git a/src/isa.ads b/src/isa.ads index fbb81c4..a15fd96 100644 --- a/src/isa.ads +++ b/src/isa.ads @@ -42,6 +42,20 @@ package ISA is Input => 14, Misc => 15); + Clear_Screen : constant Byte := 16#E0#; + Ret : constant Byte := 16#EE#; + Key_Down : constant Byte := 16#9E#; + Key_Up : constant Byte := 16#A1#; + Get_Delay : constant Byte := 16#07#; + Get_Key : constant Byte := 16#0A#; + Set_Delay : constant Byte := 16#15#; + Set_Sound : constant Byte := 16#18#; + Add_Address : constant Byte := 16#1E#; + Get_Font : constant Byte := 16#29#; + Get_BCD : constant Byte := 16#33#; + Reg_Store : constant Byte := 16#55#; + Reg_Load : constant Byte := 16#65#; + Start_Address : constant Address := 16#200#; function To_Byte (O : Opcode) return Byte;