2023-11-27 19:02:10 +01:00
|
|
|
// Digital control for a successive approximation ADC with switched capacitors.
|
|
|
|
|
|
2024-06-10 12:40:37 +02:00
|
|
|
`timescale 100ns/100ns
|
|
|
|
|
|
2023-11-27 19:02:10 +01:00
|
|
|
module adc(Clk, Comp, Start, Sample, Done, Result);
|
2025-01-19 12:42:45 +01:00
|
|
|
parameter Bits=6;
|
|
|
|
|
|
2023-11-27 19:02:10 +01:00
|
|
|
input wire Clk, Comp, Start;
|
|
|
|
|
output reg Sample, Done;
|
|
|
|
|
output reg [Bits - 1 : 0] Result;
|
|
|
|
|
|
|
|
|
|
reg [Bits - 1 : 0] SR;
|
|
|
|
|
reg Running;
|
|
|
|
|
|
|
|
|
|
initial begin
|
|
|
|
|
$display("ADC simulation starting");
|
|
|
|
|
Done = 0;
|
|
|
|
|
Sample = 0;
|
|
|
|
|
Result = 0;
|
|
|
|
|
Running = 0;
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
always @(posedge(Clk)) begin
|
|
|
|
|
if (Running) begin
|
|
|
|
|
if (Sample) begin
|
|
|
|
|
Sample <= 0;
|
2024-06-10 12:40:37 +02:00
|
|
|
SR[Bits - 1] <= 1;
|
|
|
|
|
Result[Bits - 1] <= 1;
|
2023-11-27 19:02:10 +01:00
|
|
|
end else if (SR != 0) begin
|
2024-06-10 12:40:37 +02:00
|
|
|
Result <= (Comp ? (Result & ~SR) : Result) | (SR >> 1);
|
|
|
|
|
SR <= SR >> 1;
|
|
|
|
|
if (SR == 1) begin
|
2023-11-27 19:02:10 +01:00
|
|
|
Running <= 0;
|
|
|
|
|
Done <= 1;
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end else if (Start) begin
|
|
|
|
|
Running <= 1;
|
|
|
|
|
Sample <= 1;
|
|
|
|
|
Done <= 0;
|
2024-06-10 12:40:37 +02:00
|
|
|
SR <= 0;
|
|
|
|
|
Result <= 0;
|
2023-11-27 19:02:10 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
endmodule
|