Skip to content

Commit

Permalink
Fixed noise LFSR drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
rejunity committed Sep 18, 2023
1 parent a5cabc8 commit e2fc6a2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
24 changes: 13 additions & 11 deletions src/noise.v
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// For the SMS (1 and 2), Genesis and Game Gear, the tapped bits are bits 0 and 3 ($0009), fed back into bit 15.
// For the SG-1000, OMV, SC-3000H, BBC Micro and Colecovision, the tapped bits are bits 0 and 1 ($0003), fed back into bit 14.
// For the Tandy 1000, the tapped bits are bits 0 and 4 ($0011), fed back into bit 14.
module noise #( parameter LFSR_BITS = 15, LFSR_TAP0 = 0, LFSR_TAP1 = 1, parameter COUNTER_BITS = 10) (
module noise #( parameter LFSR_BITS = 15, LFSR_TAP0 = 0, LFSR_TAP1 = 1, parameter COUNTER_BITS = 10 ) (
input wire clk,
input wire reset,
input wire reset_lfsr,
input wire restart_noise,

input wire [2:0] control,
input wire [COUNTER_BITS-1:0] tone_freq,
Expand All @@ -14,13 +14,8 @@ module noise #( parameter LFSR_BITS = 15, LFSR_TAP0 = 0, LFSR_TAP1 = 1, paramete
localparam MASTER_CLOCK = 16;
localparam TONE_DIV_2 = 2;

reg [LFSR_BITS-1:0] lfsr;
reg is_white_noise;
reg [COUNTER_BITS-1:0] noise_freq;
always @(posedge clk) begin
if (reset | reset_lfsr)
lfsr <= 1'b1 << (LFSR_BITS-1);

// NF0, NF1 bits
case(control[1:0])
2'b00: noise_freq <= 512 /MASTER_CLOCK/TONE_DIV_2;
Expand All @@ -39,11 +34,18 @@ module noise #( parameter LFSR_BITS = 15, LFSR_TAP0 = 0, LFSR_TAP1 = 1, paramete
.compare(noise_freq),
.out(noise_trigger));

always @(posedge noise_trigger) begin
if (is_white_noise) begin
lfsr <= {lfsr[LFSR_TAP0] ^ lfsr[LFSR_TAP1], lfsr[LFSR_BITS-1:1]};
reg is_white_noise;
reg [LFSR_BITS-1:0] lfsr;
assign reset_lfsr = reset | restart_noise;
always @(posedge noise_trigger, posedge reset_lfsr) begin
if (reset_lfsr) begin
lfsr <= 1'b1 << (LFSR_BITS-1);
end else begin
lfsr <= {lfsr[LFSR_TAP0] , lfsr[LFSR_BITS-1:1]};
if (is_white_noise) begin
lfsr <= {lfsr[LFSR_TAP0] ^ lfsr[LFSR_TAP1], lfsr[LFSR_BITS-1:1]};
end else begin
lfsr <= {lfsr[LFSR_TAP0] , lfsr[LFSR_BITS-1:1]};
end
end
end

Expand Down
12 changes: 6 additions & 6 deletions src/tt_um_rejunity_sn76489.v
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module tt_um_rejunity_sn76489 #( parameter NUM_TONES = 3, parameter NUM_NOISES =
reg [FREQUENCY_COUNTER_BITS-1:0] control_tone_freq[NUM_TONES-1:0];
reg [NOISE_CONTROL_BITS-1:0] control_noise[NUM_NOISES-1:0];
reg [2:0] latch_control_reg;
reg reset_noise;
reg restart_noise;

always @(posedge clk) begin
if (reset) begin
Expand All @@ -46,9 +46,9 @@ module tt_um_rejunity_sn76489 #( parameter NUM_TONES = 3, parameter NUM_NOISES =
control_noise[0] <= 3'b100;

latch_control_reg <= 0;
reset_noise <= 0;
restart_noise <= 0;
end else begin
reset_noise <= 0;
restart_noise <= 0;
if (data[7] == 1'b1) begin
case(data[6:4])
3'b000 : control_tone_freq[0][3:0] <= data[3:0];
Expand All @@ -57,7 +57,7 @@ module tt_um_rejunity_sn76489 #( parameter NUM_TONES = 3, parameter NUM_NOISES =
3'b110 :
begin
control_noise[0] <= data[2:0];
reset_noise <= 1;
restart_noise <= 1;
end
3'b001 : control_attn[0] <= data[3:0];
3'b011 : control_attn[1] <= data[3:0];
Expand Down Expand Up @@ -126,7 +126,7 @@ module tt_um_rejunity_sn76489 #( parameter NUM_TONES = 3, parameter NUM_NOISES =
// noise #(.COUNTER_BITS(FREQUENCY_COUNTER_BITS)) gen (
// .clk(clk),
// .reset(reset),
// .reset_lfsr(reset_noise),
// .reset_lfsr(restart_noise),
// .compare(noise_freq),
// .is_white_noise(noise_type),
// .out(channels[NUM_TONES+i])
Expand All @@ -135,7 +135,7 @@ module tt_um_rejunity_sn76489 #( parameter NUM_TONES = 3, parameter NUM_NOISES =
noise #(.COUNTER_BITS(FREQUENCY_COUNTER_BITS)) gen (
.clk(clk),
.reset(reset),
.reset_lfsr(reset_noise),
.restart_noise(restart_noise),
.control(control_noise[i]),
.tone_freq(control_tone_freq[NUM_TONES-1]), // last tone frequency
.out(channels[NUM_TONES+i])
Expand Down

0 comments on commit e2fc6a2

Please sign in to comment.