Files
fpga_modem/rtl/toplevel/top_generic.v
2025-10-06 16:25:40 +02:00

67 lines
1.7 KiB
Verilog

`timescale 1ns/1ps
module top_generic(
input wire aclk,
input wire aresetn,
output wire led_green,
output wire led_red,
output wire[5:0] r2r
);
assign led_green = 1'b0;
assign led_red = 1'b0;
reg [11:0] count;
localparam integer DIV_MAX = 100_000 - 1; // 1 ms tick at 100 MHz
reg [16:0] div_counter = 0; // enough bits for 100k (2^17=131072)
reg [31:0] freq;
always @(posedge aclk) begin
if (!aresetn) begin
div_counter <= 0;
count <= 0;
end else begin
if (div_counter == DIV_MAX) begin
div_counter <= 0;
if (count == 12'd3999)
count <= 0; // wrap at 4000
else
count <= count + 1'b1; // increment every 1 ms
end else begin
div_counter <= div_counter + 1'b1;
end
end
freq <= count;
end
wire [15:0] sin_q15;
wire clk_en;
nco_q15 #(
.CLK_HZ(100_000_000),
.FS_HZ(40_000)
) nco (
.clk (aclk),
.rst_n (aresetn),
.freq_hz(freq),
.sin_q15(sin_q15),
.cos_q15(),
.clk_en (clk_en)
);
// sin_q15: signed Q15 in [-32768, +32767]
wire signed [15:0] s = sin_q15;
// Bias to 0..65535 and round before downscaling by 1024 (>>10)
wire [16:0] biased = s + 17'sd32768; // 0..65535
wire [5:0] dac_code_next = biased[15:10]; // 0..63 (MSB=bit5)
// Register it at the sample rate (clk_en)
reg [5:0] dac_code;
always @(posedge aclk) begin
if (!aresetn) dac_code <= 6'd0;
else dac_code <= dac_code_next;
end
assign r2r = dac_code;
endmodule