]> code.bitgloo.com Git - clyne/ada-chip.git/commitdiff
play sound
authorClyne Sullivan <clyne@bitgloo.com>
Sat, 11 Jan 2025 22:59:48 +0000 (17:59 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sat, 11 Jan 2025 22:59:48 +0000 (17:59 -0500)
beep.ogg [new file with mode: 0644]
src/ada_chip.adb
src/cpu.ads

diff --git a/beep.ogg b/beep.ogg
new file mode 100644 (file)
index 0000000..a452254
Binary files /dev/null and b/beep.ogg differ
index 36dc1f2b4eb59805e15e53de487af6ef86c0b9af..ed495ee4135bf70d416a26ac537737ed0832ff0a 100644 (file)
@@ -2,18 +2,29 @@ with Ada.Command_Line;
 with Ada.Numerics.Discrete_Random;
 with Ada.Text_IO;
 with Sf;
+with Sf.Audio;
+with Sf.Audio.Sound;
+with Sf.Audio.SoundBuffer;
 
 with ISA; use ISA;
 with CPU;
 with Video;
 
 procedure Ada_Chip is
+   use Sf.Audio;
+
    package Random_Byte is new Ada.Numerics.Discrete_Random (Byte);
 
    Steps_Per_Frame : constant := 8;
 
    State            : CPU.Instance;
    Random_Generator : Random_Byte.Generator;
+   Delay_Timer      : Byte := 0;
+   Sound_Timer      : 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
       use Sf;
@@ -114,12 +125,13 @@ procedure Ada_Chip is
          end case;
          when Misc => case To_Byte (ins) is
             when 16#07# =>
-               State.Registers (X_Register (ins)) := State.Delay_Timer;
+               State.Registers (X_Register (ins)) := Delay_Timer;
             when 16#0A# =>
                State.Registers (X_Register (ins)) := Byte (Video.Next_Key);
             when 16#15# =>
-               State.Delay_Timer := State.Registers (X_Register (ins));
-            when 16#18# => null; --  TODO: sound
+               Delay_Timer := State.Registers (X_Register (ins));
+            when 16#18# =>
+               Sound_Timer := State.Registers (X_Register (ins));
             when 16#1E# =>
                State.Address_Register := State.Address_Register +
                   Address (State.Registers (X_Register (ins)));
@@ -160,18 +172,26 @@ begin
       Video.Initialize;
       Random_Byte.Reset (Random_Generator);
       CPU.Load_File (State, Ada.Command_Line.Argument (1));
+      Sound.setBuffer (Beep_Sound, Beep_Sound_Buffer);
 
       while Video.Is_Running loop
          Video.Display;
          Video.Poll_Events;
 
-         if State.Delay_Timer > 0 then
-            State.Delay_Timer := State.Delay_Timer - 1;
+         if Delay_Timer > 0 then
+            Delay_Timer := Delay_Timer - 1;
+         end if;
+
+         if Sound_Timer > 0 then
+            Sound.play (Beep_Sound);
+            Sound_Timer := Sound_Timer - 1;
          end if;
 
          for I in 0 .. Steps_Per_Frame loop
             Run_Step;
          end loop;
       end loop;
+
+      Sound.destroy (Beep_Sound);
    end if;
 end Ada_Chip;
index ebf24867c257c335affbb2f67cc85edc59b52910..340375b8c6367a6b71c061760b80d31009005717 100644 (file)
@@ -31,7 +31,6 @@ package CPU is
       Program_Counter  : Address := Start_Address;
       Address_Register : Address := 0;
       Stack            : Address_Stack.Vector;
-      Delay_Timer      : Byte := 0;
    end record;
 
    procedure Load_File (Inst : in out Instance; File_Name : String);