diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000000000000000000000000000000000..bed0738c7eeb449bca98b5d2f33c89a1ee56349a --- /dev/null +++ b/.gitattributes @@ -0,0 +1,60 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.avro filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ckpt filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.lz4 filter=lfs diff=lfs merge=lfs -text +*.mds filter=lfs diff=lfs merge=lfs -text +*.mlmodel filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.npy filter=lfs diff=lfs merge=lfs -text +*.npz filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pickle filter=lfs diff=lfs merge=lfs -text +*.pkl filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +*.safetensors filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tar filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.wasm filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text +*tfevents* filter=lfs diff=lfs merge=lfs -text +# Audio files - uncompressed +*.pcm filter=lfs diff=lfs merge=lfs -text +*.sam filter=lfs diff=lfs merge=lfs -text +*.raw filter=lfs diff=lfs merge=lfs -text +# Audio files - compressed +*.aac filter=lfs diff=lfs merge=lfs -text +*.flac filter=lfs diff=lfs merge=lfs -text +*.mp3 filter=lfs diff=lfs merge=lfs -text +*.ogg filter=lfs diff=lfs merge=lfs -text +*.wav filter=lfs diff=lfs merge=lfs -text +# Image files - uncompressed +*.bmp filter=lfs diff=lfs merge=lfs -text +*.gif filter=lfs diff=lfs merge=lfs -text +*.png filter=lfs diff=lfs merge=lfs -text +*.tiff filter=lfs diff=lfs merge=lfs -text +# Image files - compressed +*.jpg filter=lfs diff=lfs merge=lfs -text +*.jpeg filter=lfs diff=lfs merge=lfs -text +*.webp filter=lfs diff=lfs merge=lfs -text +# Video files - compressed +*.mp4 filter=lfs diff=lfs merge=lfs -text +*.webm filter=lfs diff=lfs merge=lfs -text diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..50883a4ee0cd866c82ed6f05267b463d55925b58 --- /dev/null +++ b/LICENSE @@ -0,0 +1,47 @@ +MIT License + +Copyright (c) 2023-2024 NVIDIA Research Projects + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + +This project contains code from human-eval (https://github.com/openai/human-eval/). + +The MIT License + +Copyright (c) OpenAI (https://openai.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..948f09768f3e030b4633e206f2495b37edd18890 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +--- +license: mit +pretty_name: ChipBench Verified +tags: [verilog, rtl, hardware-design, benchmark] +--- + +# ChipBench Verified + +A fixed version of [ChipBench](https://github.com/zhongkaiyu/ChipBench). Same format and file names as the original, so the original harness runs it as is. + +We checked each task's spec against its hidden tests: + +| Tasks | Spec and tests agree | Weak tests | Unfair tests | Both | +|---|---|---|---|---| +| 134 | 68 | 14 | 43 | 9 | + +Weak tests: the spec states behaviour the tests never check. Unfair tests: the tests require something the spec does not +state. Both: the task has both problems. + +We fixed the spec in 52 tasks and broken tests in 36 tasks, and added checks for unchecked spec behaviour +in 18 tasks. In 5 tasks with weak tests we did not add checks, so their tests can still miss a design that skips +some stated behaviour. Every changed task was re-run to confirm it is passable from the spec. Diff against the original +files to see each change. + +Layout: `Verilog Gen/dataset_*/` and `Verilog Debugging/dataset_debug_zero_shot_*/` with `ProbNNN__prompt.txt`, `_ref.sv` and `_test.sv`. Covers Verilog Gen (45) and zero-shot Verilog Debugging (89). + + +More details: https://chipagents.ai/research/open-rtl-benchmarks + +Derived from ChipBench (https://github.com/zhongkaiyu/ChipBench) under its original license; see LICENSE. diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob001_continuous_input_sequence_detect_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob001_continuous_input_sequence_detect_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..0378cf8256bc61afb7022a03ca40858e8d4db02b --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob001_continuous_input_sequence_detect_prompt.txt @@ -0,0 +1,62 @@ +I would like you to implement a module named `TopModule` with the following interface. All input and output ports are one bit unless otherwise specified. + +- **Input ports**: + - `clk` + - `rst_n` + - `a` + +- **Output ports**: + - `match` + +The module should implement an 8-bit shift register that shifts in the input `a` on every positive edge of the clock. The shift register is named `a_tem` and retains the 8 most recent bits of `a`. The module should perform the following functionality: + +1. **Reset Behavior**: + - When `rst_n` is de-asserted (logic 0), the shift register `a_tem` should be cleared to `8'b0`, and the output `match` should be set to `0`. + +2. **Shift Register Behavior**: + - On every positive edge of the clock, if `rst_n` is asserted (logic 1), the input `a` should be shifted into the least significant bit (LSB) of the shift register `a_tem`, and the previous bits should shift left by one position. + +3. **Matching Logic**: + - The output `match` should be asserted (logic 1) whenever the value of the shift register `a_tem` matches the specific 8-bit pattern `8'b0111_0001`. + - If the shift register does not match this pattern, the output `match` should remain de-asserted (logic 0). + +The design must operate synchronously with the clock and use a negative edge active reset (`rst_n`). Ensure the module correctly implements this functionality. + + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input a, + output reg match + ); + + reg [7:0] a_tem; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match <= 1'b0; + end + else if (a_tem == 8'b0111_0001) + begin + match <= 1'b1; + end + else + begin + match <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 8'b0; + end + else + begin + a_tem <= {a, a_tem[6:0]}; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob001_continuous_input_sequence_detect_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob001_continuous_input_sequence_detect_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..7f331ec29a6c80c9534b2e3dc96a5cc965d76324 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob001_continuous_input_sequence_detect_ref.sv @@ -0,0 +1,33 @@ +module RefModule( + input clk, + input rst_n, + input a, + output reg match + ); + + reg [7:0] a_tem; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match <= 1'b0; + end + else if (a_tem == 8'b0111_0001) + begin + match <= 1'b1; + end + else + begin + match <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 8'b0; + end + else + begin + a_tem <= {a_tem[6:0],a}; + end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob001_continuous_input_sequence_detect_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob001_continuous_input_sequence_detect_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..d2be5472b13f3a43ba2a478f58fccd93ef3d7257 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob001_continuous_input_sequence_detect_test.sv @@ -0,0 +1,165 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 +`default_nettype none + +module stimulus_gen ( + input wire clk, + output reg rst_n, + output reg a, + input wire tb_match, + input wire match_dut +); + bit failed = 0; + + always @(posedge clk, negedge clk) + if (!tb_match) + failed <= 1; + + initial begin + @(posedge clk); + failed <= 0; + rst_n <= 0; + a <= 0; + + // Initial reset + @(posedge clk); + rst_n <= 1; + + // Test Case 1: No match + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + @(posedge clk) a <= 1; + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + + // Test Case 2: Matching sequence (0111_0001) + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + @(posedge clk) a <= 1; + @(posedge clk) a <= 1; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + + // Test Case 3: Randomized input + repeat (100) @(posedge clk) begin + a <= $random; + end + + // Test Case 4: Edge cases with reset + @(posedge clk); + rst_n <= 0; + @(posedge clk); + rst_n <= 1; + repeat (8) @(posedge clk) a <= 1; + + // Add delay to observe outputs + repeat (100) @(posedge clk); + + #1 $finish; + end +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + + int clocks; + } stats; + + stats stats1; + + wire [511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk = 0; + initial forever #5 clk = ~clk; + + logic rst_n; + logic a; + logic match_ref; + logic match_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, a, match_ref, match_dut); + end + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + stimulus_gen stim1 ( + .clk(clk), + .rst_n(rst_n), + .a(a), + .tb_match(tb_match), + .match_dut(match_dut) + ); + + RefModule good1 ( + .clk(clk), + .rst_n(rst_n), + .a(a), + .match(match_ref) + ); + + TopModule top_module1 ( + .clk(clk), + .rst_n(rst_n), + .a(a), + .match(match_dut) + ); + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat (5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + final begin + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( + {match_ref} === + ({match_ref} ^ {match_dut} ^ {match_ref}) + ); + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (match_ref !== (match_ref ^ match_dut ^ match_ref)) begin + if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match++; + end + end + + // Add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob002_extraneous_items_sequence_detect_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob002_extraneous_items_sequence_detect_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..32925637b8129cb8da22679291b08f8d39bf6da8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob002_extraneous_items_sequence_detect_prompt.txt @@ -0,0 +1,87 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input a + - output match + +The module should implement a sequence detection circuit that detects the +pattern "011XXX110" in the input data stream. The pattern consists of 9 bits +where the first 3 bits are "011", the last 3 bits are "110", and the middle +3 bits can be any value (don't care). + +The sequence detection should work as follows: +- Monitor the input data stream continuously +- Detect when the pattern "011XXX110" appears in the input +- Output match signal should be high when the pattern is detected +- Output match signal should be low at all other times + +Implementation approach: +The module should use a 9-bit shift register to store the last 9 input bits. +The match signal should be asserted when: +- The first 3 bits (bits 8:6) equal "011" AND +- The last 3 bits (bits 2:0) equal "110" +- The middle 3 bits (bits 5:3) can be any value + +### Timing and Behavior: +- When rst_n is low, the module should reset to its initial state +- On every positive clock edge (clk), the module should shift in the new input bit +- The output match is updated sequentially: it goes high on the second rising clk edge after the pattern's last bit is applied to a +- The detection is continuous - the module checks for the pattern on every clock cycle + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input a, + output match + ); + + reg [8:0] a_tem; + reg match_f; + reg match_b; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match_f <= 1'b0; + end + else if (a_tem[8:6] == 3'b011) + begin + match_f <= 1'b1; + end + else + begin + match_f <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match_b <= 1'b0; + end + else if (a_tem[2:0] == 3'b110) + begin + match_b <= 1'b1; + end + else + begin + match_b <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 9'b0; + end + else + begin + a_tem <= {a, a_tem[7:0]}; + end + + assign match = match_b && match_f; +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob002_extraneous_items_sequence_detect_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob002_extraneous_items_sequence_detect_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..9aa18d6688f374e6ffa77394980b17c15b7107df --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob002_extraneous_items_sequence_detect_ref.sv @@ -0,0 +1,51 @@ +module RefModule( + input clk, + input rst_n, + input a, + output match + ); + + reg [8:0] a_tem; + reg match_f; + reg match_b; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match_f <= 1'b0; + end + else if (a_tem[8:6] == 3'b011) + begin + match_f <= 1'b1; + end + else + begin + match_f <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match_b <= 1'b0; + end + else if (a_tem[2:0] == 3'b110) + begin + match_b <= 1'b1; + end + else + begin + match_b <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 9'b0; + end + else + begin + a_tem <= {a_tem[7:0],a}; + end + + assign match = match_b && match_f; +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob002_extraneous_items_sequence_detect_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob002_extraneous_items_sequence_detect_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..7ebedeb9cae8bb2584af627b24db2b13c1bc3147 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob002_extraneous_items_sequence_detect_test.sv @@ -0,0 +1,178 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic a, + output logic rst_n, + output reg[511:0] wavedrom_title, + output reg wavedrom_enable, + input tb_match + // === End your code here === +); + // === Start your code here === + reg reset; + assign rst_n = ~reset; + + task wavedrom_start(input[511:0] title = ""); + endtask + + task wavedrom_stop; + #1; + endtask + + initial begin + reset = 1; + a = 0; + @(posedge clk); + reset = 0; + + wavedrom_start("Sequence Detection Test"); + + // Test specific sequences for "011XXX110" pattern + @(posedge clk) a = 0; // Start of 011101110 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 1; // Middle bit 1 + @(posedge clk) a = 0; // Middle bit 0 + @(posedge clk) a = 1; // Middle bit 1 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 0; // Should detect pattern here + + @(posedge clk) a = 0; // Start of 011001110 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 0; // Middle bit 0 + @(posedge clk) a = 0; // Middle bit 0 + @(posedge clk) a = 1; // Middle bit 1 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 0; // Should detect pattern here + + wavedrom_stop(); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + a <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic a; + logic rst_n; + logic match_ref; + logic match_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, a, rst_n, match_ref, match_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .* , + .a, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .a, + .match(match_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .a, + .match(match_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { match_ref } === ( { match_ref } ^ { match_dut } ^ { match_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (match_ref !== ( match_ref ^ match_dut ^ match_ref )) + begin if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match = stats1.errors_match+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob006_data_serial-to-parallel_circuit_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob006_data_serial-to-parallel_circuit_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..c8e334048d0640495374768fde5aba1cf285c1f7 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob006_data_serial-to-parallel_circuit_prompt.txt @@ -0,0 +1,88 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_a + - input data_a + - output ready_a + - output valid_b + - output data_b (6 bits) + +The module should implement a serial-to-parallel conversion circuit that +converts single-bit input data into 6-bit parallel output data. The circuit +uses different handshake protocols for input and output interfaces. + +The serial-to-parallel conversion should work as follows: +- Input interface: Uses valid-ready handshake mechanism + - valid_a indicates the validity of input data_a + - ready_a indicates the module is always ready to receive upstream data +- Output interface: Uses valid-only handshake mechanism + - valid_b indicates the validity of output data_b + - data_b is the 6-bit parallel output data + +Data conversion process: +- The module receives single-bit input data_a when valid_a is high +- After receiving 6 input data bits, the module outputs the concatenated + 6-bit data_b with valid_b asserted +- valid_b is high after each clock edge at which five bits of the current + word were already received, including while the sixth bit is awaited +- The first received data bit should be placed in the LSB (bit 0) of data_b +- The last received data bit should be placed in the MSB (bit 5) of data_b +- ready_a is always high indicates the module is always ready to receive input data + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input valid_a , + input data_a , + + output reg ready_a , + output reg valid_b , + output reg [5:0] data_b +); + reg [5:0] data_r; + reg [2:0] cnt; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~ready_a&&~valid_a? cnt: + cnt == 5? 0 : + cnt+1; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_r <= 6'b0; + else + data_r <= ready_a&&valid_a? {data_a, data_r[5:1]}: data_r; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_b <= 6'b0; + else + data_b <= cnt==5&&valid_a? {data_a, data_r[5:1]}: data_b; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_b <= 0; + else + valid_b <= cnt==5; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + ready_a <= 0; + else + ready_a <= 1; + end +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob006_data_serial-to-parallel_circuit_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob006_data_serial-to-parallel_circuit_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..280616e82b10504600dea0c2e26db481db5b0642 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob006_data_serial-to-parallel_circuit_ref.sv @@ -0,0 +1,50 @@ +module RefModule( + input clk , + input rst_n , + input valid_a , + input data_a , + + output reg ready_a , + output reg valid_b , + output reg [5:0] data_b +); + reg [5:0] data_r; + reg [2:0] cnt; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~ready_a||~valid_a? cnt: + cnt == 5? 0 : + cnt+1; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_r <= 6'b0; + else + data_r <= ready_a&&valid_a? {data_a, data_r[5:1]}: data_r; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_b <= 6'b0; + else + data_b <= cnt==5&&valid_a? {data_a, data_r[5:1]}: data_b; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_b <= 0; + else + valid_b <= cnt==5; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + ready_a <= 0; + else + ready_a <= 1; + end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob006_data_serial-to-parallel_circuit_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob006_data_serial-to-parallel_circuit_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..80960f300c161a5c1c4aa9165937c0313ed072a0 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob006_data_serial-to-parallel_circuit_test.sv @@ -0,0 +1,202 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_a, + output logic data_a + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_a = 0; + data_a = 0; + repeat(4)@(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test serial data input: 6 bits per group + // First group: 101010 + valid_a = 1; + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + + // Second group: 110011 + data_a = 1; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 1; @(posedge clk); + + // Test with valid_a low (should ignore data) + valid_a = 0; + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 1; @(posedge clk); + + // Third group: 111000 + valid_a = 1; + data_a = 1; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 0; @(posedge clk); + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + valid_a <= $random; + data_a <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_ready_a; + int errortime_ready_a; + int errors_valid_b; + int errortime_valid_b; + int errors_data_b; + int errortime_data_b; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_a; + logic data_a; + logic ready_a_ref; + logic ready_a_dut; + logic valid_b_ref; + logic valid_b_dut; + logic [5:0] data_b_ref; + logic [5:0] data_b_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_a, data_a, ready_a_ref, ready_a_dut, valid_b_ref, valid_b_dut, data_b_ref, data_b_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_a, + .data_a ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_a, + .data_a, + .ready_a(ready_a_ref), + .valid_b(valid_b_ref), + .data_b(data_b_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_a, + .data_a, + .ready_a(ready_a_dut), + .valid_b(valid_b_dut), + .data_b(data_b_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_ready_a) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "ready_a", stats1.errors_ready_a, stats1.errortime_ready_a); + else $display("Hint: Output '%s' has no mismatches.", "ready_a"); + if (stats1.errors_valid_b) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_b", stats1.errors_valid_b, stats1.errortime_valid_b); + else $display("Hint: Output '%s' has no mismatches.", "valid_b"); + if (stats1.errors_data_b) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_b", stats1.errors_data_b, stats1.errortime_data_b); + else $display("Hint: Output '%s' has no mismatches.", "data_b"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { ready_a_ref, valid_b_ref, data_b_ref } === ( { ready_a_ref, valid_b_ref, data_b_ref } ^ { ready_a_dut, valid_b_dut, data_b_dut } ^ { ready_a_ref, valid_b_ref, data_b_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (ready_a_ref !== ( ready_a_ref ^ ready_a_dut ^ ready_a_ref )) + begin if (stats1.errors_ready_a == 0) stats1.errortime_ready_a = $time; + stats1.errors_ready_a = stats1.errors_ready_a+1'b1; end + if (valid_b_ref !== ( valid_b_ref ^ valid_b_dut ^ valid_b_ref )) + begin if (stats1.errors_valid_b == 0) stats1.errortime_valid_b = $time; + stats1.errors_valid_b = stats1.errors_valid_b+1'b1; end + if (data_b_ref !== ( data_b_ref ^ data_b_dut ^ data_b_ref )) + begin if (stats1.errors_data_b == 0) stats1.errortime_data_b = $time; + stats1.errors_data_b = stats1.errors_data_b+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob007_data_accumulation_output_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob007_data_accumulation_output_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..e02212519cadd6640e2ff08e4bcd45fbab8e9955 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob007_data_accumulation_output_prompt.txt @@ -0,0 +1,82 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input data_in (8 bits) + - input valid_a + - input ready_b + - output ready_a + - output valid_b + - output data_out (10 bits) + +The module should implement a serial data accumulation circuit that receives +8-bit input data and outputs the accumulated sum of every 4 input values. +The circuit uses valid-ready handshake mechanisms for both input and output +interfaces to ensure efficient data transfer without performance loss. + +The data accumulation should work as follows: +- Input interface: Uses valid-ready handshake mechanism + - valid_a indicates the validity of input data_in + - ready_a indicates the module is ready to receive upstream data +- Output interface: Uses valid-ready handshake mechanism + - valid_b indicates the validity of output data_out + - ready_b indicates downstream is ready to receive data + +Data accumulation process: +- The module receives 8-bit input data_in when valid_a is high and ready_a is high +- After receiving 4 input data values, the module outputs the accumulated sum + as 10-bit data_out with valid_b asserted +- The output data_out represents the sum of the 4 most recently received inputs; + if ready_b is low when the first input of a group is received, that input is + added to the previous sum instead +- Both input and output interfaces must support backpressure through ready signals +- The design must ensure no performance loss when both upstream and downstream + are ready for full-speed transmission (no bubbles in data flow) + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input [7:0] data_in , + input valid_a , + input ready_b , + + output ready_a , + output reg valid_b , + output reg [9:0] data_out +); +reg [1:0] data_cnt; + +assign ready_a = !valid_b | ready_b; + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_cnt <= 'd0; + else if(valid_a || ready_a) + data_cnt <= (data_cnt == 2'd3) ? 'd0 : (data_cnt + 1'd1); +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_b <= 'd0; + else if(data_cnt == 2'd3 && valid_a && ready_a) + valid_b <= 1'd1; + else if(valid_b && ready_b) + valid_b <= 1'd0; +end + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(ready_b && valid_a && ready_a && (data_cnt == 2'd0)) + data_out <= data_in; + else if(valid_a && ready_a) + data_out <= data_out + data_in; + +end + + +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob007_data_accumulation_output_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob007_data_accumulation_output_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..64f76b80741305805eb36463a9cd86dcf227cc64 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob007_data_accumulation_output_ref.sv @@ -0,0 +1,44 @@ +`timescale 1ns/1ns + +module RefModule( + input clk , + input rst_n , + input [7:0] data_in , + input valid_a , + input ready_b , + + output ready_a , + output reg valid_b , + output reg [9:0] data_out +); +reg [1:0] data_cnt; + +assign ready_a = !valid_b | ready_b; + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_cnt <= 'd0; + else if(valid_a && ready_a) + data_cnt <= (data_cnt == 2'd3) ? 'd0 : (data_cnt + 1'd1); +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_b <= 'd0; + else if(data_cnt == 2'd3 && valid_a && ready_a) + valid_b <= 1'd1; + else if(valid_b && ready_b) + valid_b <= 1'd0; +end + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(ready_b && valid_a && ready_a && (data_cnt == 2'd0)) + data_out <= data_in; + else if(valid_a && ready_a) + data_out <= data_out + data_in; + +end + + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob007_data_accumulation_output_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob007_data_accumulation_output_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..09a5acd6798b6d59660e73418745df1d352ac65d --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob007_data_accumulation_output_test.sv @@ -0,0 +1,235 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [7:0] data_in, + output logic valid_a, + output logic ready_b + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + data_in = 0; + valid_a = 0; + ready_b = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test first group of 4 values: 1, 2, 3, 4 (sum = 10) + valid_a <= 1; + data_in <= 8'd1; @(posedge clk); + data_in <= 8'd2; @(posedge clk); + data_in <= 8'd3; @(posedge clk); + data_in <= 8'd4; @(posedge clk); + + data_in <= 8'd5; @(posedge clk); + @(posedge clk); + @(posedge clk); + ready_b <= 1; + + data_in <= 8'd6; @(posedge clk); + data_in <= 8'd7; @(posedge clk); + data_in <= 8'd8; @(posedge clk); + + // Test with valid_a low (should ignore data) + valid_a <= 0; + data_in <= 8'd10; @(posedge clk); + data_in <= 8'd20; @(posedge clk); + + // Test with ready_b low (backpressure) + valid_a <= 1; + ready_b <= 0; + data_in <= 8'd1; @(posedge clk); + data_in <= 8'd2; @(posedge clk); + + // Test third group: 10, 20, 30, 40 (sum = 100) + ready_b <= 1; + data_in <= 8'd10; @(posedge clk); + data_in <= 8'd20; @(posedge clk); + data_in <= 8'd30; @(posedge clk); + data_in <= 8'd40; @(posedge clk); + + // Random testing + repeat(300) @(posedge clk or negedge clk) begin + valid_a <= $random; + ready_b <= $random; + data_in <= $random % 256; + end + + // === Additional checks: asynchronous active-low reset in mid-operation === + // The quoted code resets data_cnt, valid_b and data_out on negedge rst_n; earlier stimulus only + // resets at start-up. rst_n changes between clock edges here, so no edge race. + @(posedge clk); + valid_a <= 1; ready_b <= 0; + data_in <= 8'd200; @(posedge clk); + data_in <= 8'd201; @(posedge clk); + data_in <= 8'd202; @(posedge clk); + data_in <= 8'd203; @(posedge clk); // group complete, valid_b held high by ready_b=0 + data_in <= 8'd99; @(posedge clk); + #2 rst_n <= 0; // reset while valid_b=1 and data_out != 0 + repeat(3) @(posedge clk); + #2 rst_n <= 1; + @(posedge clk); + ready_b <= 1; + data_in <= 8'd7; @(posedge clk); + data_in <= 8'd9; @(posedge clk); // mid-group (data_cnt = 2) + #2 rst_n <= 0; // reset in the middle of a group + @(posedge clk); + #2 rst_n <= 1; + repeat(200) @(posedge clk or negedge clk) begin + valid_a <= $random; + ready_b <= $random; + data_in <= $random % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_ready_a; + int errortime_ready_a; + int errors_valid_b; + int errortime_valid_b; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [7:0] data_in; + logic valid_a; + logic ready_b; + logic ready_a_ref; + logic ready_a_dut; + logic valid_b_ref; + logic valid_b_dut; + logic [9:0] data_out_ref; + logic [9:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data_in, valid_a, ready_b, ready_a_ref, ready_a_dut, valid_b_ref, valid_b_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .data_in, + .valid_a, + .ready_b ); + + RefModule good1 ( + .clk, + .rst_n, + .data_in, + .valid_a, + .ready_b, + .ready_a(ready_a_ref), + .valid_b(valid_b_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .data_in, + .valid_a, + .ready_b, + .ready_a(ready_a_dut), + .valid_b(valid_b_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_ready_a) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "ready_a", stats1.errors_ready_a, stats1.errortime_ready_a); + else $display("Hint: Output '%s' has no mismatches.", "ready_a"); + if (stats1.errors_valid_b) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_b", stats1.errors_valid_b, stats1.errortime_valid_b); + else $display("Hint: Output '%s' has no mismatches.", "valid_b"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { ready_a_ref, valid_b_ref, data_out_ref } === ( { ready_a_ref, valid_b_ref, data_out_ref } ^ { ready_a_dut, valid_b_dut, data_out_dut } ^ { ready_a_ref, valid_b_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (ready_a_ref !== ( ready_a_ref ^ ready_a_dut ^ ready_a_ref )) + begin if (stats1.errors_ready_a == 0) stats1.errortime_ready_a = $time; + stats1.errors_ready_a = stats1.errors_ready_a+1'b1; end + if (valid_b_ref !== ( valid_b_ref ^ valid_b_dut ^ valid_b_ref )) + begin if (stats1.errors_valid_b == 0) stats1.errortime_valid_b = $time; + stats1.errors_valid_b = stats1.errors_valid_b+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob008_non-integer_data_width_conversion_24to128_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob008_non-integer_data_width_conversion_24to128_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..1c25a04ef3d6fcbc62c31b7e893509ecb93f4d98 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob008_non-integer_data_width_conversion_24to128_prompt.txt @@ -0,0 +1,78 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_in + - input data_in (24 bits) + - output reg valid_out + - output reg data_out (128 bits) + +The module should implement a data width conversion circuit that converts +24-bit input data into 128-bit output data. The circuit uses valid-only +handshake mechanism for both input and output interfaces. + +The data width conversion should work as follows: +- Input interface: Uses valid-only handshake mechanism + - valid_in indicates the validity of input data_in +- Output interface: Uses valid-only handshake mechanism + - valid_out indicates the validity of output data_out + +Data conversion process: +- The module receives 24-bit input data_in when valid_in is high +- The module accumulates multiple 24-bit inputs to form a 128-bit output +- The first received 24-bit data should be placed in the high bits of data_out +- The last received 24-bit data should be placed in the low bits of data_out +- After accumulating enough data to form a complete 128-bit word, the module + outputs data_out with valid_out asserted + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input valid_in , + input [23:0] data_in , + + output reg valid_out , + output reg [127:0] data_out +); + reg [3:0] cnt; + reg [127:0] data_lock; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~valid_in? cnt:cnt+1; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_out <= 0; + else + valid_out <= (cnt==5 || cnt==10 || cnt==15) || valid_in; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_lock <= 0; + else + data_lock <= valid_in? {data_lock[103:0], data_in}: data_lock; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else if(cnt==5) + data_out <= valid_in? {data_lock[119:0], data_in[23:16]}: data_out; + else if(cnt==10) + data_out <= valid_in? {data_lock[111:0], data_in[23: 8]}: data_out; + else if(cnt==15) + data_out <= valid_in? {data_lock[103:0], data_in[23: 0]}: data_out; + else + data_out <= data_out; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob008_non-integer_data_width_conversion_24to128_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob008_non-integer_data_width_conversion_24to128_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..86db2f4a5ed196af303085b2266739c89c840a47 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob008_non-integer_data_width_conversion_24to128_ref.sv @@ -0,0 +1,48 @@ +`timescale 1ns/1ns + +module RefModule( + input clk , + input rst_n , + input valid_in , + input [23:0] data_in , + + output reg valid_out , + output reg [127:0] data_out +); + reg [3:0] cnt; + reg [127:0] data_lock; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~valid_in? cnt:cnt+1; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_out <= 0; + else + valid_out <= (cnt==5 || cnt==10 || cnt==15)&&valid_in; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_lock <= 0; + else + data_lock <= valid_in? {data_lock[103:0], data_in}: data_lock; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else if(cnt==5) + data_out <= valid_in? {data_lock[119:0], data_in[23:16]}: data_out; + else if(cnt==10) + data_out <= valid_in? {data_lock[111:0], data_in[23: 8]}: data_out; + else if(cnt==15) + data_out <= valid_in? {data_lock[103:0], data_in[23: 0]}: data_out; + else + data_out <= data_out; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob008_non-integer_data_width_conversion_24to128_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob008_non-integer_data_width_conversion_24to128_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..7256496acfb63697009cadc94c0c275adccae24e --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob008_non-integer_data_width_conversion_24to128_test.sv @@ -0,0 +1,190 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_in, + output logic [23:0] data_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_in = 0; + data_in = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test first group: 6 x 24-bit inputs to form 128-bit output + // Inputs: 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666 + valid_in <= 1; + data_in <= 24'h111111; @(posedge clk); + data_in <= 24'h222222; @(posedge clk); + data_in <= 24'h333333; @(posedge clk); + data_in <= 24'h444444; @(posedge clk); + data_in <= 24'h555555; @(posedge clk); + data_in <= 24'h666666; @(posedge clk); + + // Test second group: 6 x 24-bit inputs + data_in <= 24'h777777; @(posedge clk); + data_in <= 24'h888888; @(posedge clk); + data_in <= 24'h999999; @(posedge clk); + data_in <= 24'haaaaaa; @(posedge clk); + data_in <= 24'hbbbbbb; @(posedge clk); + data_in <= 24'hcccccc; @(posedge clk); + + // Test with valid_in low (should ignore data) + valid_in <= 0; + data_in <= 24'hdddddd; @(posedge clk); + data_in <= 24'heeeeee; @(posedge clk); + + // Test third group: 6 x 24-bit inputs + valid_in <= 1; + data_in <= 24'hffffff; @(posedge clk); + data_in <= 24'h000000; @(posedge clk); + data_in <= 24'h123456; @(posedge clk); + data_in <= 24'h789abc; @(posedge clk); + data_in <= 24'hdef012; @(posedge clk); + data_in <= 24'h345678; @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + valid_in <= $random; + data_in <= $random % (1<<24); + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_valid_out; + int errortime_valid_out; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_in; + logic [23:0] data_in; + logic valid_out_ref; + logic valid_out_dut; + logic [127:0] data_out_ref; + logic [127:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_in, data_in, valid_out_ref, valid_out_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_in, + .data_in ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_valid_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_out", stats1.errors_valid_out, stats1.errortime_valid_out); + else $display("Hint: Output '%s' has no mismatches.", "valid_out"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { valid_out_ref, data_out_ref } === ( { valid_out_ref, data_out_ref } ^ { valid_out_dut, data_out_dut } ^ { valid_out_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (valid_out_ref !== ( valid_out_ref ^ valid_out_dut ^ valid_out_ref )) + begin if (stats1.errors_valid_out == 0) stats1.errortime_valid_out = $time; + stats1.errors_valid_out = stats1.errors_valid_out+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob009_non-integer_data_width_conversion_8to12_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob009_non-integer_data_width_conversion_8to12_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..4985a3e794c5f2efd2d3f8426b49730b25096ff2 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob009_non-integer_data_width_conversion_8to12_prompt.txt @@ -0,0 +1,80 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_in + - input data_in (8 bits) + - output valid_out + - output data_out (12 bits) + +The module should implement a data width conversion circuit that converts +8-bit input data into 12-bit output data. The circuit uses valid-only +handshake mechanism for both input and output interfaces. + +The data width conversion should work as follows: +- Input interface: Uses valid-only handshake mechanism + - valid_in indicates the validity of input data_in +- Output interface: Uses valid-only handshake mechanism + - valid_out indicates the validity of output data_out + +Data conversion process: +- The module receives 8-bit input data_in when valid_in is high +- The module accumulates two 8-bit inputs to form a 12-bit output +- The first received 8-bit data should be placed in the high bits of data_out +- The second received 8-bit data should be placed in the low bits of data_out +- After receiving exactly 2 input data values, the module outputs data_out + with valid_out asserted in the next clock cycle +- If only one data input is received, the module waits for the second input + before producing any output + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [11:0] data_out +); +reg [7:0] data_lock; +reg [1:0] valid_cnt ; +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_lock <= 'd0; + else if(valid_in ) + data_lock <= data_in; +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_cnt <= 'd0; + else if(!valid_in)begin + if(valid_cnt == 2'd2) + valid_cnt <= 2'd0; + else + valid_cnt <= valid_cnt + 1'd1; + end +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + valid_out <= 1'd1; + else if(valid_in && valid_cnt == 2'd2) + valid_out <= 1'd1; + else + valid_out <= 'd0; +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + data_out <= {data_lock, data_in[7:4]}; + else if(valid_in && valid_cnt == 2'd2) + data_out <= {data_lock[3:0], data_in}; +end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob009_non-integer_data_width_conversion_8to12_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob009_non-integer_data_width_conversion_8to12_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..67250e6ddee50ae7ab11c617b4762a2b2e50c1b5 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob009_non-integer_data_width_conversion_8to12_ref.sv @@ -0,0 +1,47 @@ + +module RefModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [11:0] data_out +); +reg [7:0] data_lock; +reg [1:0] valid_cnt ; +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_lock <= 'd0; + else if(valid_in ) + data_lock <= data_in; +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_cnt <= 'd0; + else if(valid_in)begin + if(valid_cnt == 2'd2) + valid_cnt <= 2'd0; + else + valid_cnt <= valid_cnt + 1'd1; + end +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + valid_out <= 1'd1; + else if(valid_in && valid_cnt == 2'd2) + valid_out <= 1'd1; + else + valid_out <= 'd0; +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + data_out <= {data_lock, data_in[7:4]}; + else if(valid_in && valid_cnt == 2'd2) + data_out <= {data_lock[3:0], data_in}; +end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob009_non-integer_data_width_conversion_8to12_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob009_non-integer_data_width_conversion_8to12_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..a315d0b0eea4b39be3d668ee20bc0b5a129b657d --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob009_non-integer_data_width_conversion_8to12_test.sv @@ -0,0 +1,205 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_in, + output logic [7:0] data_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_in = 0; + data_in = 0; + repeat(3) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test first pair: 0xAA, 0x55 (should form 0xAA5) + valid_in = 1; + data_in = 8'hAA; @(posedge clk); + data_in = 8'h55; @(posedge clk); + + // Test second pair: 0x12, 0x34 (should form 0x123) + data_in = 8'h12; @(posedge clk); + data_in = 8'h34; @(posedge clk); + + // Test with valid_in low (should ignore data) + valid_in = 0; + data_in = 8'hFF; @(posedge clk); + data_in = 8'hEE; @(posedge clk); + + // Test third pair: 0x78, 0x9A (should form 0x789) + valid_in = 1; + data_in = 8'h78; @(posedge clk); + data_in = 8'h9A; @(posedge clk); + + // Test fourth pair: 0xBC, 0xDE (should form 0xBCD) + data_in = 8'hBC; @(posedge clk); + data_in = 8'hDE; @(posedge clk); + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + valid_in <= $random; + data_in <= $random % 256; + end + + // === Additional checks: asynchronous active-low reset in mid-operation === + // The quoted code clears data_lock, valid_cnt, valid_out and data_out on negedge rst_n; earlier + // stimulus only resets at start-up. Inputs and rst_n change 2 time units after posedge (no edge race). + @(posedge clk); #2; + valid_in <= 1; data_in <= 8'h3C; + @(posedge clk); #2 data_in <= 8'hA7; // valid_out=1 with data_out != 0 after this edge + @(posedge clk); #2 valid_in <= 0; + rst_n <= 0; // reset while valid_out=1, data_out != 0, valid_cnt = 2 + repeat(2) @(posedge clk); + #2 rst_n <= 1; + @(posedge clk); #2; + valid_in <= 1; data_in <= 8'h5A; + @(posedge clk); #2 valid_in <= 0; // one byte received (valid_cnt = 1) + rst_n <= 0; // reset mid-group + @(posedge clk); #2 rst_n <= 1; + valid_in <= 1; data_in <= 8'hC3; + @(posedge clk); #2 data_in <= 8'h81; + @(posedge clk); #2 data_in <= 8'h18; + @(posedge clk); #2 valid_in <= 0; + repeat(100) @(posedge clk, negedge clk) begin + valid_in <= $random; + data_in <= $random % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_valid_out; + int errortime_valid_out; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_in; + logic [7:0] data_in; + logic valid_out_ref; + logic valid_out_dut; + logic [11:0] data_out_ref; + logic [11:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_in, data_in, valid_out_ref, valid_out_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_in, + .data_in ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_valid_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_out", stats1.errors_valid_out, stats1.errortime_valid_out); + else $display("Hint: Output '%s' has no mismatches.", "valid_out"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { valid_out_ref, data_out_ref } === ( { valid_out_ref, data_out_ref } ^ { valid_out_dut, data_out_dut } ^ { valid_out_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (valid_out_ref !== ( valid_out_ref ^ valid_out_dut ^ valid_out_ref )) + begin if (stats1.errors_valid_out == 0) stats1.errortime_valid_out = $time; + stats1.errors_valid_out = stats1.errors_valid_out+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob010_integer_multiple_data_bit_width_conversion_8to16_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob010_integer_multiple_data_bit_width_conversion_8to16_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..2d656db7739fb73708776b7354968e475e9aa29a --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob010_integer_multiple_data_bit_width_conversion_8to16_prompt.txt @@ -0,0 +1,74 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_in + - input data_in (8 bits) + - output reg valid_out + - output reg data_out (16 bits) + +The module should implement a data width conversion circuit that converts +8-bit input data into 16-bit output data. The circuit uses valid-only +handshake mechanism for both input and output interfaces. + +The data width conversion should work as follows: +- Input interface: Uses valid-only handshake mechanism + - valid_in indicates the validity of input data_in +- Output interface: Uses valid-only handshake mechanism + - valid_out indicates the validity of output data_out + +Data conversion process: +- The module receives 8-bit input data_in when valid_in is high +- The module accumulates two 8-bit inputs to form a 16-bit output +- The first received 8-bit data should be placed in the high 8 bits of data_out +- The second received 8-bit data should be placed in the low 8 bits of data_out +- After receiving exactly 2 input data values, the module outputs data_out + with valid_out asserted in the next clock cycle +- If only one data input is received, the module waits for the second input + before producing any output + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [15:0] data_out +); + reg [7:0] data_r; + reg flag; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + flag <= 0; + else + flag <= valid_in? ~flag: flag; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_r <= 0; + else + data_r <= !valid_in? data_in: data_r; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else + data_out <= flag&&valid_in? {data_r, data_in}: data_out; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_out <= 0; + else + valid_out <= flag&&valid_in; + end +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob010_integer_multiple_data_bit_width_conversion_8to16_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob010_integer_multiple_data_bit_width_conversion_8to16_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..871ba50d8fffed3bb0f14adbfb85fad0277d59a1 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob010_integer_multiple_data_bit_width_conversion_8to16_ref.sv @@ -0,0 +1,40 @@ +module RefModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [15:0] data_out +); + reg [7:0] data_r; + reg flag; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + flag <= 0; + else + flag <= valid_in? ~flag: flag; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_r <= 0; + else + data_r <= valid_in? data_in: data_r; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else + data_out <= flag&&valid_in? {data_r, data_in}: data_out; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_out <= 0; + else + valid_out <= flag&&valid_in; + end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob010_integer_multiple_data_bit_width_conversion_8to16_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob010_integer_multiple_data_bit_width_conversion_8to16_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..799c6e6a4757f8af250d91797e0be99a8d88a037 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob010_integer_multiple_data_bit_width_conversion_8to16_test.sv @@ -0,0 +1,181 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_in, + output logic [7:0] data_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_in = 0; + data_in = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test first pair: 0xAA, 0x55 (should form 0xAA55) + valid_in = 1; + data_in = 8'hAA; @(posedge clk); + data_in = 8'h55; @(posedge clk); + + // Test second pair: 0x12, 0x34 (should form 0x1234) + data_in = 8'h12; @(posedge clk); + data_in = 8'h34; @(posedge clk); + + // Test with valid_in low (should ignore data) + valid_in = 0; + data_in = 8'hFF; @(posedge clk); + data_in = 8'hEE; @(posedge clk); + + // Test third pair: 0x78, 0x9A (should form 0x789A) + valid_in = 1; + data_in = 8'h78; @(posedge clk); + data_in = 8'h9A; @(posedge clk); + + // Test fourth pair: 0xBC, 0xDE (should form 0xBCDE) + data_in = 8'hBC; @(posedge clk); + data_in = 8'hDE; @(posedge clk); + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + valid_in <= $random; + data_in <= $urandom % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_valid_out; + int errortime_valid_out; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_in; + logic [7:0] data_in; + logic valid_out_ref; + logic valid_out_dut; + logic [15:0] data_out_ref; + logic [15:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_in, data_in, valid_out_ref, valid_out_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_in, + .data_in ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_valid_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_out", stats1.errors_valid_out, stats1.errortime_valid_out); + else $display("Hint: Output '%s' has no mismatches.", "valid_out"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { valid_out_ref, data_out_ref } === ( { valid_out_ref, data_out_ref } ^ { valid_out_dut, data_out_dut } ^ { valid_out_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (valid_out_ref !== ( valid_out_ref ^ valid_out_dut ^ valid_out_ref )) + begin if (stats1.errors_valid_out == 0) stats1.errortime_valid_out = $time; + stats1.errors_valid_out = stats1.errors_valid_out+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob011_4-bit_carry_look-ahead_adder_circuit_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob011_4-bit_carry_look-ahead_adder_circuit_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..91d9f42fb28827658555111b540665ca2a369cec --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob011_4-bit_carry_look-ahead_adder_circuit_prompt.txt @@ -0,0 +1,45 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input A_in (4 bits) + - input B_in (4 bits) + - input C_1 + - output S (4 bits) + - output CO + +Implement a 4-bit carry look-ahead adder using gate-level description in +Verilog. The logical expressions for the carry look-ahead adder are as +follows: + +Intermediate variables: G_i = A_i AND B_i, P_i = A_i XOR B_i + +Sum: S_i = P_i XOR C_(i-1), Carry: C_i = G_i OR (P_i AND C_(i-1)) + +Where C_i and C_(i-1) represent carry signals at positions i and i-1. + +The module should be implemented using gate-level primitives (AND, OR, XOR) +rather than behavioral descriptions. All signals should be declared as wire +type. + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input [3:0] A_in , + input [3:0] B_in , + input C_1 , + + output wire CO , + output wire [3:0] S +); + wire [3:0] G,P,C; + assign P=A_in^B_in; + assign G=A_in^B_in; + + assign C={G[3]|(P[3]&C[2]), G[2]|(P[2]&C[1]), G[1]|(P[1]&C[0]), G[0]|(P[0]&C_1)}; + assign S={P[3]^C[2], P[2]^C[1], P[1]^C[0], P[0]^C_1}; + assign CO=C[3]; + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob011_4-bit_carry_look-ahead_adder_circuit_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob011_4-bit_carry_look-ahead_adder_circuit_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..c104cd362510376fdde777a47447ac151aa3f397 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob011_4-bit_carry_look-ahead_adder_circuit_ref.sv @@ -0,0 +1,18 @@ + +module RefModule( + input [3:0] A_in , + input [3:0] B_in , + input C_1 , + + output wire CO , + output wire [3:0] S +); + wire [3:0] G,P,C; + assign P=A_in^B_in; + assign G=A_in&B_in; + + assign C={G[3]|(P[3]&C[2]), G[2]|(P[2]&C[1]), G[1]|(P[1]&C[0]), G[0]|(P[0]&C_1)}; + assign S={P[3]^C[2], P[2]^C[1], P[1]^C[0], P[0]^C_1}; + assign CO=C[3]; + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob011_4-bit_carry_look-ahead_adder_circuit_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob011_4-bit_carry_look-ahead_adder_circuit_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..01cb3f2bee979edffaa00c96aa755bdf89161b11 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob011_4-bit_carry_look-ahead_adder_circuit_test.sv @@ -0,0 +1,220 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic [3:0] A_in, + output logic [3:0] B_in, + output logic C_1 + // === End your code here === +); + // === Start your code here === + initial begin + A_in <= 0; + B_in <= 0; + C_1 <= 0; + + // Test basic addition cases + @(posedge clk); + A_in <= 4'b0001; + B_in <= 4'b0001; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0010; + B_in <= 4'b0011; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0100; + B_in <= 4'b0100; + C_1 <= 1; + @(posedge clk); + + A_in <= 4'b1000; + B_in <= 4'b1000; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b1111; + B_in <= 4'b0001; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b1111; + B_in <= 4'b1111; + C_1 <= 1; + @(posedge clk); + + A_in <= 4'b1010; + B_in <= 4'b0101; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0110; + B_in <= 4'b1001; + C_1 <= 1; + @(posedge clk); + + // Test edge cases + A_in <= 4'b0000; + B_in <= 4'b0000; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0000; + B_in <= 4'b0000; + C_1 <= 1; + @(posedge clk); + + A_in <= 4'b1111; + B_in <= 4'b0000; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0000; + B_in <= 4'b1111; + C_1 <= 1; + @(posedge clk); + + // Random test sequence + repeat(200) @(posedge clk) begin + A_in <= $random & 15; // 4-bit random + B_in <= $random & 15; // 4-bit random + C_1 <= $random & 1; // 1-bit random + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_S; + int errortime_S; + int errors_CO; + int errortime_CO; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic [3:0] A_in; + logic [3:0] B_in; + logic C_1; + logic [3:0] S_ref; + logic [3:0] S_dut; + logic CO_ref; + logic CO_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, A_in, B_in, C_1, S_ref, S_dut, CO_ref, CO_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .A_in, + .B_in, + .C_1 + ); + + RefModule good1 ( + .A_in, + .B_in, + .C_1, + .CO(CO_ref), + .S(S_ref) + ); + + TopModule top_module1 ( + .A_in, + .B_in, + .C_1, + .CO(CO_dut), + .S(S_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_S) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "S", stats1.errors_S, stats1.errortime_S); + else $display("Hint: Output '%s' has no mismatches.", "S"); + if (stats1.errors_CO) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "CO", stats1.errors_CO, stats1.errortime_CO); + else $display("Hint: Output '%s' has no mismatches.", "CO"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { S_ref, CO_ref } === ( { S_ref, CO_ref } ^ { S_dut, CO_dut } ^ { S_ref, CO_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (S_ref !== ( S_ref ^ S_dut ^ S_ref )) + begin if (stats1.errors_S == 0) stats1.errortime_S = $time; + stats1.errors_S = stats1.errors_S+1'b1; end + if (CO_ref !== ( CO_ref ^ CO_dut ^ CO_ref )) + begin if (stats1.errors_CO == 0) stats1.errortime_CO = $time; + stats1.errors_CO = stats1.errors_CO+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob013_least_common_multiple_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob013_least_common_multiple_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..a794aeb88ee388935e90cff3c6094eeba11feae0 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob013_least_common_multiple_prompt.txt @@ -0,0 +1,124 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input A (8 bits) + - input B (8 bits) + - input vld_in + - output lcm_out (16 bits) + - output mcd_out (8 bits) + - output vld_out + +Design a sequential circuit that takes two unsigned numbers as input and +outputs their least common multiple (LCM) and greatest common divisor (GCD). +The bit width can be determined by parameter DATA_W. For this implementation, +DATA_W is 8, so A, B, and mcd_out are 8 bits while lcm_out is 16 bits. + +The circuit should operate as follows: +- When vld_in is asserted, the input data A and B are valid and should be + processed +- The circuit computes both the LCM and GCD of the two input numbers +- When the computation is complete, vld_out should be asserted to indicate + that the output data is valid +- Before the output is valid, lcm_out should be driven to high impedance + ('hz). This is required in this design; otherwise there will be a problem +- The rst_n signal is an active-low reset signal + +Assume all sequential logic is triggered on the positive edge of the clock. + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule#( +parameter DATA_W = 8) +( +input [DATA_W-1:0] A, +input [DATA_W-1:0] B, +input vld_in, +input rst_n, +input clk, +output wire [DATA_W*2-1:0] lcm_out, +output wire [DATA_W-1:0] mcd_out, +output reg vld_out +); + reg [DATA_W*2-1:0] A_reg; + reg [DATA_W*2-1:0] B_reg; + reg [DATA_W*2-1:0] mcd_out_r1; + reg [DATA_W*2-1:0] mult_reg; + reg [1:0] c_state, n_state; + parameter IDLE = 'd0, lcm1 = 'd1, Finish = 'd2; + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) c_state <= IDLE; + else c_state <= n_state; + end + + always @(*) begin + case(c_state) + IDLE : if(vld_in) n_state = lcm1; + else n_state = IDLE; + lcm1 : if(A_reg == B_reg) n_state = Finish; + else n_state = lcm1; + Finish : n_state = IDLE; + default : n_state = IDLE; + endcase + end + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + else begin + case(c_state) + IDLE : + if(vld_in) begin + A_reg <= A; + B_reg <= B; + mult_reg <= A * B; + mcd_out_r1 <= 0; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + mult_reg <= mult_reg; + mcd_out_r1 <= 0; + end + lcm1 : + if(A_reg > B_reg) begin + A_reg <= A_reg - B_reg; + B_reg <= B_reg; + end + else if(A_reg < B_reg) begin + B_reg <= B_reg + A_reg; + A_reg <= A_reg; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + end + Finish : mcd_out_r1 <= A_reg; + + default : begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + endcase + end + end + assign mcd_out = mcd_out_r1; + assign lcm_out = (mcd_out_r1 == 'd0) ? 'hz : (mult_reg / mcd_out_r1); // If the GCD register is 0, output high impedance, otherwise the circuit will have problems + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) vld_out <= 1'b0; + else if(c_state == Finish) vld_out <= 1'b1; + else vld_out <= 1'b0; + end + +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob013_least_common_multiple_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob013_least_common_multiple_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..c6c378d54ca460b83b669849fc4f89e7a2d7b746 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob013_least_common_multiple_ref.sv @@ -0,0 +1,92 @@ + +module RefModule#( +parameter DATA_W = 8) +( +input [DATA_W-1:0] A, +input [DATA_W-1:0] B, +input vld_in, +input rst_n, +input clk, +output wire [DATA_W*2-1:0] lcm_out, +output wire [DATA_W-1:0] mcd_out, +output reg vld_out +); + reg [DATA_W*2-1:0] A_reg; + reg [DATA_W*2-1:0] B_reg; + reg [DATA_W*2-1:0] mcd_out_r1; + reg [DATA_W*2-1:0] mult_reg; + reg [1:0] c_state, n_state; + parameter IDLE = 'd0, lcm1 = 'd1, Finish = 'd2; + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) c_state <= IDLE; + else c_state <= n_state; + end + + always @(*) begin + case(c_state) + IDLE : if(vld_in) n_state = lcm1; + else n_state = IDLE; + lcm1 : if(A_reg == B_reg) n_state = Finish; + else n_state = lcm1; + Finish : n_state = IDLE; + default : n_state = IDLE; + endcase + end + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + else begin + case(c_state) + IDLE : + if(vld_in) begin + A_reg <= A; + B_reg <= B; + mult_reg <= A * B; + mcd_out_r1 <= 0; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + mult_reg <= mult_reg; + mcd_out_r1 <= 0; + end + lcm1 : + if(A_reg > B_reg) begin + A_reg <= A_reg - B_reg; + B_reg <= B_reg; + end + else if(A_reg < B_reg) begin + B_reg <= B_reg - A_reg; + A_reg <= A_reg; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + end + Finish : mcd_out_r1 <= A_reg; + + default : begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + endcase + end + end + assign mcd_out = mcd_out_r1; + assign lcm_out = (mcd_out_r1 == 'd0) ? 'hz : (mult_reg / mcd_out_r1); // If the GCD register is 0, output high impedance, otherwise the circuit will have problems + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) vld_out <= 1'b0; + else if(c_state == Finish) vld_out <= 1'b1; + else vld_out <= 1'b0; + end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob013_least_common_multiple_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob013_least_common_multiple_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..ca87e1e8e7186a4ecb91b6bfe6e51215fbae87f6 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob013_least_common_multiple_test.sv @@ -0,0 +1,237 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [7:0] A, + output logic [7:0] B, + output logic vld_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n <= 0; + A <= 0; + B <= 0; + vld_in <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic cases + A <= 8'd12; + B <= 8'd8; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); // Wait for computation + + A <= 8'd15; + B <= 8'd25; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd7; + B <= 8'd11; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd16; + B <= 8'd24; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + + // Test edge cases + A <= 8'd1; + B <= 8'd1; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd255; + B <= 8'd1; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd0; + B <= 8'd5; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + // Random test sequence + repeat(50) @(posedge clk) begin + A <= $random & 255; // 8-bit random + B <= $random & 255; // 8-bit random + vld_in <= $random & 1; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_lcm_out; + int errortime_lcm_out; + int errors_mcd_out; + int errortime_mcd_out; + int errors_vld_out; + int errortime_vld_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [7:0] A; + logic [7:0] B; + logic vld_in; + logic [15:0] lcm_out_ref; + logic [15:0] lcm_out_dut; + logic [7:0] mcd_out_ref; + logic [7:0] mcd_out_dut; + logic vld_out_ref; + logic vld_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, A, B, vld_in, lcm_out_ref, lcm_out_dut, mcd_out_ref, mcd_out_dut, vld_out_ref, vld_out_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .A, + .B, + .vld_in + ); + + RefModule good1 ( + .A, + .B, + .vld_in, + .rst_n, + .clk, + .lcm_out(lcm_out_ref), + .mcd_out(mcd_out_ref), + .vld_out(vld_out_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .A, + .B, + .vld_in, + .lcm_out(lcm_out_dut), + .mcd_out(mcd_out_dut), + .vld_out(vld_out_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_lcm_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "lcm_out", stats1.errors_lcm_out, stats1.errortime_lcm_out); + else $display("Hint: Output '%s' has no mismatches.", "lcm_out"); + if (stats1.errors_mcd_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "mcd_out", stats1.errors_mcd_out, stats1.errortime_mcd_out); + else $display("Hint: Output '%s' has no mismatches.", "mcd_out"); + if (stats1.errors_vld_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "vld_out", stats1.errors_vld_out, stats1.errortime_vld_out); + else $display("Hint: Output '%s' has no mismatches.", "vld_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + // Where the reference drives lcm_out='hz (the spec requires 'hz while the output is not valid) the DUT must drive + // 'hz too; the X-only tolerance below would turn a 'z reference into 'x and reject every design. + wire lcm_ok = (lcm_out_ref === 16'bz) ? (lcm_out_dut === 16'bz) : (lcm_out_ref === ( lcm_out_ref ^ lcm_out_dut ^ lcm_out_ref )); + assign tb_match = lcm_ok && ( { mcd_out_ref, vld_out_ref } === ( { mcd_out_ref, vld_out_ref } ^ { mcd_out_dut, vld_out_dut } ^ { mcd_out_ref, vld_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (!lcm_ok) + begin if (stats1.errors_lcm_out == 0) stats1.errortime_lcm_out = $time; + stats1.errors_lcm_out = stats1.errors_lcm_out+1'b1; end + if (mcd_out_ref !== ( mcd_out_ref ^ mcd_out_dut ^ mcd_out_ref )) + begin if (stats1.errors_mcd_out == 0) stats1.errortime_mcd_out = $time; + stats1.errors_mcd_out = stats1.errors_mcd_out+1'b1; end + if (vld_out_ref !== ( vld_out_ref ^ vld_out_dut ^ vld_out_ref )) + begin if (stats1.errors_vld_out == 0) stats1.errortime_vld_out = $time; + stats1.errors_vld_out = stats1.errors_vld_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob014_sequence_generator_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob014_sequence_generator_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..b0c05f9bef35ceda514b97c3096c33d4b60977ae --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob014_sequence_generator_prompt.txt @@ -0,0 +1,43 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output data + +Implement a module that continuously outputs the sequence 001011 in a +cyclic manner. + +The circuit should generate the pattern as follows: +- Output '0', then '0', then '1', then '0', then '1', then '1' +- After outputting the last '1', the sequence repeats from the beginning +- The sequence 001011 repeats indefinitely + +The rst_n signal is an active-low reset signal. When reset is asserted, +the sequence should restart from the beginning. Assume all sequential +logic is triggered on the positive edge of the clock. + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + output reg data + ); + + reg [5:0] q; + + always@(posedge clk or negedge rst_n) + if (!rst_n) + q <= 6'b001011; + else + q <= {q[5], q[4:0]}; + + always@(posedge clk or negedge rst_n) + if (!rst_n) + data <= 1'd0; + else + data <= q[5]; +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob014_sequence_generator_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob014_sequence_generator_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..139d5513975bbb32f359727bfb417bd3d7338b02 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob014_sequence_generator_ref.sv @@ -0,0 +1,21 @@ + +module RefModule( + input clk, + input rst_n, + output reg data + ); + + reg [5:0] q; + + always@(posedge clk or negedge rst_n) + if (!rst_n) + q <= 6'b001011; + else + q <= {q[4:0],q[5]}; + + always@(posedge clk or negedge rst_n) + if (!rst_n) + data <= 1'd0; + else + data <= q[5]; +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob014_sequence_generator_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob014_sequence_generator_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..d674f42565304d0f2bcf1950261451fbe1fe02a7 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob014_sequence_generator_test.sv @@ -0,0 +1,139 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Let it run for multiple cycles + repeat(200) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_data; + int errortime_data; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic data_ref; + logic data_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data_ref, data_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n + ); + + RefModule good1 ( + .clk, + .rst_n, + .data(data_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .data(data_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_data) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data", stats1.errors_data, stats1.errortime_data); + else $display("Hint: Output '%s' has no mismatches.", "data"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { data_ref } === ( { data_ref } ^ { data_dut } ^ { data_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (data_ref !== ( data_ref ^ data_dut ^ data_ref )) + begin if (stats1.errors_data == 0) stats1.errortime_data = $time; + stats1.errors_data = stats1.errors_data+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob016_odd-number_division_with_a_duty_cycle_of_half_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob016_odd-number_division_with_a_duty_cycle_of_half_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..fa20139a2dbe66142350ba7a01a08dfcd4aebcbb --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob016_odd-number_division_with_a_duty_cycle_of_half_prompt.txt @@ -0,0 +1,82 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk_in + - input rst + - output clk_out7 + +The module should implement a clock divider that produces a 7:1 frequency +division with a 50% duty cycle output. reset is active low. + +The clock divider should work as follows: +- Input clock: clk_in (reference clock signal) +- Output clock: clk_out7 (divided clock signal) +- Division ratio: 7:1 (output frequency is 1/7 of input frequency) +- Duty cycle: 50% (output is high for half the period, low for half the period) + +Clock division behavior: +- The output clk_out7 should toggle at 1/7 the frequency of clk_in +- The output should maintain a 50% duty cycle (equal high and low times) +- The output should be synchronized to the input clock clk_in +- The division should be continuous and stable + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule + #(parameter N = 7) + ( + input wire rst , + input wire clk_in, + output wire clk_out7 + ); + + + + reg [3:0] cnt ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + cnt <= 'b0 ; + end + else if (cnt == N-1) begin + cnt <= 'b0 ; + end + else begin + cnt <= cnt + 1'b1 ; + end + end + + reg clkp ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + clkp <= 1'b0 ; + end + else if (cnt == (N>>1)) begin + clkp <= 1 ; + end + else if (cnt == N-1) begin + clkp <= 0 ; + end + end + + + reg clkn ; + always @(negedge clk_in or negedge rst) begin + if (!rst) begin + clkn <= 1'b0 ; + end + else if (cnt == (N>>1) ) begin + clkn <= 1 ; + end + else if (cnt == N-1) begin + clkn <= 0 ; + end + end + + + assign clk_out7 = clkp & clkn ; + + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob016_odd-number_division_with_a_duty_cycle_of_half_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob016_odd-number_division_with_a_duty_cycle_of_half_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..8c5863988ee32c234c730e1f807a42794cb70c74 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob016_odd-number_division_with_a_duty_cycle_of_half_ref.sv @@ -0,0 +1,56 @@ + +module RefModule + #(parameter N = 7) + ( + input wire rst , + input wire clk_in, + output wire clk_out7 + ); + + + + reg [3:0] cnt ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + cnt <= 'b0 ; + end + else if (cnt == N-1) begin + cnt <= 'b0 ; + end + else begin + cnt <= cnt + 1'b1 ; + end + end + + reg clkp ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + clkp <= 1'b0 ; + end + else if (cnt == (N>>1)) begin + clkp <= 1 ; + end + else if (cnt == N-1) begin + clkp <= 0 ; + end + end + + + reg clkn ; + always @(negedge clk_in or negedge rst) begin + if (!rst) begin + clkn <= 1'b0 ; + end + else if (cnt == (N>>1) ) begin + clkn <= 1 ; + end + else if (cnt == N-1) begin + clkn <= 0 ; + end + end + + + assign clk_out7 = clkp | clkn ; + + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob016_odd-number_division_with_a_duty_cycle_of_half_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob016_odd-number_division_with_a_duty_cycle_of_half_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..f9718e4354229f62c57df0869666fd678dadda81 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob016_odd-number_division_with_a_duty_cycle_of_half_test.sv @@ -0,0 +1,133 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test clock divider for multiple cycles + repeat(300) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_clk_out7; + int errortime_clk_out7; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic clk_out7_ref; + logic clk_out7_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, clk_out7_ref, clk_out7_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst ); + + RefModule good1 ( + .rst, + .clk_in(clk), + .clk_out7(clk_out7_ref) ); + + TopModule top_module1 ( + .clk_in(clk), + .rst, + .clk_out7(clk_out7_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_clk_out7) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "clk_out7", stats1.errors_clk_out7, stats1.errortime_clk_out7); + else $display("Hint: Output '%s' has no mismatches.", "clk_out7"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { clk_out7_ref } === ( { clk_out7_ref } ^ { clk_out7_dut } ^ { clk_out7_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (clk_out7_ref !== ( clk_out7_ref ^ clk_out7_dut ^ clk_out7_ref )) + begin if (stats1.errors_clk_out7 == 0) stats1.errortime_clk_out7 = $time; + stats1.errors_clk_out7 = stats1.errors_clk_out7+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob017_arbitrary_fractional_frequency_division_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob017_arbitrary_fractional_frequency_division_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..ac1eb21fb43a69d80c4367c6215906ae2220dc45 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob017_arbitrary_fractional_frequency_division_prompt.txt @@ -0,0 +1,89 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk_in + - input rst + - output clk_out + +The module should implement an arbitrary fractional frequency divider that +can achieve non-integer division ratios, such as 8.7x frequency division. + +The fractional frequency divider should work as follows: +- Input clock: clk_in (reference clock signal) +- Output clock: clk_out (divided clock signal) +- Division ratio: 8.7x (output frequency is 1/8.7 of input frequency) +- Reset: rst is active low + +Fractional division principle: +Since hardware cannot perform fractional operations (e.g., 2.1 clock cycles +or 3.3 registers), fractional division cannot achieve exact fractional +periods for each output clock cycle. Instead, the implementation uses +the least common multiple approach to achieve the desired average frequency. + +For 8.7x division: +- Over 10 input clock cycles, the output should produce 53 output clock + cycles to achieve an average 8.7x division ratio +- This means: 53 output cycles / 10 input cycles = 5.3x, which when + inverted gives 1/5.3 ≈ 1/8.7x frequency division +- clk_out is high for the first 4 input clock cycles of every output period + +I give u some useful parameter: +parameter M_N = 8'd87; +parameter c89 = 8'd24; // 8/9 clock switching point +parameter div_e = 5'd8; //even period +parameter div_o = 5'd9; //odd period + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input wire clk_in, + input wire rst, + output wire clk_out +); + parameter M_N = 8'd87; + parameter c89 = 8'd24; + parameter div_e = 5'd8; + parameter div_o = 5'd9; +//*************code***********// + reg [3:0] clk_cnt; + reg [6:0] cyc_cnt; + reg div_flag; + reg clk_out_r; + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_cnt <= 0; + else if(~div_flag) + clk_cnt <= clk_cnt==(div_e-1)? 0: clk_cnt+1; + else + clk_cnt <= clk_cnt==(div_o-1)? 0: clk_cnt+1; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + cyc_cnt <= 0; + else + cyc_cnt <= cyc_cnt==(M_N-1)? 0: cyc_cnt+1; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + div_flag <= 0; + else + div_flag <= cyc_cnt==(M_N-1)||cyc_cnt==(c89-1)? ~div_flag: div_flag; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_out_r <= 0; + else if(~div_flag) + clk_out_r <= clk_cnt<=((div_e>>1)+1); + else + clk_out_r <= clk_cnt<=((div_o>>2)+1); + end + + assign clk_out = clk_out_r; +//*************code***********// +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob017_arbitrary_fractional_frequency_division_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob017_arbitrary_fractional_frequency_division_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..350db63a669018308988298e429b2b98dd23d2f0 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob017_arbitrary_fractional_frequency_division_ref.sv @@ -0,0 +1,51 @@ + +module RefModule( + input wire clk_in, + input wire rst, + output wire clk_out +); + parameter M_N = 8'd87; + parameter c89 = 8'd24; // 8/9 clock switching point + parameter div_e = 5'd8; //Even period + parameter div_o = 5'd9; //Odd period +//*************code***********// + reg [3:0] clk_cnt; + reg [6:0] cyc_cnt; + reg div_flag; + reg clk_out_r; + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_cnt <= 0; + else if(~div_flag) + clk_cnt <= clk_cnt==(div_e-1)? 0: clk_cnt+1; + else + clk_cnt <= clk_cnt==(div_o-1)? 0: clk_cnt+1; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + cyc_cnt <= 0; + else + cyc_cnt <= cyc_cnt==(M_N-1)? 0: cyc_cnt+1; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + div_flag <= 0; + else + div_flag <= cyc_cnt==(M_N-1)||cyc_cnt==(c89-1)? ~div_flag: div_flag; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_out_r <= 0; + else if(~div_flag) + clk_out_r <= clk_cnt<=((div_e>>2)+1); + else + clk_out_r <= clk_cnt<=((div_o>>2)+1); + end + + assign clk_out = clk_out_r; +//*************code***********// +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob017_arbitrary_fractional_frequency_division_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob017_arbitrary_fractional_frequency_division_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..999415b04af6fa5676ee8d641f6deb0976eb13e9 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob017_arbitrary_fractional_frequency_division_test.sv @@ -0,0 +1,134 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test fractional frequency divider for multiple cycles + // The divider should achieve 8.7x division ratio + repeat(1000) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_clk_out; + int errortime_clk_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic clk_out_ref; + logic clk_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, clk_out_ref, clk_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst ); + + RefModule good1 ( + .clk_in(clk), + .rst, + .clk_out(clk_out_ref) ); + + TopModule top_module1 ( + .clk_in(clk), + .rst, + .clk_out(clk_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_clk_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "clk_out", stats1.errors_clk_out, stats1.errortime_clk_out); + else $display("Hint: Output '%s' has no mismatches.", "clk_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { clk_out_ref } === ( { clk_out_ref } ^ { clk_out_dut } ^ { clk_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (clk_out_ref !== ( clk_out_ref ^ clk_out_dut ^ clk_out_ref )) + begin if (stats1.errors_clk_out == 0) stats1.errortime_clk_out = $time; + stats1.errors_clk_out = stats1.errors_clk_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob019_implement_full_subtractor_using_three_to_eight_decoder_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob019_implement_full_subtractor_using_three_to_eight_decoder_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..574d60acb2f1292ea820de027a7f86935a33e79c --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob019_implement_full_subtractor_using_three_to_eight_decoder_prompt.txt @@ -0,0 +1,211 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input A + - input B + - input Ci + - output D + - output Co + +Implement a full subtractor using a 3-to-8 decoder and necessary logic +gates. In the full subtractor: A is the minuend (the number being +subtracted from), B is the subtrahend (the number being subtracted), Ci +is the borrow input from the lower bit position, D is the difference, and +Co is the borrow output to the higher bit position. + +You are provided with the following 3-to-8 decoder module code, which you +should include and instantiate in your solution: + +``` +module decoder_38( + input E , + input A0 , + input A1 , + input A2 , + + output reg Y0n , + output reg Y1n , + output reg Y2n , + output reg Y3n , + output reg Y4n , + output reg Y5n , + output reg Y6n , + output reg Y7n +); + +always @(*)begin + if(!E)begin + Y0n = 1'b1; + Y1n = 1'b1; + Y2n = 1'b1; + Y3n = 1'b1; + Y4n = 1'b1; + Y5n = 1'b1; + Y6n = 1'b1; + Y7n = 1'b1; + end + else begin + case({A2,A1,A0}) + 3'b000 : begin + Y0n = 1'b0; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b001 : begin + Y0n = 1'b1; Y1n = 1'b0; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b010 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b0; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b011 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b0; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b100 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b0; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b101 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b0; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b110 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b0; Y7n = 1'b1; + end + 3'b111 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b0; + end + default: begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + endcase + end +end + +endmodule +``` + +You should instantiate this decoder module in your implementation and use +it along with logic gates to implement the full subtractor functionality. +The decoder outputs are active-low, where only one output is low (0) at a +time based on the input address. + + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input A , + input B , + input Ci , + + output wire D , + output wire Co +); + +wire Y0_n ; +wire Y1_n ; +wire Y2_n ; +wire Y3_n ; +wire Y4_n ; +wire Y5_n ; +wire Y6_n ; +wire Y7_n ; + +decoder_38 U0( + .E (1'b1), + .A0 (Ci ), + .A1 (B ), + .A2 (A ), + + .Y0n (Y0_n), + .Y1n (Y1_n), + .Y2n (Y2_n), + .Y3n (Y3_n), + .Y4n (Y4_n), + .Y5n (Y5_n), + .Y6n (Y6_n), + .Y7n (Y7_n) +); + +assign D = ~(Y1_n & Y2_n | Y4_n & Y7_n); +assign Co = ~(Y1_n & Y2_n & Y3_n & Y7_n); + +endmodule + +module decoder_38( + input E , + input A0 , + input A1 , + input A2 , + + output reg Y0n , + output reg Y1n , + output reg Y2n , + output reg Y3n , + output reg Y4n , + output reg Y5n , + output reg Y6n , + output reg Y7n +); + +always @(*)begin + if(!E)begin + Y0n = 1'b1; + Y1n = 1'b1; + Y2n = 1'b1; + Y3n = 1'b1; + Y4n = 1'b1; + Y5n = 1'b1; + Y6n = 1'b1; + Y7n = 1'b1; + end + else begin + case({A2,A1,A0}) + 3'b000 : begin + Y0n = 1'b0; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b001 : begin + Y0n = 1'b1; Y1n = 1'b0; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b010 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b0; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b011 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b0; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b100 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b0; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b101 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b0; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b110 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b0; Y7n = 1'b1; + end + 3'b111 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b0; + end + default: begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + endcase + end +end + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob019_implement_full_subtractor_using_three_to_eight_decoder_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob019_implement_full_subtractor_using_three_to_eight_decoder_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..cfe1f270a91fa303a28afb6e33951c980b8b1eee --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob019_implement_full_subtractor_using_three_to_eight_decoder_ref.sv @@ -0,0 +1,41 @@ + +module RefModule( + input A , + input B , + input Ci , + + output wire D , + output wire Co +); + +wire Y0_n ; +wire Y1_n ; +wire Y2_n ; +wire Y3_n ; +wire Y4_n ; +wire Y5_n ; +wire Y6_n ; +wire Y7_n ; + +decoder_38 U0( + .E (1'b1), + .A0 (Ci ), + .A1 (B ), + .A2 (A ), + + .Y0n (Y0_n), + .Y1n (Y1_n), + .Y2n (Y2_n), + .Y3n (Y3_n), + .Y4n (Y4_n), + .Y5n (Y5_n), + .Y6n (Y6_n), + .Y7n (Y7_n) +); + +assign D = ~(Y1_n & Y2_n & Y4_n & Y7_n); +assign Co = ~(Y1_n & Y2_n & Y3_n & Y7_n); + +endmodule + +// decoder_38 module is in prompt file. \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob019_implement_full_subtractor_using_three_to_eight_decoder_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob019_implement_full_subtractor_using_three_to_eight_decoder_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..7d73473e5e674a64e29c7e9848b6f1e24b1481bb --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob019_implement_full_subtractor_using_three_to_eight_decoder_test.sv @@ -0,0 +1,178 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic A, + output logic B, + output logic Ci + // === End your code here === +); + // === Start your code here === + initial begin + A <= 0; + B <= 0; + Ci <= 0; + + // Test all 8 possible combinations + @(posedge clk); + A <= 0; B <= 0; Ci <= 0; + @(posedge clk); + + A <= 0; B <= 0; Ci <= 1; + @(posedge clk); + + A <= 0; B <= 1; Ci <= 0; + @(posedge clk); + + A <= 0; B <= 1; Ci <= 1; + @(posedge clk); + + A <= 1; B <= 0; Ci <= 0; + @(posedge clk); + + A <= 1; B <= 0; Ci <= 1; + @(posedge clk); + + A <= 1; B <= 1; Ci <= 0; + @(posedge clk); + + A <= 1; B <= 1; Ci <= 1; + @(posedge clk); + + repeat(20) @(posedge clk); + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_D; + int errortime_D; + int errors_Co; + int errortime_Co; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic A; + logic B; + logic Ci; + logic D_ref; + logic D_dut; + logic Co_ref; + logic Co_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, A, B, Ci, D_ref, D_dut, Co_ref, Co_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .A, + .B, + .Ci + ); + + RefModule good1 ( + .A, + .B, + .Ci, + .D(D_ref), + .Co(Co_ref) + ); + + TopModule top_module1 ( + .A, + .B, + .Ci, + .D(D_dut), + .Co(Co_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_D) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "D", stats1.errors_D, stats1.errortime_D); + else $display("Hint: Output '%s' has no mismatches.", "D"); + if (stats1.errors_Co) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "Co", stats1.errors_Co, stats1.errortime_Co); + else $display("Hint: Output '%s' has no mismatches.", "Co"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { D_ref, Co_ref } === ( { D_ref, Co_ref } ^ { D_dut, Co_dut } ^ { D_ref, Co_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (D_ref !== ( D_ref ^ D_dut ^ D_ref )) + begin if (stats1.errors_D == 0) stats1.errortime_D = $time; + stats1.errors_D = stats1.errors_D+1'b1; end + if (Co_ref !== ( Co_ref ^ Co_dut ^ Co_ref )) + begin if (stats1.errors_Co == 0) stats1.errortime_Co = $time; + stats1.errors_Co = stats1.errors_Co+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob021_asynchronous_FIFO_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob021_asynchronous_FIFO_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..6bac9a395dde95ee04c0eceafec04ffe1ab4714b --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob021_asynchronous_FIFO_prompt.txt @@ -0,0 +1,209 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input wclk + - input rclk + - input wrstn + - input rrstn + - input winc + - input rinc + - input wdata (WIDTH bits) + - output wfull + - output rempty + - output rdata (WIDTH bits) + +The module should implement an asynchronous FIFO (First-In-First-Out) buffer +with parameterizable width and depth. The FIFO uses a dual-port RAM for +data storage and implements proper read/write pointer management with +clock domain crossing. + +The asynchronous FIFO should work as follows: +- Write interface: Uses wclk domain with winc (write enable) and wdata +- Read interface: Uses rclk domain with rinc (read enable) and rdata +- Reset: wrstn (write domain reset) and rrstn (read domain reset) +- Status signals: wfull (write full) and rempty (read empty) + +FIFO operation: +- Data is written when winc is high and wfull is low +- Data is read when rinc is high and rempty is low +- wfull indicates the FIFO is full and cannot accept more writes +- rempty indicates the FIFO is empty and has no data to read +- The FIFO should handle clock domain crossing properly + + +Dual-port RAM module (provided): +```verilog +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk +,input wenc +,input [$clog2(DEPTH)-1:0] waddr +,input [WIDTH-1:0] wdata +,input rclk +,input renc +,input [$clog2(DEPTH)-1:0] raddr +,output reg [WIDTH-1:0] rdata +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin +if(wenc) +RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin +if(renc) +rdata <= RAM_MEM[raddr]; +end + +endmodule +``` + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input wclk , + input rclk , + input wrstn , + input rrstn , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wire wfull , + output wire rempty , + output wire [WIDTH-1:0] rdata +); + +parameter ADDR_WIDTH = $clog2(DEPTH); + +/**********************addr bin gen*************************/ +reg [ADDR_WIDTH:0] waddr_bin; +reg [ADDR_WIDTH:0] raddr_bin; + +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + waddr_bin <= 'd0; + end + else if(!wfull && winc)begin + waddr_bin <= waddr_bin + 1'd1; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + raddr_bin <= 'd0; + end + else if(!rempty && rinc)begin + raddr_bin <= raddr_bin + 1'd1; + end +end + +/**********************addr gray gen*************************/ +wire [ADDR_WIDTH:0] waddr_gray; +wire [ADDR_WIDTH:0] raddr_gray; +reg [ADDR_WIDTH:0] wptr; +reg [ADDR_WIDTH:0] rptr; +assign waddr_gray = waddr_bin ^ (waddr_bin>>1); +assign raddr_gray = raddr_bin ^ (raddr_bin>>1); +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + wptr <= 'd0; + end + else begin + wptr <= waddr_gray; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + rptr <= 'd0; + end + else begin + rptr <= raddr_gray; + end +end +/**********************syn addr gray*************************/ +reg [ADDR_WIDTH:0] wptr_buff; +reg [ADDR_WIDTH:0] wptr_syn; +reg [ADDR_WIDTH:0] rptr_buff; +reg [ADDR_WIDTH:0] rptr_syn; +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + rptr_buff <= 'd0; + rptr_syn <= 'd0; + end + else begin + rptr_buff <= rptr; + rptr_syn <= rptr_buff; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + wptr_buff <= 'd0; + wptr_syn <= 'd0; + end + else begin + wptr_buff <= wptr; + wptr_syn <= wptr_buff; + end +end +/**********************full empty gen*************************/ +assign wfull = (wptr == {~rptr_syn[ADDR_WIDTH:ADDR_WIDTH-1],rptr_syn[ADDR_WIDTH-2:0]}); +assign rempty = (rptr == wptr_syn); + +/**********************RAM*************************/ +wire wen ; +wire ren ; +wire wren;//high write +wire [ADDR_WIDTH-1:0] waddr; +wire [ADDR_WIDTH-1:0] raddr; +assign wen = winc & !wfull; +assign ren = rinc & rempty; +assign waddr = waddr_bin[ADDR_WIDTH-1:0]; +assign raddr = raddr_bin[ADDR_WIDTH-1:0]; + +dual_port_RAM #(.DEPTH(DEPTH), + .WIDTH(WIDTH) +)dual_port_RAM( + .wclk (wclk), + .wenc (wen), + .waddr(waddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .wdata(wdata), //Data write + .rclk (rclk), + .renc (ren), + .raddr(raddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .rdata(rdata) //Data output +); + +endmodule + +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk + ,input wenc + ,input [$clog2(DEPTH)-1:0] waddr //Take log2 of depth to get address bit width. + ,input [WIDTH-1:0] wdata //Data write + ,input rclk + ,input renc + ,input [$clog2(DEPTH)-1:0] raddr //Take log2 of depth to get address bit width. + ,output reg [WIDTH-1:0] rdata //Data output +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin + if(wenc) + RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin + if(renc) + rdata <= RAM_MEM[raddr]; +end + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob021_asynchronous_FIFO_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob021_asynchronous_FIFO_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..90a3a5e5a7986918edcab7156f41d6e8c4e9d5d7 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob021_asynchronous_FIFO_ref.sv @@ -0,0 +1,147 @@ + +module RefModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input wclk , + input rclk , + input wrstn , + input rrstn , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wire wfull , + output wire rempty , + output wire [WIDTH-1:0] rdata +); + +parameter ADDR_WIDTH = $clog2(DEPTH); + +/**********************addr bin gen*************************/ +reg [ADDR_WIDTH:0] waddr_bin; +reg [ADDR_WIDTH:0] raddr_bin; + +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + waddr_bin <= 'd0; + end + else if(!wfull && winc)begin + waddr_bin <= waddr_bin + 1'd1; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + raddr_bin <= 'd0; + end + else if(!rempty && rinc)begin + raddr_bin <= raddr_bin + 1'd1; + end +end + +/**********************addr gray gen*************************/ +wire [ADDR_WIDTH:0] waddr_gray; +wire [ADDR_WIDTH:0] raddr_gray; +reg [ADDR_WIDTH:0] wptr; +reg [ADDR_WIDTH:0] rptr; +assign waddr_gray = waddr_bin ^ (waddr_bin>>1); +assign raddr_gray = raddr_bin ^ (raddr_bin>>1); +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + wptr <= 'd0; + end + else begin + wptr <= waddr_gray; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + rptr <= 'd0; + end + else begin + rptr <= raddr_gray; + end +end +/**********************syn addr gray*************************/ +reg [ADDR_WIDTH:0] wptr_buff; +reg [ADDR_WIDTH:0] wptr_syn; +reg [ADDR_WIDTH:0] rptr_buff; +reg [ADDR_WIDTH:0] rptr_syn; +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + rptr_buff <= 'd0; + rptr_syn <= 'd0; + end + else begin + rptr_buff <= rptr; + rptr_syn <= rptr_buff; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + wptr_buff <= 'd0; + wptr_syn <= 'd0; + end + else begin + wptr_buff <= wptr; + wptr_syn <= wptr_buff; + end +end +/**********************full empty gen*************************/ +assign wfull = (wptr == {~rptr_syn[ADDR_WIDTH:ADDR_WIDTH-1],rptr_syn[ADDR_WIDTH-2:0]}); +assign rempty = (rptr == wptr_syn); + +/**********************RAM*************************/ +wire wen ; +wire ren ; +wire wren;//high write +wire [ADDR_WIDTH-1:0] waddr; +wire [ADDR_WIDTH-1:0] raddr; +assign wen = winc & !wfull; +assign ren = rinc & !rempty; +assign waddr = waddr_bin[ADDR_WIDTH-1:0]; +assign raddr = raddr_bin[ADDR_WIDTH-1:0]; + +// Additional check: RefModule uses its own copy of the spec's provided dual_port_RAM, so the design's RAM is +// checked against the spec instead of being shared with the reference. +RefModule_dual_port_RAM #(.DEPTH(DEPTH), + .WIDTH(WIDTH) +)dual_port_RAM( + .wclk (wclk), + .wenc (wen), + .waddr(waddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .wdata(wdata), //Data write + .rclk (rclk), + .renc (ren), + .raddr(raddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .rdata(rdata) //Data output +); + +endmodule + +// Verbatim copy of the dual_port_RAM given on the spec (renamed so it cannot collide with the design's copy). +module RefModule_dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk + ,input wenc + ,input [$clog2(DEPTH)-1:0] waddr //Take log2 of depth to get address bit width. + ,input [WIDTH-1:0] wdata //Data write + ,input rclk + ,input renc + ,input [$clog2(DEPTH)-1:0] raddr //Take log2 of depth to get address bit width. + ,output reg [WIDTH-1:0] rdata //Data output +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin + if(wenc) + RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin + if(renc) + rdata <= RAM_MEM[raddr]; +end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob021_asynchronous_FIFO_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob021_asynchronous_FIFO_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..200af4ab99380bed3386e07eb16fdd1d6502c518 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob021_asynchronous_FIFO_test.sv @@ -0,0 +1,281 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic wclk, + output logic rclk, + output logic wrstn, + output logic rrstn, + output logic winc, + output logic rinc, + output logic [7:0] wdata + // === End your code here === +); + // === Start your code here === + reg wclk_reg = 0; + reg rclk_reg = 0; + assign wclk = wclk_reg; + assign rclk = rclk_reg; + + initial forever #10 wclk_reg = ~wclk_reg; // 50MHz write clock + initial forever #15 rclk_reg = ~rclk_reg; // 33.3MHz read clock + + initial begin + wrstn = 0; + rrstn = 0; + winc = 0; + rinc = 0; + wdata = 0; + repeat(20) @(posedge wclk); @(posedge rclk); + wrstn <= 1; + rrstn <= 1; + @(posedge wclk); @(posedge rclk); + + // Test basic write and read operations + winc <= 1; + wdata <= 8'hAA; @(posedge wclk); + wdata <= 8'h55; @(posedge wclk); + wdata <= 8'h33; @(posedge wclk); + wdata <= 8'hCC; @(posedge wclk); + winc <= 0; + @(posedge wclk); + + // Test read operations + rinc <= 1; + repeat(4) @(posedge rclk); + rinc <= 0; + @(posedge rclk); + + // Test simultaneous write and read + winc <= 1; + rinc <= 1; + wdata <= 8'h11; @(posedge wclk); @(posedge rclk); + wdata <= 8'h22; @(posedge wclk); @(posedge rclk); + wdata <= 8'h44; @(posedge wclk); @(posedge rclk); + wdata <= 8'h88; @(posedge wclk); @(posedge rclk); + winc <= 0; + rinc <= 0; + @(posedge wclk); + + // Test FIFO full condition + winc <= 1; + wdata <= 8'hF0; @(posedge wclk); + wdata <= 8'hF1; @(posedge wclk); + wdata <= 8'hF2; @(posedge wclk); + wdata <= 8'hF3; @(posedge wclk); + wdata <= 8'hF4; @(posedge wclk); + wdata <= 8'hF5; @(posedge wclk); + wdata <= 8'hF6; @(posedge wclk); + wdata <= 8'hF7; @(posedge wclk); + wdata <= 8'hF8; @(posedge wclk); + wdata <= 8'hF9; @(posedge wclk); + wdata <= 8'hFA; @(posedge wclk); + wdata <= 8'hFB; @(posedge wclk); + wdata <= 8'hFC; @(posedge wclk); + wdata <= 8'hFD; @(posedge wclk); + wdata <= 8'hFE; @(posedge wclk); + wdata <= 8'hFF; @(posedge wclk); + winc <= 0; + @(posedge wclk); + + // Test FIFO empty condition + rinc <= 1; + repeat(20) @(posedge rclk); // Should trigger rempty + rinc <= 0; + @(posedge rclk); + + // Random testing + repeat(200) @(posedge wclk, posedge rclk) begin + winc <= $random; + rinc <= $random; + wdata <= $random % 256; + end + + // Additional checks: both resets asserted mid-run with data in the FIFO. The given code resets + // the pointers, gray pointers and synchronisers asynchronously (wrstn / rrstn), so the FIFO must + // read as empty and not full right away and restart from address 0 after the release. Resets + // change 3 ps after a wclk edge (never on a wclk/rclk edge or a sample point). + for (int r = 0; r < 2; r++) begin + @(posedge wclk) begin winc <= 1; rinc <= 0; end + repeat (5 + 4 * r) @(posedge wclk) wdata <= $random % 256; + @(posedge wclk) winc <= 0; + @(posedge wclk); + #3 begin wrstn = 0; rrstn = 0; end + repeat (3) @(posedge rclk); + @(posedge wclk); + #3 begin wrstn = 1; rrstn = 1; end + repeat (2) @(posedge rclk); + // refill, drain, then random traffic + @(posedge wclk) winc <= 1; + repeat (6) @(posedge wclk) wdata <= $random % 256; + @(posedge wclk) winc <= 0; + repeat (4) @(posedge rclk); + @(posedge rclk) rinc <= 1; + repeat (10) @(posedge rclk); + @(posedge rclk) rinc <= 0; + repeat (150) @(posedge wclk, posedge rclk) begin + winc <= $random; + rinc <= $random; + wdata <= $random % 256; + end + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_wfull; + int errortime_wfull; + int errors_rempty; + int errortime_rempty; + int errors_rdata; + int errortime_rdata; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic wclk; + logic rclk; + logic wrstn; + logic rrstn; + logic winc; + logic rinc; + logic [7:0] wdata; + logic wfull_ref; + logic rempty_ref; + logic [7:0] rdata_ref; + logic wfull_dut; + logic rempty_dut; + logic [7:0] rdata_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, wclk, rclk, wrstn, rrstn, winc, rinc, wdata, wfull_ref, rempty_ref, rdata_ref, wfull_dut, rempty_dut, rdata_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .wclk, + .rclk, + .wrstn, + .rrstn, + .winc, + .rinc, + .wdata ); + + RefModule good1 ( + .wclk, + .rclk, + .wrstn, + .rrstn, + .winc, + .rinc, + .wdata, + .wfull(wfull_ref), + .rempty(rempty_ref), + .rdata(rdata_ref) ); + + TopModule top_module1 ( + .wclk, + .rclk, + .wrstn, + .rrstn, + .winc, + .rinc, + .wdata, + .wfull(wfull_dut), + .rempty(rempty_dut), + .rdata(rdata_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_wfull) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "wfull", stats1.errors_wfull, stats1.errortime_wfull); + else $display("Hint: Output '%s' has no mismatches.", "wfull"); + if (stats1.errors_rempty) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rempty", stats1.errors_rempty, stats1.errortime_rempty); + else $display("Hint: Output '%s' has no mismatches.", "rempty"); + if (stats1.errors_rdata) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rdata", stats1.errors_rdata, stats1.errortime_rdata); + else $display("Hint: Output '%s' has no mismatches.", "rdata"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { wfull_ref, rempty_ref, rdata_ref } === ( { wfull_ref, rempty_ref, rdata_ref } ^ { wfull_dut, rempty_dut, rdata_dut } ^ { wfull_ref, rempty_ref, rdata_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (wfull_ref !== ( wfull_ref ^ wfull_dut ^ wfull_ref )) + begin if (stats1.errors_wfull == 0) stats1.errortime_wfull = $time; + stats1.errors_wfull = stats1.errors_wfull+1'b1; end + if (rempty_ref !== ( rempty_ref ^ rempty_dut ^ rempty_ref )) + begin if (stats1.errors_rempty == 0) stats1.errortime_rempty = $time; + stats1.errors_rempty = stats1.errors_rempty+1'b1; end + if (rdata_ref !== ( rdata_ref ^ rdata_dut ^ rdata_ref )) + begin if (stats1.errors_rdata == 0) stats1.errortime_rdata = $time; + stats1.errors_rdata = stats1.errors_rdata+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob022_synchronous_FIFO_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob022_synchronous_FIFO_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..27da48e33175874e2e482d78352136285fd7e6e7 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob022_synchronous_FIFO_prompt.txt @@ -0,0 +1,156 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input winc + - input rinc + - input wdata (WIDTH bits) + - output wfull + - output rempty + - output rdata (WIDTH bits) + +The module should implement a synchronous FIFO (First In, First Out) buffer +that uses a dual-port RAM for data storage. The FIFO should be parameterizable +in both WIDTH and DEPTH, with configurable bit WIDTH and DEPTH parameters. + +The synchronous FIFO should work as follows: +- Input interface: + - clk: Single clock signal for both read and write operations + - rst_n: Active low reset signal + - winc: Write enable signal (pulse to write data) + - rinc: Read enable signal (pulse to read data) + - wdata: 8-bit input data to be written +- Output interface: + - wfull: FIFO full flag (high when FIFO is full) + - rempty: FIFO empty flag (high when FIFO is empty) + - rdata: 8-bit output data read from FIFO + +FIFO operation: +- Write operations occur on the positive edge of clk when winc is high +- Read operations occur on the positive edge of clk when rinc is high +- The FIFO uses a dual-port RAM for data storage +- Write and read pointers are managed to track FIFO status +- Full and empty conditions are detected based on pointer comparison +- The FIFO should handle simultaneous read and write operations correctly + +Dual-port RAM module (provided): +``` +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk +,input wenc +,input [$clog2(DEPTH)-1:0] waddr +,input [WIDTH-1:0] wdata +,input rclk +,input renc +,input [$clog2(DEPTH)-1:0] raddr +,output reg [WIDTH-1:0] rdata +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin + if(wenc) + RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin + if(renc) + rdata <= RAM_MEM[raddr]; +end + +endmodule +``` + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input clk , + input rst_n , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wfull , + output rempty , + output wire [WIDTH-1:0] rdata +); + +localparam DP_WD = $clog2(DEPTH); + +reg [DP_WD :0]waddr; +wire wenc; +wire waddr_d_h; +wire [DP_WD -1:0]waddr_d_l; +assign wenc = winc & wfull; +assign waddr_d_h = (waddr[DP_WD-1:0] == DEPTH-1) ? ~waddr[DP_WD] : waddr[DP_WD]; +assign waddr_d_l = (waddr[DP_WD-1:0] == DEPTH-1) ? 0 : waddr[DP_WD-1:0] + 1; +always @(posedge clk or negedge rst_n)begin + if(~rst_n) waddr <= 0; + else if(wenc) waddr <= {waddr_d_h, waddr_d_l}; +end + +reg [DP_WD :0]raddr; +wire renc; +wire raddr_d_h; +wire [DP_WD -1:0]raddr_d_l; +assign renc = rinc & (!rempty); +assign raddr_d_h = (raddr[DP_WD-1:0] == DEPTH-1) ? ~raddr[DP_WD] : raddr[DP_WD]; +assign raddr_d_l = (raddr[DP_WD-1:0] == DEPTH-1) ? 0 : raddr[DP_WD-1:0] + 1; +always @(posedge clk or negedge rst_n)begin + if(~rst_n) raddr <= 0; + else if(renc) raddr <= {raddr_d_h, raddr_d_l}; +end + +wire [DP_WD :0]fifo_cnt = (waddr[DP_WD] == raddr[DP_WD]) ? waddr[DP_WD-1:0] - raddr[DP_WD-1:0]: + (waddr[DP_WD-1:0] + DEPTH - raddr[DP_WD-1:0]); + +assign rempty = (fifo_cnt == 0); +assign wfull = (fifo_cnt == DEPTH); + +dual_port_RAM #(.DEPTH(DEPTH), .WIDTH(WIDTH)) +u_ram ( + .wclk (clk), + .wenc (wenc), + .waddr (waddr[$clog2(DEPTH)-1:0]), + .wdata (wdata), + .rclk (clk), + .renc (renc), + .raddr (raddr[$clog2(DEPTH)-1:0]), + .rdata (rdata) +); + +endmodule + +/**********************************RAM************************************/ +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk + ,input wenc + ,input [$clog2(DEPTH)-1:0] waddr //Take log2 of depth to get address bit width. + ,input [WIDTH-1:0] wdata //Data write + ,input rclk + ,input renc + ,input [$clog2(DEPTH)-1:0] raddr //Take log2 of depth to get address bit width. + ,output reg [WIDTH-1:0] rdata //Data output +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin + if(wenc) + RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin + if(renc) + rdata <= RAM_MEM[raddr]; +end + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob022_synchronous_FIFO_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob022_synchronous_FIFO_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..b3194008457a02e29fc6d3a7d787b6c008fbb7da --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob022_synchronous_FIFO_ref.sv @@ -0,0 +1,61 @@ + +module RefModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input clk , + input rst_n , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wfull , + output rempty , + output wire [WIDTH-1:0] rdata +); + +localparam DP_WD = $clog2(DEPTH); + +reg [DP_WD :0]waddr; +wire wenc; +wire waddr_d_h; +wire [DP_WD -1:0]waddr_d_l; +assign wenc = winc & (!wfull); +assign waddr_d_h = (waddr[DP_WD-1:0] == DEPTH-1) ? ~waddr[DP_WD] : waddr[DP_WD]; +assign waddr_d_l = (waddr[DP_WD-1:0] == DEPTH-1) ? 0 : waddr[DP_WD-1:0] + 1; +always @(posedge clk or negedge rst_n)begin + if(~rst_n) waddr <= 0; + else if(wenc) waddr <= {waddr_d_h, waddr_d_l}; +end + +reg [DP_WD :0]raddr; +wire renc; +wire raddr_d_h; +wire [DP_WD -1:0]raddr_d_l; +assign renc = rinc & (!rempty); +assign raddr_d_h = (raddr[DP_WD-1:0] == DEPTH-1) ? ~raddr[DP_WD] : raddr[DP_WD]; +assign raddr_d_l = (raddr[DP_WD-1:0] == DEPTH-1) ? 0 : raddr[DP_WD-1:0] + 1; +always @(posedge clk or negedge rst_n)begin + if(~rst_n) raddr <= 0; + else if(renc) raddr <= {raddr_d_h, raddr_d_l}; +end + +wire [DP_WD :0]fifo_cnt = (waddr[DP_WD] == raddr[DP_WD]) ? waddr[DP_WD-1:0] - raddr[DP_WD-1:0]: + (waddr[DP_WD-1:0] + DEPTH - raddr[DP_WD-1:0]); + +assign rempty = (fifo_cnt == 0); +assign wfull = (fifo_cnt == DEPTH); + +dual_port_RAM #(.DEPTH(DEPTH), .WIDTH(WIDTH)) +u_ram ( + .wclk (clk), + .wenc (wenc), + .waddr (waddr[$clog2(DEPTH)-1:0]), + .wdata (wdata), + .rclk (clk), + .renc (renc), + .raddr (raddr[$clog2(DEPTH)-1:0]), + .rdata (rdata) +); + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob022_synchronous_FIFO_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob022_synchronous_FIFO_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..ce3e41170df8cd9e2bb3160877e307951fb99970 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob022_synchronous_FIFO_test.sv @@ -0,0 +1,229 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic winc, + output logic rinc, + output logic [7:0] wdata + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + winc = 0; + rinc = 0; + wdata = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic write operations + winc <= 1; + wdata <= 8'hAA; @(posedge clk); + wdata <= 8'h55; @(posedge clk); + wdata <= 8'h33; @(posedge clk); + wdata <= 8'hCC; @(posedge clk); + winc <= 0; + @(posedge clk); + + // Test basic read operations + rinc <= 1; + repeat(4) @(posedge clk); + rinc <= 0; + @(posedge clk); + + // Test simultaneous write and read + winc <= 1; + rinc <= 1; + wdata <= 8'h11; @(posedge clk); + wdata <= 8'h22; @(posedge clk); + wdata <= 8'h44; @(posedge clk); + wdata <= 8'h88; @(posedge clk); + winc <= 0; + rinc <= 0; + @(posedge clk); + + // Test FIFO full condition + winc <= 1; + wdata <= 8'hF0; @(posedge clk); + wdata <= 8'hF1; @(posedge clk); + wdata <= 8'hF2; @(posedge clk); + wdata <= 8'hF3; @(posedge clk); + wdata <= 8'hF4; @(posedge clk); + wdata <= 8'hF5; @(posedge clk); + wdata <= 8'hF6; @(posedge clk); + wdata <= 8'hF7; @(posedge clk); + wdata <= 8'hF8; @(posedge clk); + wdata <= 8'hF9; @(posedge clk); + wdata <= 8'hFA; @(posedge clk); + wdata <= 8'hFB; @(posedge clk); + wdata <= 8'hFC; @(posedge clk); + wdata <= 8'hFD; @(posedge clk); + wdata <= 8'hFE; @(posedge clk); + wdata <= 8'hFF; @(posedge clk); // Should trigger wfull + winc <= 0; + @(posedge clk); + + // Test FIFO empty condition + rinc <= 1; + repeat(20) @(posedge clk); // Should trigger rempty + rinc <= 0; + @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + winc <= $random; + rinc <= $random; + wdata <= $random % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_wfull; + int errortime_wfull; + int errors_rempty; + int errortime_rempty; + int errors_rdata; + int errortime_rdata; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic winc; + logic rinc; + logic [7:0] wdata; + logic wfull_ref; + logic rempty_ref; + logic [7:0] rdata_ref; + logic wfull_dut; + logic rempty_dut; + logic [7:0] rdata_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, winc, rinc, wdata, wfull_ref, rempty_ref, rdata_ref, wfull_dut, rempty_dut, rdata_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .winc, + .rinc, + .wdata ); + + RefModule good1 ( + .clk, + .rst_n, + .winc, + .rinc, + .wdata, + .wfull(wfull_ref), + .rempty(rempty_ref), + .rdata(rdata_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .winc, + .rinc, + .wdata, + .wfull(wfull_dut), + .rempty(rempty_dut), + .rdata(rdata_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_wfull) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "wfull", stats1.errors_wfull, stats1.errortime_wfull); + else $display("Hint: Output '%s' has no mismatches.", "wfull"); + if (stats1.errors_rempty) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rempty", stats1.errors_rempty, stats1.errortime_rempty); + else $display("Hint: Output '%s' has no mismatches.", "rempty"); + if (stats1.errors_rdata) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rdata", stats1.errors_rdata, stats1.errortime_rdata); + else $display("Hint: Output '%s' has no mismatches.", "rdata"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { wfull_ref, rempty_ref, rdata_ref } === ( { wfull_ref, rempty_ref, rdata_ref } ^ { wfull_dut, rempty_dut, rdata_dut } ^ { wfull_ref, rempty_ref, rdata_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (wfull_ref !== ( wfull_ref ^ wfull_dut ^ wfull_ref )) + begin if (stats1.errors_wfull == 0) stats1.errortime_wfull = $time; + stats1.errors_wfull = stats1.errors_wfull+1'b1; end + if (rempty_ref !== ( rempty_ref ^ rempty_dut ^ rempty_ref )) + begin if (stats1.errors_rempty == 0) stats1.errortime_rempty = $time; + stats1.errors_rempty = stats1.errors_rempty+1'b1; end + if (rdata_ref !== ( rdata_ref ^ rdata_dut ^ rdata_ref )) + begin if (stats1.errors_rdata == 0) stats1.errortime_rdata = $time; + stats1.errors_rdata = stats1.errors_rdata+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob023_gray_code_counter_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob023_gray_code_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..c9b60eff7c1ae616c2bd13a2644e4d5cce200f15 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob023_gray_code_counter_prompt.txt @@ -0,0 +1,74 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output reg [3:0] gray_out + +The module should implement a 4-bit Gray code counter. Assume all sequential +logic is triggered on the positive edge of the clock. The reset input is active +low asynchronous, and should reset the counter to 0. + +The Gray code counter should work as follows: +- The counter should generate a 4-bit Gray code sequence +- The counter increments every two clock cycles +- The implementation should follow this process: + 1. Input a Gray code value (use an internal wire variable initialized to 0) + 2. Convert the Gray code to binary representation + 3. Increment the binary value by 1 + 4. Convert the incremented binary back to Gray code + 5. Output the resulting Gray code +- Gray code is a binary numeral system where two successive values differ + in only one bit position +- The counter should cycle through all 16 possible 4-bit Gray code values + and then repeat +- When rst_n is low, the counter should reset to 0000 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + + output reg [3:0] gray_out +); +//Gray code to binary conversion +reg [3:0] bin_out; +wire [3:0] gray_wire; + +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_out <= 4'b0; + end + else begin + bin_out[3] = gray_wire[3]; + bin_out[2] = gray_wire[2]^bin_out[3]; + bin_out[1] = gray_wire[1]^bin_out[2]; + bin_out[0] = gray_wire[0]^bin_out[1]; + end +end +//Binary increment +reg [3:0] bin_add_wire; +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_add_wire <= 4'b0; + end + else begin + bin_add_wire <= bin_out + 1'b1; + end +end +//Binary to gray code conversion +assign gray_wire = (bin_add_wire >> 2) ^ bin_add_wire; + +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + gray_out <= 4'b0; + end + else begin + gray_out <= gray_wire; + end +end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob023_gray_code_counter_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob023_gray_code_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..894e7d68646d0b1562e7cdf67d0a96597dac0e4f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob023_gray_code_counter_ref.sv @@ -0,0 +1,44 @@ + +module RefModule( + input clk, + input rst_n, + + output reg [3:0] gray_out +); +//Gray code to binary conversion +reg [3:0] bin_out; +wire [3:0] gray_wire; + +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_out <= 4'b0; + end + else begin + bin_out[3] = gray_wire[3]; + bin_out[2] = gray_wire[2]^bin_out[3]; + bin_out[1] = gray_wire[1]^bin_out[2]; + bin_out[0] = gray_wire[0]^bin_out[1]; + end +end +//Binary increment +reg [3:0] bin_add_wire; +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_add_wire <= 4'b0; + end + else begin + bin_add_wire <= bin_out + 1'b1; + end +end +//Binary to gray code conversion +assign gray_wire = (bin_add_wire >> 1) ^ bin_add_wire; + +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + gray_out <= 4'b0; + end + else begin + gray_out <= gray_wire; + end +end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob023_gray_code_counter_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob023_gray_code_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..71832152abeadfdf1876bddadd268eb2ce375283 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob023_gray_code_counter_test.sv @@ -0,0 +1,132 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test Gray code counter for multiple cycles + repeat(200) @(posedge clk); + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_gray_out; + int errortime_gray_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [3:0] gray_out_ref; + logic [3:0] gray_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, gray_out_ref, gray_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .gray_out(gray_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .gray_out(gray_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_gray_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "gray_out", stats1.errors_gray_out, stats1.errortime_gray_out); + else $display("Hint: Output '%s' has no mismatches.", "gray_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { gray_out_ref } === ( { gray_out_ref } ^ { gray_out_dut } ^ { gray_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (gray_out_ref !== ( gray_out_ref ^ gray_out_dut ^ gray_out_ref )) + begin if (stats1.errors_gray_out == 0) stats1.errortime_gray_out = $time; + stats1.errors_gray_out = stats1.errors_gray_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob024_multiplication_and_bitwise_operations_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob024_multiplication_and_bitwise_operations_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..60da767775ef14dab6089977a5c0e162b6b74ebd --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob024_multiplication_and_bitwise_operations_prompt.txt @@ -0,0 +1,26 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input A (8 bits) + - output B (16 bits) + +Design a computational unit circuit that multiplies A[7:0] by the constant +0xFB (binary 11111011). You should implement this using as few resources +as possible. + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input [7:0] A, + output [15:0] B + ); + +//*************code***********// + + assign B = (A<<8)-(A<<2)+A; +//*************code***********// + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob024_multiplication_and_bitwise_operations_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob024_multiplication_and_bitwise_operations_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..e4f0d68117fad73983a4ce341fc8f75e4dbc3c54 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob024_multiplication_and_bitwise_operations_ref.sv @@ -0,0 +1,12 @@ + +module RefModule( + input [7:0] A, + output [15:0] B + ); + +//*************code***********// + + assign B = (A<<8)-(A<<2)-A; +//*************code***********// + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob024_multiplication_and_bitwise_operations_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob024_multiplication_and_bitwise_operations_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..ed0a3fd059cc6bcba35d77a1f961ec9015cbb5ae --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob024_multiplication_and_bitwise_operations_test.sv @@ -0,0 +1,160 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic [7:0] A + // === End your code here === +); + // === Start your code here === + initial begin + A <= 0; + + // Test basic cases + @(posedge clk); + A <= 8'd0; + @(posedge clk); + + + A <= 8'd200; + @(posedge clk); + + A <= 8'd250; + @(posedge clk); + + A <= 8'd255; + @(posedge clk); + + // Test edge cases + A <= 8'd128; + @(posedge clk); + + A <= 8'd64; + @(posedge clk); + + A <= 8'd32; + @(posedge clk); + + A <= 8'd16; + @(posedge clk); + + // Random test sequence + repeat(200) @(posedge clk) begin + A <= $random & 255; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_B; + int errortime_B; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic [7:0] A; + logic [15:0] B_ref; + logic [15:0] B_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, A, B_ref, B_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .A + ); + + RefModule good1 ( + .A, + .B(B_ref) + ); + + TopModule top_module1 ( + .A, + .B(B_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_B) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "B", stats1.errors_B, stats1.errortime_B); + else $display("Hint: Output '%s' has no mismatches.", "B"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { B_ref } === ( { B_ref } ^ { B_dut } ^ { B_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (B_ref !== ( B_ref ^ B_dut ^ B_ref )) + begin if (stats1.errors_B == 0) stats1.errortime_B = $time; + stats1.errors_B = stats1.errors_B+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob025_pulse_synchronization_circuit_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob025_pulse_synchronization_circuit_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..7ca3a5cf541b399c7f00751f42ca9d7762038cf3 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob025_pulse_synchronization_circuit_prompt.txt @@ -0,0 +1,73 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk_fast + - input clk_slow + - input rst_n + - input data_in + - output data_out + +Extract a single clock cycle width pulse from clock domain A and establish +another single clock width pulse in a new clock domain B. + +The frequency of clock domain A is 10 times that of clock domain B. The +intervals between pulses in clock domain A are large, so there is no need +to consider the problem of pulses being too close together. + +When the fast clock domain detects an input pulse, it will toggle a flag +signal and synchronize it to the slow clock domain. After the slow clock +domain detects the change in this flag signal, it outputs a corresponding +single cycle pulse. + +The rst_n signal is an active-low asynchronous reset signal. Assume all +sequential logic is triggered on the positive edge of the clock. + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk_fast , + input clk_slow , + input rst_n , + input data_in , + + output data_out +); +reg Q_fast; +always @(posedge clk_fast or negedge rst_n) begin + if(~rst_n) begin + Q_fast <= 'd0; + end + else if(data_in)begin + Q_fast <= ~Q_fast; + end + else if(~data_in)begin + Q_fast <= Q_fast; + end +end +reg Q_buff0; +reg Q_buff1; +always @(posedge clk_slow or negedge rst_n) begin + if(~rst_n) begin + Q_buff0 <= 'd0; + Q_buff1 <= 'd0; + end + else begin + Q_buff0 <= Q_fast; + Q_buff1 <= Q_buff0; + end +end +reg Q_slow; +always @(posedge clk_slow or negedge rst_n) begin + if(~rst_n) begin + Q_slow <= 'd0; + end + else begin + Q_slow <= Q_buff1; + end +end + +assign data_out = Q_buff1 & Q_slow; +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob025_pulse_synchronization_circuit_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob025_pulse_synchronization_circuit_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..370add92fcdb39b2b3bfc5db449ea3df65945962 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob025_pulse_synchronization_circuit_ref.sv @@ -0,0 +1,45 @@ + +module RefModule( + input clk_fast , + input clk_slow , + input rst_n , + input data_in , + + output data_out +); +reg Q_fast; +always @(posedge clk_fast or negedge rst_n) begin + if(~rst_n) begin + Q_fast <= 'd0; + end + else if(data_in)begin + Q_fast <= ~Q_fast; + end + else if(~data_in)begin + Q_fast <= Q_fast; + end +end +reg Q_buff0; +reg Q_buff1; +always @(posedge clk_slow or negedge rst_n) begin + if(~rst_n) begin + Q_buff0 <= 'd0; + Q_buff1 <= 'd0; + end + else begin + Q_buff0 <= Q_fast; + Q_buff1 <= Q_buff0; + end +end +reg Q_slow; +always @(posedge clk_slow or negedge rst_n) begin + if(~rst_n) begin + Q_slow <= 'd0; + end + else begin + Q_slow <= Q_buff1; + end +end + +assign data_out = Q_buff1 ^ Q_slow; +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob025_pulse_synchronization_circuit_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob025_pulse_synchronization_circuit_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..16b80ac0342a138eada73c60b6497597f4374a04 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob025_pulse_synchronization_circuit_test.sv @@ -0,0 +1,194 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic clk_fast, + output logic clk_slow, + output logic rst_n, + output logic data_in + // === End your code here === +); + // === Start your code here === + initial begin + clk_fast <= 0; + clk_slow <= 0; + rst_n <= 0; + data_in <= 0; + + // Generate fast clock (10x faster than slow clock) + forever begin + #5 clk_fast <= ~clk_fast; + end + end + + initial begin + // Generate slow clock + forever begin + #50 clk_slow <= ~clk_slow; + end + end + + initial begin + // Reset sequence + @(posedge clk_fast); + @(posedge clk_fast); + rst_n <= 1; + @(posedge clk_fast); + + // Test basic pulse detection + data_in <= 1; + @(posedge clk_fast); + data_in <= 0; + @(posedge clk_fast); + @(posedge clk_fast); + @(posedge clk_fast); + + // Wait for slow clock to process + repeat(20) @(posedge clk_slow); + + // Test multiple pulses + data_in <= 1; + @(posedge clk_fast); + @(posedge clk_fast); + @(posedge clk_fast); + @(posedge clk_fast); + data_in <= 0; + @(posedge clk_fast); + @(posedge clk_fast); + + // Wait for slow clock to process + repeat(20) @(posedge clk_slow); + + // Random test sequence + repeat(100) @(posedge clk_fast) begin + data_in <= $random & 1; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic clk_fast; + logic clk_slow; + logic rst_n; + logic data_in; + logic data_out_ref; + logic data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, clk_fast, clk_slow, rst_n, data_in, data_out_ref, data_out_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .clk_fast, + .clk_slow, + .rst_n, + .data_in + ); + + RefModule good1 ( + .clk_fast, + .clk_slow, + .rst_n, + .data_in, + .data_out(data_out_ref) + ); + + TopModule top_module1 ( + .clk_fast, + .clk_slow, + .rst_n, + .data_in, + .data_out(data_out_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { data_out_ref } === ( { data_out_ref } ^ { data_out_dut } ^ { data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob028_up_and_down_counter_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob028_up_and_down_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..fd55dd8a34cdc1398023693725fdc69b6acc72b8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob028_up_and_down_counter_prompt.txt @@ -0,0 +1,75 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input mode + - output reg [3:0] number + - output reg zero + +The module should implement a decimal up/down counter. Assume all sequential +logic is triggered on the positive edge of the clock. The reset input is active +low asynchronous, and should reset the counter to 0, and zero is also be 0. + +The up/down counter should work as follows: +- When mode signal is 1, the counter increments (counts up) each clock cycle +- When mode signal is 0, the counter decrements (counts down) each clock cycle +- The counter output (number) is a 4-bit value representing the current count +- The zero signal is asserted (goes high) for the next cycle when the number reaches 0, and is + low at all other times +- When rst_n is low, the counter should reset to 0 + +Counter operation: +- Mode = 1: Counter increments each clock cycle (0, 1, 2, 3, ..., 9, 0, 1, ...), only up to 9 +- Mode = 0: Counter decrements each clock cycle (9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, ...) +- Zero detection: The zero signal is high for next cycle only when number equals 0 +- Reset behavior: Active low reset to 0 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input mode, + output reg [3:0]number, + output reg zero + ); + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + zero <= 1'd0; + end + else if (number == 4'd0) + begin + zero <= 1'b1; + end + else + begin + zero <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + number <= 4'b0; + end + else if(mode) + begin + if(number == 9) + number <= 0; + else + number <= number - 1'd1; + end + else if(!mode) + begin + if(number == 0) + number <= 9; + else + number <= number - 1'd1; + end + else number <= number; +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob028_up_and_down_counter_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob028_up_and_down_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..3f4a2006e216d481bf02975a552c29d203eb8309 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob028_up_and_down_counter_ref.sv @@ -0,0 +1,44 @@ + +module RefModule( + input clk, + input rst_n, + input mode, + output reg [3:0]number, + output reg zero + ); + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + zero <= 1'd0; + end + else if (number == 4'd0) + begin + zero <= 1'b1; + end + else + begin + zero <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + number <= 4'b0; + end + else if(mode) + begin + if(number == 9) + number <= 0; + else + number <= number + 1'd1; + end + else if(!mode) + begin + if(number == 0) + number <= 9; + else + number <= number - 1'd1; + end + else number <= number; +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob028_up_and_down_counter_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob028_up_and_down_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..9c8d87a866b931fe81a5cb15a4f3ba531c1391a7 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob028_up_and_down_counter_test.sv @@ -0,0 +1,165 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic mode + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + mode = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test up counting (mode = 1) + mode <= 1; + repeat(15) @(posedge clk); // Count up: 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4 + + // Test down counting (mode = 0) + mode <= 0; + repeat(15) @(posedge clk); // Count down: 5,4,3,2,1,0,9,8,7,6,5,4,3,2,1 + + // Test mode switching + mode <= 1; @(posedge clk); // Count up + mode <= 0; @(posedge clk); // Count down + mode <= 1; @(posedge clk); // Count up + mode <= 0; @(posedge clk); // Count down + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + mode <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_number; + int errortime_number; + int errors_zero; + int errortime_zero; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic mode; + logic [3:0] number_ref; + logic zero_ref; + logic [3:0] number_dut; + logic zero_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, mode, number_ref, zero_ref, number_dut, zero_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .mode ); + + RefModule good1 ( + .clk, + .rst_n, + .mode, + .number(number_ref), + .zero(zero_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .mode, + .number(number_dut), + .zero(zero_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_number) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "number", stats1.errors_number, stats1.errortime_number); + else $display("Hint: Output '%s' has no mismatches.", "number"); + if (stats1.errors_zero) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "zero", stats1.errors_zero, stats1.errortime_zero); + else $display("Hint: Output '%s' has no mismatches.", "zero"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { number_ref, zero_ref } === ( { number_ref, zero_ref } ^ { number_dut, zero_dut } ^ { number_ref, zero_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (number_ref !== ( number_ref ^ number_dut ^ number_ref )) + begin if (stats1.errors_number == 0) stats1.errortime_number = $time; + stats1.errors_number = stats1.errors_number+1'b1; end + if (zero_ref !== ( zero_ref ^ zero_dut ^ zero_ref )) + begin if (stats1.errors_zero == 0) stats1.errortime_zero = $time; + stats1.errors_zero = stats1.errors_zero+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob030_simple_implementation_RAM_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob030_simple_implementation_RAM_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..92bddb6e57e64c8f21a538ce091f96dc73b7b5a0 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob030_simple_implementation_RAM_prompt.txt @@ -0,0 +1,59 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input read_en + - input write_en + - input read_addr (8 bits) + - input write_addr (8 bits) + - input write_data (4 bits) + - output read_data (4 bits) + +Implement a dual-port RAM with depth of 8 and data width of 4 bits. All +data should be initialized to 0000. The RAM has two separate ports: one +for reading data and one for writing data. Read and write operations can +be performed simultaneously. + +When the read enable signal read_en is asserted, the module should read +data from the location specified by read_addr and output it on read_data. +When the write enable signal write_en is asserted, the module should write +write_data to the location specified by write_addr. + +The rst_n signal is an active-low asynchronous reset signal. Assume all +sequential logic is triggered on the positive edge of the clock. + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + + input write_en, + input [7:0]write_addr, + input [3:0]write_data, + + input read_en, + input [7:0]read_addr, + output reg [3:0]read_data +); + reg [3:0] myRAM [7:0]; + + reg [8:0] i; + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + for(i=0;i<256;i=i+1) + myRAM[i] <= 0; + else + myRAM[write_addr] <= !write_en? write_data: myRAM[write_addr]; + end + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + read_data <= 0; + else + read_data <= read_en? myRAM[read_addr]: read_data; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob030_simple_implementation_RAM_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob030_simple_implementation_RAM_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..c979fc6d858b9b6871182cfcc04ae6454d16a591 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob030_simple_implementation_RAM_ref.sv @@ -0,0 +1,31 @@ + +module RefModule( + input clk, + input rst_n, + + input write_en, + input [7:0]write_addr, + input [3:0]write_data, + + input read_en, + input [7:0]read_addr, + output reg [3:0]read_data +); + reg [3:0] myRAM [7:0]; + + reg [8:0] i; + // integer i; // Not synthesizable + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + for(i=0;i<256;i=i+1) + myRAM[i] <= 0; + else + myRAM[write_addr] <= write_en? write_data: myRAM[write_addr]; + end + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + read_data <= 0; + else + read_data <= read_en? myRAM[read_addr]: read_data; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob030_simple_implementation_RAM_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob030_simple_implementation_RAM_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..ac1dd5591beb9185c3f68ca4c4f392e5e4c6c7a5 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob030_simple_implementation_RAM_test.sv @@ -0,0 +1,222 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic read_en, + output logic write_en, + output logic [7:0] read_addr, + output logic [7:0] write_addr, + output logic [3:0] write_data + // === End your code here === +); + // === Start your code here === + initial begin + rst_n <= 0; + read_en <= 0; + write_en <= 0; + read_addr <= 0; + write_addr <= 0; + write_data <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic write operations + write_en <= 1; + write_addr <= 0; + write_data <= 4'b1010; + @(posedge clk); + + write_addr <= 1; + write_data <= 4'b0101; + @(posedge clk); + + write_addr <= 2; + write_data <= 4'b1111; + @(posedge clk); + + write_en <= 0; + @(posedge clk); + + // Test basic read operations + read_en <= 1; + read_addr <= 0; + @(posedge clk); // 1010 + + read_addr <= 1; + @(posedge clk); // 0101 + + read_addr <= 2; + @(posedge clk); // 1111 + + read_en <= 0; + @(posedge clk); + + // Test simultaneous read and write + read_en <= 1; + write_en <= 1; + read_addr <= 0; + write_addr <= 3; + write_data <= 4'b1100; + @(posedge clk); + + read_addr <= 1; + write_addr <= 4; + write_data <= 4'b0011; + @(posedge clk); + + read_en <= 0; + write_en <= 0; + @(posedge clk); + + // Random test sequence + repeat(200) @(posedge clk) begin + read_en <= $random & 1; + write_en <= $random & 1; + read_addr <= $random & 7; // Only use addresses 0-7 for depth 8 + write_addr <= $random & 7; + write_data <= $random & 15; // 4-bit data + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_read_data; + int errortime_read_data; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic read_en; + logic write_en; + logic [7:0] read_addr; + logic [7:0] write_addr; + logic [3:0] write_data; + logic [3:0] read_data_ref; + logic [3:0] read_data_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, read_en, write_en, read_addr, write_addr, write_data, read_data_ref, read_data_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .read_en, + .write_en, + .read_addr, + .write_addr, + .write_data + ); + + RefModule good1 ( + .clk, + .rst_n, + .write_en, + .write_addr, + .write_data, + .read_en, + .read_addr, + .read_data(read_data_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .read_en, + .write_en, + .read_addr, + .write_addr, + .write_data, + .read_data(read_data_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_read_data) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "read_data", stats1.errors_read_data, stats1.errortime_read_data); + else $display("Hint: Output '%s' has no mismatches.", "read_data"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { read_data_ref } === ( { read_data_ref } ^ { read_data_dut } ^ { read_data_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (read_data_ref !== ( read_data_ref ^ read_data_dut ^ read_data_ref )) + begin if (stats1.errors_read_data == 0) stats1.errortime_read_data = $time; + stats1.errors_read_data = stats1.errors_read_data+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob031_johnson_counter_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob031_johnson_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..5c4e0e07c42f74e5cec29a5d07ab655886fa78c3 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob031_johnson_counter_prompt.txt @@ -0,0 +1,35 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output reg [3:0] Q + +The module should implement a 4-bit Johnson counter (also known as a twisted +ring counter). Assume all sequential logic is triggered on the positive edge +of the clock. The reset input is active low asynchronous, and should reset +the counter to 0000. + +The Johnson counter should work as follows: +- The counter follows a specific sequence of states in a circular pattern +- The state sequence is: 0000 -> 1000 -> 1100 -> 1110 -> 1111 -> 0111 -> 0011 -> 0001 -> 0000 +- This creates a cycle of 8 states before repeating +- On each positive clock edge, the counter advances to the next state in the sequence +- When rst_n is low, the counter should reset to 0000 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n, + + output reg [3:0] Q +); + always@(posedge clk or negedge rst_n)begin + if(!rst_n) Q <= 'd0; + else Q <= {Q[0], Q[3 : 1]}; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob031_johnson_counter_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob031_johnson_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..1f68a1ac386fe58d6d32038dd39d05c6c255c978 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob031_johnson_counter_ref.sv @@ -0,0 +1,12 @@ + +module RefModule( + input clk , + input rst_n, + + output reg [3:0] Q +); + always@(posedge clk or negedge rst_n)begin + if(!rst_n) Q <= 'd0; + else Q <= {~Q[0], Q[3 : 1]}; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob031_johnson_counter_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob031_johnson_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..dc33439851b599a6017ec24dc89ff6eab20774ae --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob031_johnson_counter_test.sv @@ -0,0 +1,133 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test Johnson counter for multiple cycles + // The counter cycles through 8 states: 0000->1000->1100->1110->1111->0111->0011->0001->0000 + repeat(200) @(posedge clk); + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_Q; + int errortime_Q; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [3:0] Q_ref; + logic [3:0] Q_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, Q_ref, Q_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .Q(Q_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .Q(Q_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_Q) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "Q", stats1.errors_Q, stats1.errortime_Q); + else $display("Hint: Output '%s' has no mismatches.", "Q"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { Q_ref } === ( { Q_ref } ^ { Q_dut } ^ { Q_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (Q_ref !== ( Q_ref ^ Q_dut ^ Q_ref )) + begin if (stats1.errors_Q == 0) stats1.errortime_Q = $time; + stats1.errors_Q = stats1.errors_Q+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob032_pipeline_multiplier_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob032_pipeline_multiplier_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..b9e3bc61d23c76394372e71efc73f0625f09cc38 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob032_pipeline_multiplier_prompt.txt @@ -0,0 +1,68 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input [3:0] mul_a + - input [3:0] mul_b + - output reg [7:0] mul_out + +The module should implement a 4-bit unsigned pipelined multiplier. Assume all +sequential logic is triggered on the positive edge of the clock. The reset +input is active low asynchronous, and should reset the multiplier to 0. + +The pipelined multiplier should work as follows: +- The multiplier performs unsigned multiplication of two 4-bit inputs (mul_a and mul_b) +- The result is an 8-bit output (mul_out) representing the product +- The multiplication is implemented using a pipelined architecture for improved + performance and throughput +- The pipeline allows new inputs to be processed every clock cycle while + previous multiplications are still being completed +- When rst_n is low, the multiplier should reset to 0 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule#( + parameter size = 4 +)( + input clk , + input rst_n , + input [size-1:0] mul_a , + input [size-1:0] mul_b , + + output reg [size*2-1:0] mul_out +); + //parameter + parameter N = size * 2; + //defination + wire [N - 1 : 0] temp [0 : 3]; + + reg [N - 1 : 0] adder_0; + reg [N - 1 : 0] adder_1; + + //output + genvar i; + generate + for(i = 0; i < 4; i = i + 1)begin : loop + assign temp[i] = mul_b[i] ? mul_a << (i-1) : 'd0; + end + endgenerate + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_0 <= 'd0; + else adder_0 <= temp[0] + temp[1]; + end + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_1 <= 'd0; + else adder_1 <= temp[2] + temp[3]; + end + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) mul_out <= 'd0; + else mul_out <= adder_0 + adder_1; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob032_pipeline_multiplier_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob032_pipeline_multiplier_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..4686788d7af68d2fe694cf911d4d8452976defa0 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob032_pipeline_multiplier_ref.sv @@ -0,0 +1,42 @@ + +module RefModule#( + parameter size = 4 +)( + input clk , + input rst_n , + input [size-1:0] mul_a , + input [size-1:0] mul_b , + + output reg [size*2-1:0] mul_out +); + //parameter + parameter N = size * 2; + //defination + wire [N - 1 : 0] temp [0 : 3]; + + reg [N - 1 : 0] adder_0; + reg [N - 1 : 0] adder_1; + + //output + genvar i; + generate + for(i = 0; i < 4; i = i + 1)begin : loop + assign temp[i] = mul_b[i] ? mul_a << i : 'd0; + end + endgenerate + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_0 <= 'd0; + else adder_0 <= temp[0] + temp[1]; + end + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_1 <= 'd0; + else adder_1 <= temp[2] + temp[3]; + end + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) mul_out <= 'd0; + else mul_out <= adder_0 + adder_1; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob032_pipeline_multiplier_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob032_pipeline_multiplier_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..1dce2b7fad20e13c6d4e6b7019b17061acb1401f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob032_pipeline_multiplier_test.sv @@ -0,0 +1,156 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [3:0] mul_a, + output logic [3:0] mul_b + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + mul_a = 0; + mul_b = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic multiplication cases + mul_a <= 4'd2; mul_b <= 4'd3; @(posedge clk); // 2 * 3 = 6 + mul_a <= 4'd4; mul_b <= 4'd5; @(posedge clk); // 4 * 5 = 20 + mul_a <= 4'd7; mul_b <= 4'd8; @(posedge clk); // 7 * 8 = 56 + mul_a <= 4'd9; mul_b <= 4'd10; @(posedge clk); // 9 * 10 = 90 + mul_a <= 4'd12; mul_b <= 4'd13; @(posedge clk); // 12 * 13 = 156 + mul_a <= 4'd15; mul_b <= 4'd15; @(posedge clk); // 15 * 15 = 225 + + // Test edge cases + mul_a <= 4'd0; mul_b <= 4'd5; @(posedge clk); // 0 * 5 = 0 + mul_a <= 4'd3; mul_b <= 4'd0; @(posedge clk); // 3 * 0 = 0 + mul_a <= 4'd1; mul_b <= 4'd1; @(posedge clk); // 1 * 1 = 1 + mul_a <= 4'd15; mul_b <= 4'd1; @(posedge clk); // 15 * 1 = 15 + mul_a <= 4'd1; mul_b <= 4'd15; @(posedge clk); // 1 * 15 = 15 + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_mul_out; + int errortime_mul_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [3:0] mul_a; + logic [3:0] mul_b; + logic [7:0] mul_out_ref; + logic [7:0] mul_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, mul_a, mul_b, mul_out_ref, mul_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .mul_a, + .mul_b ); + + RefModule good1 ( + .clk, + .rst_n, + .mul_a, + .mul_b, + .mul_out(mul_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .mul_a, + .mul_b, + .mul_out(mul_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_mul_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "mul_out", stats1.errors_mul_out, stats1.errortime_mul_out); + else $display("Hint: Output '%s' has no mismatches.", "mul_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { mul_out_ref } === ( { mul_out_ref } ^ { mul_out_dut } ^ { mul_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (mul_out_ref !== ( mul_out_ref ^ mul_out_dut ^ mul_out_ref )) + begin if (stats1.errors_mul_out == 0) stats1.errortime_mul_out = $time; + stats1.errors_mul_out = stats1.errors_mul_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob033_traffic_lights_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob033_traffic_lights_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..c770623dd15c60d64ab30b5b3308fe844088fa17 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob033_traffic_lights_prompt.txt @@ -0,0 +1,150 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input pass_request + - output reg [7:0] clock + - output reg red + - output reg yellow + - output reg green + +The module should implement a traffic light controller with pedestrian +request functionality. Assume all sequential logic is triggered on the +positive edge of the clock. The reset input is active low asynchronous, +and should reset the traffic light to red state (see Reset behavior). + +The traffic light controller should work as follows: +- Normal operation: Traffic light cycles through red (10 clock cycles), + green (60 clock cycles), and yellow (5 clock cycles) in sequence +- Pedestrian request: When pass_request is active, if remaining green time + is greater than 10 clock cycles, it should be shortened to 10 clock + cycles; if remaining green time is 10 clock cycles or less, it remains + unchanged +- Light sequence: Red → Green → Yellow → Red (repeating) +- Clock counter: The clock output shows the countdown timer for the current + light state +- Light outputs: Only one light should be active at a time (red, yellow, + or green) + +Traffic light operation: +- Red light: 10 clock cycles, clock counts down from 10 to 1 +- Green light: 60 clock cycles normally, or 10 clock cycles if pedestrian + request is active and remaining time > 10 +- Yellow light: 5 clock cycles, clock counts down from 5 to 1 +- Pedestrian request: Shortens green time to 10 cycles if remaining time > 10 +- Reset behavior: Active low reset to red state with clock = 10; all lights + are 0 during reset and for the first two clock edges after release + (clock 9, 8); the third edge turns red on with clock = 10 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule + ( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input pass_request, + output wire[7:0]clock, + output reg red, + output reg yellow, + output reg green + ); + + parameter idle = 2'd0, + s1_red = 2'd1, + s2_green = 2'd2, + s3_yellow = 2'd3; + reg [7:0] cnt; + reg [1:0] state; + reg p_red,p_yellow,p_green; //Buffer previous signal light values for rising edge detection + + +always @(posedge clk or negedge rst_n) + begin + if(!rst_n) + begin + state <= idle; + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + end + else case(state) + idle: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + state <= s1_red; + end + s1_red: + begin + p_red <= 1'b1; + p_green <= 1'b0; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s2_green; + else + state <= s1_red; + end + s2_green: + begin + p_red <= 1'b0; + p_green <= 1'b1; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s3_yellow; + else + state <= s2_green; + end + s3_yellow: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b1; + if (cnt == 3) + state <= s1_red; + else + state <= s3_yellow; + end + endcase + end + +always @(posedge clk or negedge rst_n) + if(!rst_n) + cnt <= 7'd10; + else if (pass_request&&green&&(cnt>10)) + cnt <= 7'd10; + else if (green&&p_green) + cnt <= 7'd60; + else if (!yellow&&p_yellow) + cnt <= 7'd5; + else if (!red&&p_red) + cnt <= 7'd10; + else cnt <= cnt -1; + assign clock = cnt; + +always @(posedge clk or negedge rst_n) + if(!rst_n) + begin + yellow <= 1'd0; + red <= 1'd0; + green <= 1'd0; + end + else + begin + yellow <= p_yellow; + red <= p_red; + green <= p_green; + end + +endmodule + + +// notice: the why we set if (cnt == 3) for state transition, because at cnt 3, the next cycle +// cnt 2 the state will be updated +// cnt 1 the p_yellow, p_red, p_green will be updated +// cnt 0 the yellow, red, green will be updated +// next cycle, the traffic light will change +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob033_traffic_lights_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob033_traffic_lights_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..3f0649950bde0cffb62e5e6de99d06676e0caa8e --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob033_traffic_lights_ref.sv @@ -0,0 +1,107 @@ + +module RefModule + ( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input pass_request, + output wire[7:0]clock, + output reg red, + output reg yellow, + output reg green + ); + + parameter idle = 2'd0, + s1_red = 2'd1, + s2_green = 2'd2, + s3_yellow = 2'd3; + reg [7:0] cnt; + reg [1:0] state; + reg p_red,p_yellow,p_green; //Buffer previous signal light values for rising edge detection + + +always @(posedge clk or negedge rst_n) + begin + if(!rst_n) + begin + state <= idle; + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + end + else case(state) + idle: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + state <= s1_red; + end + s1_red: + begin + p_red <= 1'b1; + p_green <= 1'b0; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s2_green; + else + state <= s1_red; + end + s2_green: + begin + p_red <= 1'b0; + p_green <= 1'b1; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s3_yellow; + else + state <= s2_green; + end + s3_yellow: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b1; + if (cnt == 3) + state <= s1_red; + else + state <= s3_yellow; + end + endcase + end + +always @(posedge clk or negedge rst_n) + if(!rst_n) + cnt <= 7'd10; + else if (pass_request&&green&&(cnt>10)) + cnt <= 7'd10; + else if (!green&&p_green) + cnt <= 7'd60; + else if (!yellow&&p_yellow) + cnt <= 7'd5; + else if (!red&&p_red) + cnt <= 7'd10; + else cnt <= cnt -1; + assign clock = cnt; + +always @(posedge clk or negedge rst_n) + if(!rst_n) + begin + yellow <= 1'd0; + red <= 1'd0; + green <= 1'd0; + end + else + begin + yellow <= p_yellow; + red <= p_red; + green <= p_green; + end + +endmodule + + +// notice: the why we set if (cnt == 3) for state transition, because at cnt 3, the next cycle +// cnt 2 the state will be updated +// cnt 1 the p_yellow, p_red, p_green will be updated +// cnt 0 the yellow, red, green will be updated +// next cycle, the traffic light will change \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob033_traffic_lights_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob033_traffic_lights_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..ab0d2d912b41d5f0f5370f2823e88596a8430e76 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob033_traffic_lights_test.sv @@ -0,0 +1,216 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic pass_request + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + pass_request = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test normal traffic light cycle + pass_request = 0; + repeat(100) @(posedge clk); // Let it cycle through red->green->yellow->red + + // Test pedestrian request during green phase + pass_request = 1; + repeat(20) @(posedge clk); // Request during green + pass_request = 0; + @(posedge clk); + + // Test pedestrian request during red phase (should not affect) + pass_request = 1; + repeat(15) @(posedge clk); // Request during red + pass_request = 0; + @(posedge clk); + + // Test pedestrian request during yellow phase (should not affect) + pass_request = 1; + repeat(10) @(posedge clk); // Request during yellow + pass_request = 0; + @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + pass_request <= $random; + end + + // === Additional checks: asynchronous active-low reset in mid-operation === + // Spec: "The reset input is active low asynchronous, and should reset the traffic light to red state"; + // "Reset behavior: Active low reset to red state with clock = 10; all lights are 0 during reset and for + // the first two clock edges after release". Earlier stimulus only resets at start-up. rst_n changes at + // posedge+2 (no edge race). + @(posedge clk); #2 pass_request <= 0; + rst_n <= 0; + repeat(3) @(posedge clk); + #2 rst_n <= 1; + repeat(40) @(posedge clk); // reset again during the following green phase + #2 rst_n <= 0; + @(posedge clk); + #2 rst_n <= 1; + repeat(17) @(posedge clk); // and once more during red + #2 rst_n <= 0; + repeat(2) @(posedge clk); + #2 rst_n <= 1; + repeat(240) @(posedge clk, negedge clk) begin + pass_request <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_clock; + int errortime_clock; + int errors_red; + int errortime_red; + int errors_yellow; + int errortime_yellow; + int errors_green; + int errortime_green; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic pass_request; + logic [7:0] clock_ref; + logic red_ref; + logic yellow_ref; + logic green_ref; + logic [7:0] clock_dut; + logic red_dut; + logic yellow_dut; + logic green_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, pass_request, clock_ref, red_ref, yellow_ref, green_ref, clock_dut, red_dut, yellow_dut, green_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .pass_request ); + + RefModule good1 ( + .clk, + .rst_n, + .pass_request, + .clock(clock_ref), + .red(red_ref), + .yellow(yellow_ref), + .green(green_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .pass_request, + .clock(clock_dut), + .red(red_dut), + .yellow(yellow_dut), + .green(green_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_clock) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "clock", stats1.errors_clock, stats1.errortime_clock); + else $display("Hint: Output '%s' has no mismatches.", "clock"); + if (stats1.errors_red) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "red", stats1.errors_red, stats1.errortime_red); + else $display("Hint: Output '%s' has no mismatches.", "red"); + if (stats1.errors_yellow) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "yellow", stats1.errors_yellow, stats1.errortime_yellow); + else $display("Hint: Output '%s' has no mismatches.", "yellow"); + if (stats1.errors_green) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "green", stats1.errors_green, stats1.errortime_green); + else $display("Hint: Output '%s' has no mismatches.", "green"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { clock_ref, red_ref, yellow_ref, green_ref } === ( { clock_ref, red_ref, yellow_ref, green_ref } ^ { clock_dut, red_dut, yellow_dut, green_dut } ^ { clock_ref, red_ref, yellow_ref, green_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (clock_ref !== ( clock_ref ^ clock_dut ^ clock_ref )) + begin if (stats1.errors_clock == 0) stats1.errortime_clock = $time; + stats1.errors_clock = stats1.errors_clock+1'b1; end + if (red_ref !== ( red_ref ^ red_dut ^ red_ref )) + begin if (stats1.errors_red == 0) stats1.errortime_red = $time; + stats1.errors_red = stats1.errors_red+1'b1; end + if (yellow_ref !== ( yellow_ref ^ yellow_dut ^ yellow_ref )) + begin if (stats1.errors_yellow == 0) stats1.errortime_yellow = $time; + stats1.errors_yellow = stats1.errors_yellow+1'b1; end + if (green_ref !== ( green_ref ^ green_dut ^ green_ref )) + begin if (stats1.errors_green == 0) stats1.errortime_green = $time; + stats1.errors_green = stats1.errors_green+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob034_gaming_machine_billing_program_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob034_gaming_machine_billing_program_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..c32699b0758661a7e3e3bf51b08ccecbf2767ce2 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob034_gaming_machine_billing_program_prompt.txt @@ -0,0 +1,78 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input [9:0] money + - input set + - input boost + - output reg [9:0] remain + - output reg yellow + - output reg red + +The module should implement a gaming machine billing system with +prepaid charging functionality. Assume all sequential logic is triggered +on the positive edge of the clock. The reset input is active low +asynchronous, and should reset the system to initial state. + +The gaming machine billing system should work as follows: +- Prepaid charging: When set signal is active, the money value is added + to the remaining balance (remain) +- Game modes: Normal mode (1 yuan per minute) and boost mode (2 yuan per + minute) +- Default mode: Normal mode (boost = 0) +- Boost mode: Activated when boost = 1, charges 2 yuan per clock cycle +- Balance deduction: Each clock cycle deducts 1 yuan (normal) or 2 yuan + (boost) from remain +- Warning indicators: Yellow light when remain > 0 and < 10, red light + when remain < 1 in normal mode, when remain < 2 in boost mode. +- Continuous charging: Users can recharge during gameplay via set signal + +Gaming machine operation: +- Normal mode: remain decreases by 1 each clock cycle +- Boost mode: remain decreases by 2 each clock cycle +- Charging: When set = 1, money value is added to remain +- Yellow warning: Active when 0 < remain < 10 +- Red warning: Active when remain < 1 for normal mode, remaon < 2 for boost mode + (insufficient funds) +- Reset behavior: Active low reset to initial state + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule +( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input [9:0]money, + input set, + input boost, + output reg[9:0]remain, + output reg yellow, + output reg red +); + always@(posedge clk or negedge rst_n) begin + if(~rst_n) begin + yellow <= 0; + red <= 0; + end + else begin + yellow <= remain<10&&remain; + red <= boost? remain<2: remain<1; + end + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + remain <= 0; + else if(boost) + remain <= set ? remain+money: + remain<2? remain: + remain-2; + else + remain <= set ? remain+money: + remain==1? remain: + remain-1; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob034_gaming_machine_billing_program_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob034_gaming_machine_billing_program_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..84d31ece4f078c42fce9b0073d9ff77540935471 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob034_gaming_machine_billing_program_ref.sv @@ -0,0 +1,36 @@ + +module RefModule +( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input [9:0]money, + input set, + input boost, + output reg[9:0]remain, + output reg yellow, + output reg red +); + always@(posedge clk or negedge rst_n) begin + if(~rst_n) begin + yellow <= 0; + red <= 0; + end + else begin + yellow <= remain<10&&remain; + red <= boost? remain<2: remain<1; + end + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + remain <= 0; + else if(boost) + remain <= set ? remain+money: + remain<2? remain: + remain-2; + else + remain <= set ? remain+money: + remain<1? remain: + remain-1; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob034_gaming_machine_billing_program_test.sv b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob034_gaming_machine_billing_program_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..e447ea3b14ac0f37fb8bc116b201da5c61489f87 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_arithmetic/Prob034_gaming_machine_billing_program_test.sv @@ -0,0 +1,208 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [9:0] money, + output logic set, + output logic boost + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + money = 0; + set = 0; + boost = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test normal mode charging + set = 1; + money = 10'd50; @(posedge clk); // Charge 50 yuan + set = 0; + @(posedge clk); + + // Test normal mode consumption (1 yuan per cycle) + boost = 0; + repeat(20) @(posedge clk); // Consume 20 yuan + + // Test charging during gameplay + boost = 0; + set = 1; + money = 10'd20; @(posedge clk); // Charge while playing + set = 0; + @(posedge clk); + + boost = 1; + repeat(25) @(posedge clk); // Consume 50 yuan + + // Test insufficient funds in normal mode + boost = 0; + repeat(10) @(posedge clk); // Try to consume more than available + + // Test insufficient funds in boost mode + set = 1; + money = 10'd3; @(posedge clk); // Charge small amount + set = 0; + @(posedge clk); + boost = 1; + repeat(5) @(posedge clk); // Try to consume in boost mode + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + set <= $random; + boost <= $random; + money <= $random % 1024; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_remain; + int errortime_remain; + int errors_yellow; + int errortime_yellow; + int errors_red; + int errortime_red; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [9:0] money; + logic set; + logic boost; + logic [9:0] remain_ref; + logic yellow_ref; + logic red_ref; + logic [9:0] remain_dut; + logic yellow_dut; + logic red_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, money, set, boost, remain_ref, yellow_ref, red_ref, remain_dut, yellow_dut, red_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .money, + .set, + .boost ); + + RefModule good1 ( + .clk, + .rst_n, + .money, + .set, + .boost, + .remain(remain_ref), + .yellow(yellow_ref), + .red(red_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .money, + .set, + .boost, + .remain(remain_dut), + .yellow(yellow_dut), + .red(red_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_remain) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "remain", stats1.errors_remain, stats1.errortime_remain); + else $display("Hint: Output '%s' has no mismatches.", "remain"); + if (stats1.errors_yellow) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "yellow", stats1.errors_yellow, stats1.errortime_yellow); + else $display("Hint: Output '%s' has no mismatches.", "yellow"); + if (stats1.errors_red) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "red", stats1.errors_red, stats1.errortime_red); + else $display("Hint: Output '%s' has no mismatches.", "red"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { remain_ref, yellow_ref, red_ref } === ( { remain_ref, yellow_ref, red_ref } ^ { remain_dut, yellow_dut, red_dut } ^ { remain_ref, yellow_ref, red_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (remain_ref !== ( remain_ref ^ remain_dut ^ remain_ref )) + begin if (stats1.errors_remain == 0) stats1.errortime_remain = $time; + stats1.errors_remain = stats1.errors_remain+1'b1; end + if (yellow_ref !== ( yellow_ref ^ yellow_dut ^ yellow_ref )) + begin if (stats1.errors_yellow == 0) stats1.errortime_yellow = $time; + stats1.errors_yellow = stats1.errors_yellow+1'b1; end + if (red_ref !== ( red_ref ^ red_dut ^ red_ref )) + begin if (stats1.errors_red == 0) stats1.errortime_red = $time; + stats1.errors_red = stats1.errors_red+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob001_continuous_input_sequence_detect_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob001_continuous_input_sequence_detect_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..5f1849c1e6d6495bacaac405bc22b362c4af98dd --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob001_continuous_input_sequence_detect_prompt.txt @@ -0,0 +1,61 @@ +I would like you to implement a module named `TopModule` with the following interface. All input and output ports are one bit unless otherwise specified. + +- **Input ports**: + - `clk` + - `rst_n` + - `a` + +- **Output ports**: + - `match` + +The module should implement an 8-bit shift register that shifts in the input `a` on every positive edge of the clock. The shift register is named `a_tem` and retains the 8 most recent bits of `a`. The module should perform the following functionality: + +1. **Reset Behavior**: + - When `rst_n` is de-asserted (logic 0), the shift register `a_tem` should be cleared to `8'b0`, and the output `match` should be set to `0`. + +2. **Shift Register Behavior**: + - On every positive edge of the clock, if `rst_n` is asserted (logic 1), the input `a` should be shifted into the least significant bit (LSB) of the shift register `a_tem`, and the previous bits should shift left by one position. + +3. **Matching Logic**: + - The output `match` should be asserted (logic 1) whenever the value of the shift register `a_tem` matches the specific 8-bit pattern `8'b0111_0001`. + - If the shift register does not match this pattern, the output `match` should remain de-asserted (logic 0). + +The design must operate synchronously with the clock and use a negative edge active reset (`rst_n`). Ensure the module correctly implements this functionality. + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input a, + output reg match + ); + + reg [7:0] a_tem; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match <= 1'b0; + end + else if (a_tem == 8'b0111_0011) + begin + match <= 1'b1; + end + else + begin + match <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 8'b0; + end + else + begin + a_tem <= {a_tem[6:0],a}; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob001_continuous_input_sequence_detect_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob001_continuous_input_sequence_detect_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..7f331ec29a6c80c9534b2e3dc96a5cc965d76324 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob001_continuous_input_sequence_detect_ref.sv @@ -0,0 +1,33 @@ +module RefModule( + input clk, + input rst_n, + input a, + output reg match + ); + + reg [7:0] a_tem; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match <= 1'b0; + end + else if (a_tem == 8'b0111_0001) + begin + match <= 1'b1; + end + else + begin + match <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 8'b0; + end + else + begin + a_tem <= {a_tem[6:0],a}; + end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob001_continuous_input_sequence_detect_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob001_continuous_input_sequence_detect_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..d2be5472b13f3a43ba2a478f58fccd93ef3d7257 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob001_continuous_input_sequence_detect_test.sv @@ -0,0 +1,165 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 +`default_nettype none + +module stimulus_gen ( + input wire clk, + output reg rst_n, + output reg a, + input wire tb_match, + input wire match_dut +); + bit failed = 0; + + always @(posedge clk, negedge clk) + if (!tb_match) + failed <= 1; + + initial begin + @(posedge clk); + failed <= 0; + rst_n <= 0; + a <= 0; + + // Initial reset + @(posedge clk); + rst_n <= 1; + + // Test Case 1: No match + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + @(posedge clk) a <= 1; + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + + // Test Case 2: Matching sequence (0111_0001) + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + @(posedge clk) a <= 1; + @(posedge clk) a <= 1; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + + // Test Case 3: Randomized input + repeat (100) @(posedge clk) begin + a <= $random; + end + + // Test Case 4: Edge cases with reset + @(posedge clk); + rst_n <= 0; + @(posedge clk); + rst_n <= 1; + repeat (8) @(posedge clk) a <= 1; + + // Add delay to observe outputs + repeat (100) @(posedge clk); + + #1 $finish; + end +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + + int clocks; + } stats; + + stats stats1; + + wire [511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk = 0; + initial forever #5 clk = ~clk; + + logic rst_n; + logic a; + logic match_ref; + logic match_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, a, match_ref, match_dut); + end + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + stimulus_gen stim1 ( + .clk(clk), + .rst_n(rst_n), + .a(a), + .tb_match(tb_match), + .match_dut(match_dut) + ); + + RefModule good1 ( + .clk(clk), + .rst_n(rst_n), + .a(a), + .match(match_ref) + ); + + TopModule top_module1 ( + .clk(clk), + .rst_n(rst_n), + .a(a), + .match(match_dut) + ); + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat (5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + final begin + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( + {match_ref} === + ({match_ref} ^ {match_dut} ^ {match_ref}) + ); + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (match_ref !== (match_ref ^ match_dut ^ match_ref)) begin + if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match++; + end + end + + // Add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob002_extraneous_items_sequence_detect_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob002_extraneous_items_sequence_detect_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..b0b26d8d8082ea55679ffa472d9f70af44f64857 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob002_extraneous_items_sequence_detect_prompt.txt @@ -0,0 +1,87 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input a + - output match + +The module should implement a sequence detection circuit that detects the +pattern "011XXX110" in the input data stream. The pattern consists of 9 bits +where the first 3 bits are "011", the last 3 bits are "110", and the middle +3 bits can be any value (don't care). + +The sequence detection should work as follows: +- Monitor the input data stream continuously +- Detect when the pattern "011XXX110" appears in the input +- Output match signal should be high when the pattern is detected +- Output match signal should be low at all other times + +Implementation approach: +The module should use a 9-bit shift register to store the last 9 input bits. +The match signal should be asserted when: +- The first 3 bits (bits 8:6) equal "011" AND +- The last 3 bits (bits 2:0) equal "110" +- The middle 3 bits (bits 5:3) can be any value + +### Timing and Behavior: +- When rst_n is low, the module should reset to its initial state +- On every positive clock edge (clk), the module should shift in the new input bit +- The output match is updated sequentially +- The detection is continuous - the module checks for the pattern on every clock cycle + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input a, + output match + ); + + reg [8:0] a_tem; + reg match_f; + reg match_b; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match_f <= 1'b0; + end + else if (a_tem[8:6] == 3'b001) + begin + match_f <= 1'b1; + end + else + begin + match_f <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match_b <= 1'b0; + end + else if (a_tem[2:0] == 3'b110) + begin + match_b <= 1'b1; + end + else + begin + match_b <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 9'b0; + end + else + begin + a_tem <= {a_tem[7:0],a}; + end + + assign match = match_b && match_f; +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob002_extraneous_items_sequence_detect_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob002_extraneous_items_sequence_detect_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..9aa18d6688f374e6ffa77394980b17c15b7107df --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob002_extraneous_items_sequence_detect_ref.sv @@ -0,0 +1,51 @@ +module RefModule( + input clk, + input rst_n, + input a, + output match + ); + + reg [8:0] a_tem; + reg match_f; + reg match_b; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match_f <= 1'b0; + end + else if (a_tem[8:6] == 3'b011) + begin + match_f <= 1'b1; + end + else + begin + match_f <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match_b <= 1'b0; + end + else if (a_tem[2:0] == 3'b110) + begin + match_b <= 1'b1; + end + else + begin + match_b <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 9'b0; + end + else + begin + a_tem <= {a_tem[7:0],a}; + end + + assign match = match_b && match_f; +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob002_extraneous_items_sequence_detect_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob002_extraneous_items_sequence_detect_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..7ebedeb9cae8bb2584af627b24db2b13c1bc3147 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob002_extraneous_items_sequence_detect_test.sv @@ -0,0 +1,178 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic a, + output logic rst_n, + output reg[511:0] wavedrom_title, + output reg wavedrom_enable, + input tb_match + // === End your code here === +); + // === Start your code here === + reg reset; + assign rst_n = ~reset; + + task wavedrom_start(input[511:0] title = ""); + endtask + + task wavedrom_stop; + #1; + endtask + + initial begin + reset = 1; + a = 0; + @(posedge clk); + reset = 0; + + wavedrom_start("Sequence Detection Test"); + + // Test specific sequences for "011XXX110" pattern + @(posedge clk) a = 0; // Start of 011101110 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 1; // Middle bit 1 + @(posedge clk) a = 0; // Middle bit 0 + @(posedge clk) a = 1; // Middle bit 1 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 0; // Should detect pattern here + + @(posedge clk) a = 0; // Start of 011001110 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 0; // Middle bit 0 + @(posedge clk) a = 0; // Middle bit 0 + @(posedge clk) a = 1; // Middle bit 1 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 0; // Should detect pattern here + + wavedrom_stop(); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + a <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic a; + logic rst_n; + logic match_ref; + logic match_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, a, rst_n, match_ref, match_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .* , + .a, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .a, + .match(match_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .a, + .match(match_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { match_ref } === ( { match_ref } ^ { match_dut } ^ { match_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (match_ref !== ( match_ref ^ match_dut ^ match_ref )) + begin if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match = stats1.errors_match+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob003_non-overlapping_sequence_detect_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob003_non-overlapping_sequence_detect_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..da872a2d1465d997c2b972b0527904c0a06a313c --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob003_non-overlapping_sequence_detect_prompt.txt @@ -0,0 +1,126 @@ +I would like you to implement a module named `TopModule` with the following interface. All input and output ports are one bit unless otherwise specified. + +### Inputs: +- `clk`: System clock signal. +- `rst_n`: Asynchronous reset signal, active low. +- `data`: Single-bit input signal, representing the data to be checked. + +### Outputs: +- `match`: A signal that outputs `1` when the input signal `data` matches the target sequence `011100`. It remains `0` at all other times. +- `not_match`: A signal that outputs `1` when the input signal `data` does not match the target sequence `011100`. It remains `0` at all other times. + +### Module Description: +The module must detect the sequence `011100` from the input signal `data`. Please use the state machine to solve this problem. The detection rules are as follows: +1. The sequence check begins with every sixth bit of input data, meaning the module processes the input in groups of six bits and does not overlap or reuse bits between groups. +2. If the first bit of a group does not match the sequence, the module skips the entire group and starts checking from the next group beginning at the seventh bit. +3. When the sequence `011100` is detected, the `match` output should assert (go high). +4. If the sequence is not detected in a group, the `not_match` output should assert (go high). + +### Output Logic: +The output signals `match` and `not_match` should be implemented as follows: +- Declare both outputs as `reg` type: `output reg match, output reg not_match` +- Use combinational logic with Wildcard Sensitivity List block to control the outputs +- The outputs are mutually exclusive - only one can be high at a time + +### Timing and Behavior: +- When `rst_n` is low, the module should reset to its initial state and begin sequence detection from the start. +- On every positive clock edge (`clk`), the module should evaluate the input signal `data` and transition states accordingly. + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input data, + output reg match, + output reg not_match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1=4'd1, + s2=4'd2, + s3=4'd3, + s4=4'd4, + s5=4'd5, + s6=4'd6, + sf1=4'd7, + sf2=4'd8, + sf3=4'd9, + sf4=4'd10, + sf5=4'd11, + sf6=4'd20; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data) +begin + case(pstate) + idle: + if(data==0) + nstate=s1; //First bit matched + else + nstate=sf1; + s1: + nstate=data?s2:sf2; + s2: + nstate=data?s3:sf3; + s3: + nstate=data?s4:sf4; + s4: + nstate=data?sf5:s5; + s5: + nstate=data?sf6:s6; + s6: + nstate=data?sf1:s1; + sf1: + nstate=sf2; + sf2: + nstate=sf3; + sf3: + nstate=sf4; + sf4: + nstate=sf5; + sf5: + nstate=sf6; + sf6: + nstate=data?sf1:s1; + default: + nstate=idle; + endcase +end + +always @(pstate or data or rst_n) +begin + if(!rst_n==1) + begin + match=1'b0; + not_match = 1'b0; + end + else if(pstate==s6) + begin + match=1'b1; + not_match = 1'b0; + end + else if(pstate==sf6) + begin + match=1'b0; + not_match = 1'b1; + end + else + begin + match=1'b0; + not_match = 1'b0; + end +end + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob003_non-overlapping_sequence_detect_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob003_non-overlapping_sequence_detect_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..10012372821357962ecd8de990a1bdcfe95a7ef1 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob003_non-overlapping_sequence_detect_ref.sv @@ -0,0 +1,95 @@ +module RefModule( + input clk, + input rst_n, + input data, + output reg match, + output reg not_match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1=4'd1, + s2=4'd2, + s3=4'd3, + s4=4'd4, + s5=4'd5, + s6=4'd6, + sf1=4'd7, + sf2=4'd8, + sf3=4'd9, + sf4=4'd10, + sf5=4'd11, + sf6=4'd12; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data) +begin + case(pstate) + idle: + if(data==0) + nstate=s1; //First bit matched + else + nstate=sf1; + s1: + nstate=data?s2:sf2; + s2: + nstate=data?s3:sf3; + s3: + nstate=data?s4:sf4; + s4: + nstate=data?sf5:s5; + s5: + nstate=data?sf6:s6; + s6: + nstate=data?sf1:s1; + sf1: + nstate=sf2; + sf2: + nstate=sf3; + sf3: + nstate=sf4; + sf4: + nstate=sf5; + sf5: + nstate=sf6; + sf6: + nstate=data?sf1:s1; + default: + nstate=idle; + endcase +end + +always @(pstate or data or rst_n) +begin + if(!rst_n==1) + begin + match=1'b0; + not_match = 1'b0; + end + else if(pstate==s6) + begin + match=1'b1; + not_match = 1'b0; + end + else if(pstate==sf6) + begin + match=1'b0; + not_match = 1'b1; + end + else + begin + match=1'b0; + not_match = 1'b0; + end +end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob003_non-overlapping_sequence_detect_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob003_non-overlapping_sequence_detect_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..f7a171a1efc9327b0131c9b4d8f328995dc37c28 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob003_non-overlapping_sequence_detect_test.sv @@ -0,0 +1,150 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + +module stimulus_gen ( + input wire clk, + output reg rst_n, + output reg data +); + + initial begin + // Initialize signals + rst_n = 1'b0; + data = 1'b0; + + // Apply reset + @(posedge clk); + rst_n = 1'b1; + @(posedge clk) + @(posedge clk) + + // Test sequence: Provide the sequence "011100" + @(posedge clk) data = 1'b0; // First bit + @(posedge clk) data = 1'b1; // Second bit + @(posedge clk) data = 1'b1; // Third bit + @(posedge clk) data = 1'b1; // Fourth bit + @(posedge clk) data = 1'b0; // Fifth bit + @(posedge clk) data = 1'b0; // Sixth bit + + // Additional test cases + repeat(500) @(negedge clk) begin + data <= $random; + end + + // End simulation after sufficient cycles + repeat(100) @(posedge clk); + #1 $finish; + end + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + int errors_not_match; + int errortime_not_match; + + int clocks; + } stats; + + stats stats1; + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk = 0; + initial forever + #5 clk = ~clk; + + logic rst_n; + logic data; + logic match_ref; + logic match_dut; + logic not_match_ref; + logic not_match_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data, match_ref, match_dut, not_match_ref, not_match_dut); + end + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // Instantiate stimulus generator + stimulus_gen stim1 ( + .clk(clk), + .rst_n(rst_n), + .data(data) + ); + + // Instantiate reference module + RefModule good1 ( + .clk(clk), + .rst_n(rst_n), + .data(data), + .match(match_ref), + .not_match(not_match_ref) + ); + + // Instantiate DUT (TopModule) + TopModule top_module1 ( + .clk(clk), + .rst_n(rst_n), + .data(data), + .match(match_dut), + .not_match(not_match_dut) + ); + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Delay until the end of the time step + @(strobe); + end + endtask + + final begin + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + if (stats1.errors_not_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "not_match", stats1.errors_not_match, stats1.errortime_not_match); + else $display("Hint: Output '%s' has no mismatches.", "not_match"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: Ensure outputs match the reference module + assign tb_match = ( { match_ref, not_match_ref } === ( { match_ref, not_match_ref } ^ { match_dut, not_match_dut } ^ { match_ref, not_match_ref } ) ); + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (match_ref !== ( match_ref ^ match_dut ^ match_ref )) begin + if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match++; + end + if (not_match_ref !== ( not_match_ref ^ not_match_dut ^ not_match_ref )) begin + if (stats1.errors_not_match == 0) stats1.errortime_not_match = $time; + stats1.errors_not_match++; + end + end + + // Add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob004_continuous_input_sequence_detect2_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob004_continuous_input_sequence_detect2_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..a6116b3a019f337173a9a1f0e1b2d76f7824ec9b --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob004_continuous_input_sequence_detect2_prompt.txt @@ -0,0 +1,110 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input data + - input data_valid + - output match + +The module should implement a sequence detection circuit that detects the +sequence "0110" in the input data stream. The data_valid signal indicates +when the input data is valid and should be considered for sequence detection. +When data_valid is high, the current input data is valid and participates +in sequence detection. When data_valid is low, the current input data is +invalid and should be ignored (not considered for sequence detection). + +The sequence detection should work as follows: +- Use state machine and update it sequentially +- Monitor the input data stream when data_valid is high +- Detect when the sequence "0110" appears in the valid data +- Output match signal should be high when the sequence "0110" is detected +- Output match signal should be low at all other times + +Output Logic for match signal: +- When reset is active (rst_n = 0): match = 0 +- When reset is inactive (rst_n = 1) AND sequence is 0110: match = 1 +- When reset is inactive (rst_n = 1) AND sequence is NOT 0110: match = 0 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input data, + input data_valid, + output reg match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1_d0=4'd1, + s2_d01=4'd2, + s3_d011=4'd3, + s4_d0110=4'd2; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data or data_valid) +begin + case(pstate) + idle: + if(data_valid && !data) + nstate=s1_d0; //First bit matched + else + nstate=idle; + s1_d0: + if (data_valid) + begin + if (data) nstate = s2_d01; //Data valid and is 1, first two bits 01 matched, next state is s2_d01 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s1_d0; //Data invalid, stay in s1_d0 + s2_d01: + if (data_valid) + begin + if (data) nstate = s3_d011; //Data valid and is 1, first three bits 011 matched, next state is s3_d011 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s2_d01; //Data invalid, stay in s2_d01 + s3_d011: + if (data_valid) + begin + if (!data) nstate = s4_d0110; //Data valid and is 0, first four bits 0110 matched, next state is s4_d0110 + else nstate = idle; //Data valid but is 1, no match, next state is idle + end + else nstate = s3_d011; //Data invalid, stay in s3_d011 + s4_d0110: + if (data_valid) + begin + if (!data) nstate = s1_d0; //Data valid and is 0, matches first bit 0 of target sequence, next state is s1_d0 + else nstate = idle; //Data valid but is 1, does not match target sequence, next state is idle + end + else nstate = idle; //Data invalid, next state is idle + default: + nstate=idle; + endcase +end + +always @(pstate or rst_n) +begin + if(!rst_n==1) + match=1'b0; + else if(pstate==s4_d0110) //Entering state s4_d0110 means all four bits matched, assert match signal high + match=1'b1; + else + match=1'b0; +end + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob004_continuous_input_sequence_detect2_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob004_continuous_input_sequence_detect2_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..de2de151907bcac48bce85248c54dd0ccded8d1f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob004_continuous_input_sequence_detect2_ref.sv @@ -0,0 +1,78 @@ + +module RefModule( + input clk, + input rst_n, + input data, + input data_valid, + output reg match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1_d0=4'd1, + s2_d01=4'd2, + s3_d011=4'd3, + s4_d0110=4'd4; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data or data_valid) +begin + case(pstate) + idle: + if(data_valid && !data) + nstate=s1_d0; //First bit matched + else + nstate=idle; + s1_d0: + if (data_valid) + begin + if (data) nstate = s2_d01; //Data valid and is 1, first two bits 01 matched, next state is s2_d01 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s1_d0; //Data invalid, stay in s1_d0 + s2_d01: + if (data_valid) + begin + if (data) nstate = s3_d011; //Data valid and is 1, first three bits 011 matched, next state is s3_d011 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s2_d01; //Data invalid, stay in s2_d01 + s3_d011: + if (data_valid) + begin + if (!data) nstate = s4_d0110; //Data valid and is 0, first four bits 0110 matched, next state is s4_d0110 + else nstate = idle; //Data valid but is 1, no match, next state is idle + end + else nstate = s3_d011; //Data invalid, stay in s3_d011 + s4_d0110: + if (data_valid) + begin + if (!data) nstate = s1_d0; //Data valid and is 0, matches first bit 0 of target sequence, next state is s1_d0 + else nstate = idle; //Data valid but is 1, does not match target sequence, next state is idle + end + else nstate = idle; //Data invalid, next state is idle + default: + nstate=idle; + endcase +end + +always @(pstate or rst_n) +begin + if(!rst_n==1) + match=1'b0; + else if(pstate==s4_d0110) //Entering state s4_d0110 means all four bits matched, assert match signal high + match=1'b1; + else + match=1'b0; +end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob004_continuous_input_sequence_detect2_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob004_continuous_input_sequence_detect2_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..82239fb98ff463683ce3e113ae82b524feb863f9 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob004_continuous_input_sequence_detect2_test.sv @@ -0,0 +1,163 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + output logic rst_n, + output logic data, + output logic data_valid +); + + initial begin + rst_n = 0; + data = 0; + data_valid = 0; + @(posedge clk); + @(posedge clk); + rst_n = 1; + @(posedge clk); + @(posedge clk); + + // Test specific sequence: 0110 + data_valid = 1; + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + data_valid = 0; + @(posedge clk); + @(posedge clk); + + // Test with data_valid low (should ignore data) + data_valid = 0; + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + @(posedge clk); + + // Test overlapping sequences + data_valid = 1; + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + @(posedge clk); + + // Random testing + repeat(500) @(negedge clk) begin + data_valid <= $random; + data <= $random; + end + + #1 $finish; + end + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + + int clocks; + } stats; + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + logic rst_n; + logic data; + logic data_valid; + logic match_ref; + logic match_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data, data_valid, match_ref, match_dut ); + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + stimulus_gen stim1 ( + .clk, + .* , + .rst_n, + .data, + .data_valid ); + RefModule good1 ( + .clk, + .rst_n, + .data, + .data_valid, + .match(match_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .data, + .data_valid, + .match(match_dut) ); + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { match_ref } === ( { match_ref } ^ { match_dut } ^ { match_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (match_ref !== ( match_ref ^ match_dut ^ match_ref )) + begin if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match = stats1.errors_match+1'b1; end + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob005_signal_generator_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob005_signal_generator_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..90a1be1644ea65208065c3a8031f1ba9e20b2fc7 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob005_signal_generator_prompt.txt @@ -0,0 +1,78 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input wave_choice (2 bits) + - output wave (5 bits) + +The module should implement a signal generator that outputs different +waveforms based on the wave_choice input signal. The generator should +produce three different types of waveforms: + +When wave_choice = 0: Generate a square wave signal +When wave_choice = 1: Generate a sawtooth wave signal +When wave_choice = 2: Generate a triangular wave signal + +The waveform generation should work as follows: +- The wave output should be a 5-bit signal that cycles through different + values to create the desired waveform patterns +- Each waveform should complete one full cycle and then repeat +- The square wave should alternate between high (20) and low (0) values, + 10 clock cycles each +- The sawtooth wave should increment from 0 to maximum value and then reset +- The triangular wave should increment from 0 to maximum (20), then decrement back to 0; + when triangular mode is selected the wave first decrements to 0 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input [1:0] wave_choice, + output reg [4:0]wave + ); + + reg [4:0] cnt; + reg flag; + + // Square wave mode, counter control + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= wave_choice!=0 ? 0: + cnt ==20? 0: + cnt + 1; + end + + // Triangle wave mode, flag bit control + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + flag <= 0; + else + flag <= wave_choice!=2 ? 0: + wave ==1 ? 1: + wave ==20? 0: + flag; + end + + + // Update wave signal + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + wave <= 0; + else + case(wave_choice) + 0 : wave <= cnt == 9? 20 : + cnt ==20? 0 : + wave; + 1 : wave <= wave==20? 0 : wave+1; + 2 : wave <= flag==0 ? wave-1: wave+1; + default: wave <= 0; + endcase + end +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob005_signal_generator_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob005_signal_generator_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..3efcd081b82e458cda37fd0b80e77f6f74d848a4 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob005_signal_generator_ref.sv @@ -0,0 +1,48 @@ + +module RefModule( + input clk, + input rst_n, + input [1:0] wave_choice, + output reg [4:0]wave + ); + + reg [4:0] cnt; + reg flag; + + // Square wave mode, counter control + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= wave_choice!=0 ? 0: + cnt ==19? 0: + cnt + 1; + end + + // Triangle wave mode, flag bit control + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + flag <= 0; + else + flag <= wave_choice!=2 ? 0: + wave ==1 ? 1: + wave ==19? 0: + flag; + end + + + // Update wave signal + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + wave <= 0; + else + case(wave_choice) + 0 : wave <= cnt == 9? 20 : + cnt ==19? 0 : + wave; + 1 : wave <= wave==20? 0 : wave+1; + 2 : wave <= flag==0 ? wave-1: wave+1; + default: wave <= 0; + endcase + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob005_signal_generator_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob005_signal_generator_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..bd10db973c22a6fd3ee41da31fe465c604253b2b --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob005_signal_generator_test.sv @@ -0,0 +1,146 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [1:0] wave_choice + // === End your code here === +); + + + initial begin + rst_n = 0; + wave_choice = 0; + repeat(20) @(posedge clk); + rst_n = 1; + @(posedge clk); + // Test square wave (wave_choice = 0) + wave_choice = 0; + repeat(800) @(posedge clk); + + // Test sawtooth wave (wave_choice = 1) + wave_choice = 1; + repeat(800) @(posedge clk); + + // Test triangular wave (wave_choice = 2) + wave_choice = 2; + repeat(800) @(posedge clk); + + wave_choice = 0; + repeat(800) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_wave; + int errortime_wave; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [1:0] wave_choice; + logic [4:0] wave_ref; + logic [4:0] wave_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, wave_choice, wave_ref, wave_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .wave_choice ); + + RefModule good1 ( + .clk, + .rst_n, + .wave_choice(wave_choice), + .wave(wave_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .wave_choice, + .wave(wave_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_wave) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "wave", stats1.errors_wave, stats1.errortime_wave); + else $display("Hint: Output '%s' has no mismatches.", "wave"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { wave_ref } === ( { wave_ref } ^ { wave_dut } ^ { wave_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (wave_ref !== ( wave_ref ^ wave_dut ^ wave_ref )) + begin if (stats1.errors_wave == 0) stats1.errortime_wave = $time; + stats1.errors_wave = stats1.errors_wave+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob006_data_serial-to-parallel_circuit_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob006_data_serial-to-parallel_circuit_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..1e63ce1cc335961bb2483a0e5ad2487dfbedc3b8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob006_data_serial-to-parallel_circuit_prompt.txt @@ -0,0 +1,85 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_a + - input data_a + - output ready_a + - output valid_b + - output data_b (6 bits) + +The module should implement a serial-to-parallel conversion circuit that +converts single-bit input data into 6-bit parallel output data. The circuit +uses different handshake protocols for input and output interfaces. + +The serial-to-parallel conversion should work as follows: +- Input interface: Uses valid-ready handshake mechanism + - valid_a indicates the validity of input data_a + - ready_a indicates the module is always ready to receive upstream data +- Output interface: Uses valid-only handshake mechanism + - valid_b indicates the validity of output data_b + - data_b is the 6-bit parallel output data + +Data conversion process: +- The module receives single-bit input data_a when valid_a is high +- After receiving 6 input data bits, the module outputs the concatenated + 6-bit data_b with valid_b asserted +- The first received data bit should be placed in the LSB (bit 0) of data_b +- The last received data bit should be placed in the MSB (bit 5) of data_b +- ready_a is always high indicates the module is always ready to receive input data + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input valid_a , + input data_a , + + output reg ready_a , + output reg valid_b , + output reg [5:0] data_b +); + reg [5:0] data_r; + reg [2:0] cnt; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~ready_a||~valid_a? cnt: + cnt == 4? 0 : + cnt+1; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_r <= 6'b0; + else + data_r <= ready_a&&valid_a? {data_a, data_r[5:1]}: data_r; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_b <= 6'b0; + else + data_b <= cnt==5&&valid_a? {data_a, data_r[5:1]}: data_b; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_b <= 0; + else + valid_b <= cnt==5; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + ready_a <= 0; + else + ready_a <= 1; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob006_data_serial-to-parallel_circuit_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob006_data_serial-to-parallel_circuit_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..280616e82b10504600dea0c2e26db481db5b0642 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob006_data_serial-to-parallel_circuit_ref.sv @@ -0,0 +1,50 @@ +module RefModule( + input clk , + input rst_n , + input valid_a , + input data_a , + + output reg ready_a , + output reg valid_b , + output reg [5:0] data_b +); + reg [5:0] data_r; + reg [2:0] cnt; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~ready_a||~valid_a? cnt: + cnt == 5? 0 : + cnt+1; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_r <= 6'b0; + else + data_r <= ready_a&&valid_a? {data_a, data_r[5:1]}: data_r; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_b <= 6'b0; + else + data_b <= cnt==5&&valid_a? {data_a, data_r[5:1]}: data_b; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_b <= 0; + else + valid_b <= cnt==5; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + ready_a <= 0; + else + ready_a <= 1; + end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob006_data_serial-to-parallel_circuit_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob006_data_serial-to-parallel_circuit_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..80960f300c161a5c1c4aa9165937c0313ed072a0 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob006_data_serial-to-parallel_circuit_test.sv @@ -0,0 +1,202 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_a, + output logic data_a + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_a = 0; + data_a = 0; + repeat(4)@(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test serial data input: 6 bits per group + // First group: 101010 + valid_a = 1; + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + + // Second group: 110011 + data_a = 1; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 1; @(posedge clk); + + // Test with valid_a low (should ignore data) + valid_a = 0; + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 1; @(posedge clk); + + // Third group: 111000 + valid_a = 1; + data_a = 1; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 0; @(posedge clk); + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + valid_a <= $random; + data_a <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_ready_a; + int errortime_ready_a; + int errors_valid_b; + int errortime_valid_b; + int errors_data_b; + int errortime_data_b; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_a; + logic data_a; + logic ready_a_ref; + logic ready_a_dut; + logic valid_b_ref; + logic valid_b_dut; + logic [5:0] data_b_ref; + logic [5:0] data_b_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_a, data_a, ready_a_ref, ready_a_dut, valid_b_ref, valid_b_dut, data_b_ref, data_b_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_a, + .data_a ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_a, + .data_a, + .ready_a(ready_a_ref), + .valid_b(valid_b_ref), + .data_b(data_b_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_a, + .data_a, + .ready_a(ready_a_dut), + .valid_b(valid_b_dut), + .data_b(data_b_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_ready_a) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "ready_a", stats1.errors_ready_a, stats1.errortime_ready_a); + else $display("Hint: Output '%s' has no mismatches.", "ready_a"); + if (stats1.errors_valid_b) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_b", stats1.errors_valid_b, stats1.errortime_valid_b); + else $display("Hint: Output '%s' has no mismatches.", "valid_b"); + if (stats1.errors_data_b) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_b", stats1.errors_data_b, stats1.errortime_data_b); + else $display("Hint: Output '%s' has no mismatches.", "data_b"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { ready_a_ref, valid_b_ref, data_b_ref } === ( { ready_a_ref, valid_b_ref, data_b_ref } ^ { ready_a_dut, valid_b_dut, data_b_dut } ^ { ready_a_ref, valid_b_ref, data_b_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (ready_a_ref !== ( ready_a_ref ^ ready_a_dut ^ ready_a_ref )) + begin if (stats1.errors_ready_a == 0) stats1.errortime_ready_a = $time; + stats1.errors_ready_a = stats1.errors_ready_a+1'b1; end + if (valid_b_ref !== ( valid_b_ref ^ valid_b_dut ^ valid_b_ref )) + begin if (stats1.errors_valid_b == 0) stats1.errortime_valid_b = $time; + stats1.errors_valid_b = stats1.errors_valid_b+1'b1; end + if (data_b_ref !== ( data_b_ref ^ data_b_dut ^ data_b_ref )) + begin if (stats1.errors_data_b == 0) stats1.errortime_data_b = $time; + stats1.errors_data_b = stats1.errors_data_b+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob007_data_accumulation_output_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob007_data_accumulation_output_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..958898deb1e2f01dd7cffe21334ae62594f66ef8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob007_data_accumulation_output_prompt.txt @@ -0,0 +1,83 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input data_in (8 bits) + - input valid_a + - input ready_b + - output ready_a + - output valid_b + - output data_out (10 bits) + +The module should implement a serial data accumulation circuit that receives +8-bit input data and outputs the accumulated sum of every 4 input values. +The circuit uses valid-ready handshake mechanisms for both input and output +interfaces to ensure efficient data transfer without performance loss. + +The data accumulation should work as follows: +- Input interface: Uses valid-ready handshake mechanism + - valid_a indicates the validity of input data_in + - ready_a indicates the module is ready to receive upstream data +- Output interface: Uses valid-ready handshake mechanism + - valid_b indicates the validity of output data_out + - ready_b indicates downstream is ready to receive data + +Data accumulation process: +- The module receives 8-bit input data_in when valid_a is high and ready_a is high +- After receiving 4 input data values, the module outputs the accumulated sum + as 10-bit data_out with valid_b asserted +- The output data_out represents the sum of the 4 most recently received inputs; + if ready_b is low when the first input of a group is received, that input is + added to the previous sum instead +- Both input and output interfaces must support backpressure through ready signals +- The design must ensure no performance loss when both upstream and downstream + are ready for full-speed transmission (no bubbles in data flow) + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input [7:0] data_in , + input valid_a , + input ready_b , + + output ready_a , + output reg valid_b , + output reg [9:0] data_out +); +reg [1:0] data_cnt; + +assign ready_a = !valid_b | ready_b; + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_cnt <= 'd0; + else if(valid_a && ready_a) + data_cnt <= (data_cnt == 2'd2) ? 'd0 : (data_cnt + 1'd1); +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_b <= 'd0; + else if(data_cnt == 2'd3 && valid_a && ready_a) + valid_b <= 1'd1; + else if(valid_b && ready_b) + valid_b <= 1'd0; +end + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(ready_b && valid_a && ready_a && (data_cnt == 2'd0)) + data_out <= data_in; + else if(valid_a && ready_a) + data_out <= data_out + data_in; + +end + + +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob007_data_accumulation_output_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob007_data_accumulation_output_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..93c5651738cceb1a0b7892457af7e03a21f71db8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob007_data_accumulation_output_ref.sv @@ -0,0 +1,43 @@ + +module RefModule( + input clk , + input rst_n , + input [7:0] data_in , + input valid_a , + input ready_b , + + output ready_a , + output reg valid_b , + output reg [9:0] data_out +); +reg [1:0] data_cnt; + +assign ready_a = !valid_b | ready_b; + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_cnt <= 'd0; + else if(valid_a && ready_a) + data_cnt <= (data_cnt == 2'd3) ? 'd0 : (data_cnt + 1'd1); +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_b <= 'd0; + else if(data_cnt == 2'd3 && valid_a && ready_a) + valid_b <= 1'd1; + else if(valid_b && ready_b) + valid_b <= 1'd0; +end + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(ready_b && valid_a && ready_a && (data_cnt == 2'd0)) + data_out <= data_in; + else if(valid_a && ready_a) + data_out <= data_out + data_in; + +end + + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob007_data_accumulation_output_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob007_data_accumulation_output_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..99cd1ac83490a6b3f47e9a6f26e024d4a192ddd4 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob007_data_accumulation_output_test.sv @@ -0,0 +1,209 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [7:0] data_in, + output logic valid_a, + output logic ready_b + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + data_in = 0; + valid_a = 0; + ready_b = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test first group of 4 values: 1, 2, 3, 4 (sum = 10) + valid_a <= 1; + data_in <= 8'd1; @(posedge clk); + data_in <= 8'd2; @(posedge clk); + data_in <= 8'd3; @(posedge clk); + data_in <= 8'd4; @(posedge clk); + + data_in <= 8'd5; @(posedge clk); + @(posedge clk); + @(posedge clk); + ready_b <= 1; + + data_in <= 8'd6; @(posedge clk); + data_in <= 8'd7; @(posedge clk); + data_in <= 8'd8; @(posedge clk); + + // Test with valid_a low (should ignore data) + valid_a <= 0; + data_in <= 8'd10; @(posedge clk); + data_in <= 8'd20; @(posedge clk); + + // Test with ready_b low (backpressure) + valid_a <= 1; + ready_b <= 0; + data_in <= 8'd1; @(posedge clk); + data_in <= 8'd2; @(posedge clk); + + // Test third group: 10, 20, 30, 40 (sum = 100) + ready_b <= 1; + data_in <= 8'd10; @(posedge clk); + data_in <= 8'd20; @(posedge clk); + data_in <= 8'd30; @(posedge clk); + data_in <= 8'd40; @(posedge clk); + + // Random testing + repeat(300) @(posedge clk or negedge clk) begin + valid_a <= $random; + ready_b <= $random; + data_in <= $random % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_ready_a; + int errortime_ready_a; + int errors_valid_b; + int errortime_valid_b; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [7:0] data_in; + logic valid_a; + logic ready_b; + logic ready_a_ref; + logic ready_a_dut; + logic valid_b_ref; + logic valid_b_dut; + logic [9:0] data_out_ref; + logic [9:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data_in, valid_a, ready_b, ready_a_ref, ready_a_dut, valid_b_ref, valid_b_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .data_in, + .valid_a, + .ready_b ); + + RefModule good1 ( + .clk, + .rst_n, + .data_in, + .valid_a, + .ready_b, + .ready_a(ready_a_ref), + .valid_b(valid_b_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .data_in, + .valid_a, + .ready_b, + .ready_a(ready_a_dut), + .valid_b(valid_b_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_ready_a) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "ready_a", stats1.errors_ready_a, stats1.errortime_ready_a); + else $display("Hint: Output '%s' has no mismatches.", "ready_a"); + if (stats1.errors_valid_b) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_b", stats1.errors_valid_b, stats1.errortime_valid_b); + else $display("Hint: Output '%s' has no mismatches.", "valid_b"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { ready_a_ref, valid_b_ref, data_out_ref } === ( { ready_a_ref, valid_b_ref, data_out_ref } ^ { ready_a_dut, valid_b_dut, data_out_dut } ^ { ready_a_ref, valid_b_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (ready_a_ref !== ( ready_a_ref ^ ready_a_dut ^ ready_a_ref )) + begin if (stats1.errors_ready_a == 0) stats1.errortime_ready_a = $time; + stats1.errors_ready_a = stats1.errors_ready_a+1'b1; end + if (valid_b_ref !== ( valid_b_ref ^ valid_b_dut ^ valid_b_ref )) + begin if (stats1.errors_valid_b == 0) stats1.errortime_valid_b = $time; + stats1.errors_valid_b = stats1.errors_valid_b+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob008_non-integer_data_width_conversion_24to128_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob008_non-integer_data_width_conversion_24to128_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..8837a50b801ff72c621b3c3a8164a76643b177dc --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob008_non-integer_data_width_conversion_24to128_prompt.txt @@ -0,0 +1,79 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_in + - input data_in (24 bits) + - output reg valid_out + - output reg data_out (128 bits) + +The module should implement a data width conversion circuit that converts +24-bit input data into 128-bit output data. The circuit uses valid-only +handshake mechanism for both input and output interfaces. + +The data width conversion should work as follows: +- Input interface: Uses valid-only handshake mechanism + - valid_in indicates the validity of input data_in +- Output interface: Uses valid-only handshake mechanism + - valid_out indicates the validity of output data_out + +Data conversion process: +- The module receives 24-bit input data_in when valid_in is high +- The module accumulates multiple 24-bit inputs to form a 128-bit output +- The first received 24-bit data should be placed in the high bits of data_out +- The last received 24-bit data should be placed in the low bits of data_out +- After accumulating enough data to form a complete 128-bit word, the module + outputs data_out with valid_out asserted + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input valid_in , + input [23:0] data_in , + + output reg valid_out , + output reg [127:0] data_out +); + reg [3:0] cnt; + reg [127:0] data_lock; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~valid_in? cnt:cnt+1; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_out <= 0; + else + valid_out <= (cnt==5 || cnt==12 || cnt==15)&&valid_in; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_lock <= 0; + else + data_lock <= valid_in? {data_lock[103:0], data_in}: data_lock; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else if(cnt==5) + data_out <= valid_in? {data_lock[119:0], data_in[23:16]}: data_out; + else if(cnt==10) + data_out <= valid_in? {data_lock[111:0], data_in[23: 8]}: data_out; + else if(cnt==15) + data_out <= valid_in? {data_lock[103:0], data_in[23: 0]}: data_out; + else + data_out <= data_out; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob008_non-integer_data_width_conversion_24to128_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob008_non-integer_data_width_conversion_24to128_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..1301d527e940e047fcc73d4de472ec628eac3f48 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob008_non-integer_data_width_conversion_24to128_ref.sv @@ -0,0 +1,47 @@ + +module RefModule( + input clk , + input rst_n , + input valid_in , + input [23:0] data_in , + + output reg valid_out , + output reg [127:0] data_out +); + reg [3:0] cnt; + reg [127:0] data_lock; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~valid_in? cnt:cnt+1; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_out <= 0; + else + valid_out <= (cnt==5 || cnt==10 || cnt==15)&&valid_in; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_lock <= 0; + else + data_lock <= valid_in? {data_lock[103:0], data_in}: data_lock; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else if(cnt==5) + data_out <= valid_in? {data_lock[119:0], data_in[23:16]}: data_out; + else if(cnt==10) + data_out <= valid_in? {data_lock[111:0], data_in[23: 8]}: data_out; + else if(cnt==15) + data_out <= valid_in? {data_lock[103:0], data_in[23: 0]}: data_out; + else + data_out <= data_out; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob008_non-integer_data_width_conversion_24to128_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob008_non-integer_data_width_conversion_24to128_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..7256496acfb63697009cadc94c0c275adccae24e --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob008_non-integer_data_width_conversion_24to128_test.sv @@ -0,0 +1,190 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_in, + output logic [23:0] data_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_in = 0; + data_in = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test first group: 6 x 24-bit inputs to form 128-bit output + // Inputs: 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666 + valid_in <= 1; + data_in <= 24'h111111; @(posedge clk); + data_in <= 24'h222222; @(posedge clk); + data_in <= 24'h333333; @(posedge clk); + data_in <= 24'h444444; @(posedge clk); + data_in <= 24'h555555; @(posedge clk); + data_in <= 24'h666666; @(posedge clk); + + // Test second group: 6 x 24-bit inputs + data_in <= 24'h777777; @(posedge clk); + data_in <= 24'h888888; @(posedge clk); + data_in <= 24'h999999; @(posedge clk); + data_in <= 24'haaaaaa; @(posedge clk); + data_in <= 24'hbbbbbb; @(posedge clk); + data_in <= 24'hcccccc; @(posedge clk); + + // Test with valid_in low (should ignore data) + valid_in <= 0; + data_in <= 24'hdddddd; @(posedge clk); + data_in <= 24'heeeeee; @(posedge clk); + + // Test third group: 6 x 24-bit inputs + valid_in <= 1; + data_in <= 24'hffffff; @(posedge clk); + data_in <= 24'h000000; @(posedge clk); + data_in <= 24'h123456; @(posedge clk); + data_in <= 24'h789abc; @(posedge clk); + data_in <= 24'hdef012; @(posedge clk); + data_in <= 24'h345678; @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + valid_in <= $random; + data_in <= $random % (1<<24); + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_valid_out; + int errortime_valid_out; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_in; + logic [23:0] data_in; + logic valid_out_ref; + logic valid_out_dut; + logic [127:0] data_out_ref; + logic [127:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_in, data_in, valid_out_ref, valid_out_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_in, + .data_in ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_valid_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_out", stats1.errors_valid_out, stats1.errortime_valid_out); + else $display("Hint: Output '%s' has no mismatches.", "valid_out"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { valid_out_ref, data_out_ref } === ( { valid_out_ref, data_out_ref } ^ { valid_out_dut, data_out_dut } ^ { valid_out_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (valid_out_ref !== ( valid_out_ref ^ valid_out_dut ^ valid_out_ref )) + begin if (stats1.errors_valid_out == 0) stats1.errortime_valid_out = $time; + stats1.errors_valid_out = stats1.errors_valid_out+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob009_non-integer_data_width_conversion_8to12_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob009_non-integer_data_width_conversion_8to12_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..90cfaec558a9af111f88376c913986b00b1c049c --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob009_non-integer_data_width_conversion_8to12_prompt.txt @@ -0,0 +1,80 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_in + - input data_in (8 bits) + - output valid_out + - output data_out (12 bits) + +The module should implement a data width conversion circuit that converts +8-bit input data into 12-bit output data. The circuit uses valid-only +handshake mechanism for both input and output interfaces. + +The data width conversion should work as follows: +- Input interface: Uses valid-only handshake mechanism + - valid_in indicates the validity of input data_in +- Output interface: Uses valid-only handshake mechanism + - valid_out indicates the validity of output data_out + +Data conversion process: +- The module receives 8-bit input data_in when valid_in is high +- The module accumulates three 8-bit inputs to form two 12-bit outputs +- The first data_out is {1st input, upper 4 bits of 2nd input}; the second + data_out is {lower 4 bits of 2nd input, 3rd input} +- One clock cycle after the 2nd and after the 3rd input, the module outputs + data_out with valid_out asserted; the 4th input starts a new group +- If only one data input is received, the module waits for the second input + before producing any output + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [11:0] data_out +); +reg [7:0] data_lock; +reg [1:0] valid_cnt ; +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_lock <= 'd0; + else if(valid_in ) + data_lock <= data_in; +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_cnt <= 'd0; + else if(valid_in)begin + if(valid_cnt == 2'd1) + valid_cnt <= 2'd0; + else + valid_cnt <= valid_cnt + 1'd1; + end +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + valid_out <= 1'd1; + else if(valid_in && valid_cnt == 2'd2) + valid_out <= 1'd1; + else + valid_out <= 'd0; +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + data_out <= {data_lock, data_in[7:4]}; + else if(valid_in && valid_cnt == 2'd3) + data_out <= {data_lock[3:0], data_in}; +end +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob009_non-integer_data_width_conversion_8to12_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob009_non-integer_data_width_conversion_8to12_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..67250e6ddee50ae7ab11c617b4762a2b2e50c1b5 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob009_non-integer_data_width_conversion_8to12_ref.sv @@ -0,0 +1,47 @@ + +module RefModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [11:0] data_out +); +reg [7:0] data_lock; +reg [1:0] valid_cnt ; +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_lock <= 'd0; + else if(valid_in ) + data_lock <= data_in; +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_cnt <= 'd0; + else if(valid_in)begin + if(valid_cnt == 2'd2) + valid_cnt <= 2'd0; + else + valid_cnt <= valid_cnt + 1'd1; + end +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + valid_out <= 1'd1; + else if(valid_in && valid_cnt == 2'd2) + valid_out <= 1'd1; + else + valid_out <= 'd0; +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + data_out <= {data_lock, data_in[7:4]}; + else if(valid_in && valid_cnt == 2'd2) + data_out <= {data_lock[3:0], data_in}; +end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob009_non-integer_data_width_conversion_8to12_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob009_non-integer_data_width_conversion_8to12_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..eb7feeb6a870af7d0f6ae7f10cf400cf0f05562d --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob009_non-integer_data_width_conversion_8to12_test.sv @@ -0,0 +1,181 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_in, + output logic [7:0] data_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_in = 0; + data_in = 0; + repeat(3) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test first pair: 0xAA, 0x55 (should form 0xAA5) + valid_in = 1; + data_in = 8'hAA; @(posedge clk); + data_in = 8'h55; @(posedge clk); + + // Test second pair: 0x12, 0x34 (should form 0x123) + data_in = 8'h12; @(posedge clk); + data_in = 8'h34; @(posedge clk); + + // Test with valid_in low (should ignore data) + valid_in = 0; + data_in = 8'hFF; @(posedge clk); + data_in = 8'hEE; @(posedge clk); + + // Test third pair: 0x78, 0x9A (should form 0x789) + valid_in = 1; + data_in = 8'h78; @(posedge clk); + data_in = 8'h9A; @(posedge clk); + + // Test fourth pair: 0xBC, 0xDE (should form 0xBCD) + data_in = 8'hBC; @(posedge clk); + data_in = 8'hDE; @(posedge clk); + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + valid_in <= $random; + data_in <= $random % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_valid_out; + int errortime_valid_out; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_in; + logic [7:0] data_in; + logic valid_out_ref; + logic valid_out_dut; + logic [11:0] data_out_ref; + logic [11:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_in, data_in, valid_out_ref, valid_out_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_in, + .data_in ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_valid_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_out", stats1.errors_valid_out, stats1.errortime_valid_out); + else $display("Hint: Output '%s' has no mismatches.", "valid_out"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { valid_out_ref, data_out_ref } === ( { valid_out_ref, data_out_ref } ^ { valid_out_dut, data_out_dut } ^ { valid_out_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (valid_out_ref !== ( valid_out_ref ^ valid_out_dut ^ valid_out_ref )) + begin if (stats1.errors_valid_out == 0) stats1.errortime_valid_out = $time; + stats1.errors_valid_out = stats1.errors_valid_out+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob010_integer_multiple_data_bit_width_conversion_8to16_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob010_integer_multiple_data_bit_width_conversion_8to16_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..d0ad3cf424f657475c082e19f09c28f902437ba9 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob010_integer_multiple_data_bit_width_conversion_8to16_prompt.txt @@ -0,0 +1,74 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_in + - input data_in (8 bits) + - output reg valid_out + - output reg data_out (16 bits) + +The module should implement a data width conversion circuit that converts +8-bit input data into 16-bit output data. The circuit uses valid-only +handshake mechanism for both input and output interfaces. + +The data width conversion should work as follows: +- Input interface: Uses valid-only handshake mechanism + - valid_in indicates the validity of input data_in +- Output interface: Uses valid-only handshake mechanism + - valid_out indicates the validity of output data_out + +Data conversion process: +- The module receives 8-bit input data_in when valid_in is high +- The module accumulates two 8-bit inputs to form a 16-bit output +- The first received 8-bit data should be placed in the high 8 bits of data_out +- The second received 8-bit data should be placed in the low 8 bits of data_out +- After receiving exactly 2 input data values, the module outputs data_out + with valid_out asserted in the next clock cycle +- If only one data input is received, the module waits for the second input + before producing any output + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [15:0] data_out +); + reg [7:0] data_r; + reg flag; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + flag <= 0; + else + flag <= valid_in? ~flag: flag; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_r <= 0; + else + data_r <= valid_in? data_r: data_in; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else + data_out <= flag&&valid_in? {data_r, data_in}: data_out; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_out <= 0; + else + valid_out <= flag&&valid_in; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob010_integer_multiple_data_bit_width_conversion_8to16_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob010_integer_multiple_data_bit_width_conversion_8to16_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..871ba50d8fffed3bb0f14adbfb85fad0277d59a1 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob010_integer_multiple_data_bit_width_conversion_8to16_ref.sv @@ -0,0 +1,40 @@ +module RefModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [15:0] data_out +); + reg [7:0] data_r; + reg flag; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + flag <= 0; + else + flag <= valid_in? ~flag: flag; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_r <= 0; + else + data_r <= valid_in? data_in: data_r; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else + data_out <= flag&&valid_in? {data_r, data_in}: data_out; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_out <= 0; + else + valid_out <= flag&&valid_in; + end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob010_integer_multiple_data_bit_width_conversion_8to16_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob010_integer_multiple_data_bit_width_conversion_8to16_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..799c6e6a4757f8af250d91797e0be99a8d88a037 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob010_integer_multiple_data_bit_width_conversion_8to16_test.sv @@ -0,0 +1,181 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_in, + output logic [7:0] data_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_in = 0; + data_in = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test first pair: 0xAA, 0x55 (should form 0xAA55) + valid_in = 1; + data_in = 8'hAA; @(posedge clk); + data_in = 8'h55; @(posedge clk); + + // Test second pair: 0x12, 0x34 (should form 0x1234) + data_in = 8'h12; @(posedge clk); + data_in = 8'h34; @(posedge clk); + + // Test with valid_in low (should ignore data) + valid_in = 0; + data_in = 8'hFF; @(posedge clk); + data_in = 8'hEE; @(posedge clk); + + // Test third pair: 0x78, 0x9A (should form 0x789A) + valid_in = 1; + data_in = 8'h78; @(posedge clk); + data_in = 8'h9A; @(posedge clk); + + // Test fourth pair: 0xBC, 0xDE (should form 0xBCDE) + data_in = 8'hBC; @(posedge clk); + data_in = 8'hDE; @(posedge clk); + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + valid_in <= $random; + data_in <= $urandom % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_valid_out; + int errortime_valid_out; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_in; + logic [7:0] data_in; + logic valid_out_ref; + logic valid_out_dut; + logic [15:0] data_out_ref; + logic [15:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_in, data_in, valid_out_ref, valid_out_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_in, + .data_in ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_valid_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_out", stats1.errors_valid_out, stats1.errortime_valid_out); + else $display("Hint: Output '%s' has no mismatches.", "valid_out"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { valid_out_ref, data_out_ref } === ( { valid_out_ref, data_out_ref } ^ { valid_out_dut, data_out_dut } ^ { valid_out_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (valid_out_ref !== ( valid_out_ref ^ valid_out_dut ^ valid_out_ref )) + begin if (stats1.errors_valid_out == 0) stats1.errortime_valid_out = $time; + stats1.errors_valid_out = stats1.errors_valid_out+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob011_4-bit_carry_look-ahead_adder_circuit_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob011_4-bit_carry_look-ahead_adder_circuit_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..6a41bbdc0fb4be3249e3d825790abdf84a589f47 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob011_4-bit_carry_look-ahead_adder_circuit_prompt.txt @@ -0,0 +1,45 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input A_in (4 bits) + - input B_in (4 bits) + - input C_1 + - output S (4 bits) + - output CO + +Implement a 4-bit carry look-ahead adder using gate-level description in +Verilog. The logical expressions for the carry look-ahead adder are as +follows: + +Intermediate variables: G_i = A_i AND B_i, P_i = A_i XOR B_i + +Sum: S_i = P_i XOR C_(i-1), Carry: C_i = G_i OR (P_i AND C_(i-1)) + +Where C_i and C_(i-1) represent carry signals at positions i and i-1. + +The module should be implemented using gate-level primitives (AND, OR, XOR) +rather than behavioral descriptions. All signals should be declared as wire +type. + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input [3:0] A_in , + input [3:0] B_in , + input C_1 , + + output wire CO , + output wire [3:0] S +); + wire [3:0] G,P,C; + assign P=A_in^C_1; + assign G=A_in&B_in; + + assign C={G[3]|(P[3]&C[2]), G[2]|(P[2]&C[1]), G[1]|(P[1]&C[0]), G[0]|(P[0]&C_1)}; + assign S={P[3]^C[2], P[2]^C[1], P[1]^C[0], P[0]^C_1}; + assign CO=C[3]; + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob011_4-bit_carry_look-ahead_adder_circuit_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob011_4-bit_carry_look-ahead_adder_circuit_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..c104cd362510376fdde777a47447ac151aa3f397 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob011_4-bit_carry_look-ahead_adder_circuit_ref.sv @@ -0,0 +1,18 @@ + +module RefModule( + input [3:0] A_in , + input [3:0] B_in , + input C_1 , + + output wire CO , + output wire [3:0] S +); + wire [3:0] G,P,C; + assign P=A_in^B_in; + assign G=A_in&B_in; + + assign C={G[3]|(P[3]&C[2]), G[2]|(P[2]&C[1]), G[1]|(P[1]&C[0]), G[0]|(P[0]&C_1)}; + assign S={P[3]^C[2], P[2]^C[1], P[1]^C[0], P[0]^C_1}; + assign CO=C[3]; + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob011_4-bit_carry_look-ahead_adder_circuit_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob011_4-bit_carry_look-ahead_adder_circuit_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..01cb3f2bee979edffaa00c96aa755bdf89161b11 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob011_4-bit_carry_look-ahead_adder_circuit_test.sv @@ -0,0 +1,220 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic [3:0] A_in, + output logic [3:0] B_in, + output logic C_1 + // === End your code here === +); + // === Start your code here === + initial begin + A_in <= 0; + B_in <= 0; + C_1 <= 0; + + // Test basic addition cases + @(posedge clk); + A_in <= 4'b0001; + B_in <= 4'b0001; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0010; + B_in <= 4'b0011; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0100; + B_in <= 4'b0100; + C_1 <= 1; + @(posedge clk); + + A_in <= 4'b1000; + B_in <= 4'b1000; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b1111; + B_in <= 4'b0001; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b1111; + B_in <= 4'b1111; + C_1 <= 1; + @(posedge clk); + + A_in <= 4'b1010; + B_in <= 4'b0101; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0110; + B_in <= 4'b1001; + C_1 <= 1; + @(posedge clk); + + // Test edge cases + A_in <= 4'b0000; + B_in <= 4'b0000; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0000; + B_in <= 4'b0000; + C_1 <= 1; + @(posedge clk); + + A_in <= 4'b1111; + B_in <= 4'b0000; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0000; + B_in <= 4'b1111; + C_1 <= 1; + @(posedge clk); + + // Random test sequence + repeat(200) @(posedge clk) begin + A_in <= $random & 15; // 4-bit random + B_in <= $random & 15; // 4-bit random + C_1 <= $random & 1; // 1-bit random + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_S; + int errortime_S; + int errors_CO; + int errortime_CO; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic [3:0] A_in; + logic [3:0] B_in; + logic C_1; + logic [3:0] S_ref; + logic [3:0] S_dut; + logic CO_ref; + logic CO_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, A_in, B_in, C_1, S_ref, S_dut, CO_ref, CO_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .A_in, + .B_in, + .C_1 + ); + + RefModule good1 ( + .A_in, + .B_in, + .C_1, + .CO(CO_ref), + .S(S_ref) + ); + + TopModule top_module1 ( + .A_in, + .B_in, + .C_1, + .CO(CO_dut), + .S(S_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_S) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "S", stats1.errors_S, stats1.errortime_S); + else $display("Hint: Output '%s' has no mismatches.", "S"); + if (stats1.errors_CO) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "CO", stats1.errors_CO, stats1.errortime_CO); + else $display("Hint: Output '%s' has no mismatches.", "CO"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { S_ref, CO_ref } === ( { S_ref, CO_ref } ^ { S_dut, CO_dut } ^ { S_ref, CO_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (S_ref !== ( S_ref ^ S_dut ^ S_ref )) + begin if (stats1.errors_S == 0) stats1.errortime_S = $time; + stats1.errors_S = stats1.errors_S+1'b1; end + if (CO_ref !== ( CO_ref ^ CO_dut ^ CO_ref )) + begin if (stats1.errors_CO == 0) stats1.errortime_CO = $time; + stats1.errors_CO = stats1.errors_CO+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob012_state_machine_overlapping_sequence_detection_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob012_state_machine_overlapping_sequence_detection_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..cb00714da5f7bc363ac799f38b4b3cc32bde2ab8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob012_state_machine_overlapping_sequence_detection_prompt.txt @@ -0,0 +1,64 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst + - input data + - output reg flag + +The module should implement a state machine that detects the sequence "1011" +with overlapping detection capability. The state machine should detect +multiple occurrences of the sequence even when they overlap. reset is active low. + +The sequence detection should work as follows: +- Monitor the input data stream continuously +- Detect when the sequence "1011" appears in the input +- Support overlapping detection: "10110111" should be detected twice +- Output flag signal should be high for one clock cycle when the sequence + is detected, starting from the next clock cycle after detection + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input wire clk , + input wire rst , + input wire data , + output reg flag +); +//*************code***********// + parameter S0=0, S1=1, S2=2, S3=2, S4=4; + reg [2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= S0; + else + state <= nstate; + end + + always@(*) begin + if(~rst) + nstate <= S0; + else + case(state) + S0 : nstate <= data? S1: S0; + S1 : nstate <= data? S1: S2; + S2 : nstate <= data? S3: S0; + S3 : nstate <= data? S4: S2; + S4 : nstate <= data? S1: S2; + default: nstate <= S0; + endcase + end + + always@(posedge clk or negedge rst) begin + if(~rst) + flag <= 0; + else + flag <= state==S4; + end + +//*************code***********// +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob012_state_machine_overlapping_sequence_detection_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob012_state_machine_overlapping_sequence_detection_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..9e6792cac0abf39247e8718dc9d59f9e638ff7b8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob012_state_machine_overlapping_sequence_detection_ref.sv @@ -0,0 +1,40 @@ +module RefModule( + input wire clk , + input wire rst , + input wire data , + output reg flag +); +//*************code***********// + parameter S0=0, S1=1, S2=2, S3=3, S4=4; + reg [2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= S0; + else + state <= nstate; + end + + always@(*) begin + if(~rst) + nstate <= S0; + else + case(state) + S0 : nstate <= data? S1: S0; + S1 : nstate <= data? S1: S2; + S2 : nstate <= data? S3: S0; + S3 : nstate <= data? S4: S2; + S4 : nstate <= data? S1: S2; + default: nstate <= S0; + endcase + end + + always@(posedge clk or negedge rst) begin + if(~rst) + flag <= 0; + else + flag <= state==S4; + end + +//*************code***********// +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob012_state_machine_overlapping_sequence_detection_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob012_state_machine_overlapping_sequence_detection_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..a837d96a98935a96d3a2d8d9b3e47d692a7e56ee --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob012_state_machine_overlapping_sequence_detection_test.sv @@ -0,0 +1,164 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst, + output logic data + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + data = 0; + repeat(10) @(posedge clk); + rst <= 1; + @(posedge clk); + + // Test sequence "1011" - should detect once + data <= 1; @(posedge clk); // First bit + data <= 0; @(posedge clk); // Second bit + data <= 1; @(posedge clk); // Third bit + data <= 1; @(posedge clk); // Fourth bit - should detect here + + // Test overlapping sequences "10110111" - should detect twice + data <= 0; @(posedge clk); // Fifth bit + data <= 1; @(posedge clk); // Sixth bit + data <= 1; @(posedge clk); // Seventh bit - should detect here + data <= 1; @(posedge clk); // Eighth bit + + // Test non-matching sequence + data <= 0; @(posedge clk); + data <= 0; @(posedge clk); + data <= 1; @(posedge clk); + data <= 0; @(posedge clk); + + // Test another "1011" sequence + data <= 1; @(posedge clk); + data <= 0; @(posedge clk); + data <= 1; @(posedge clk); + data <= 1; @(posedge clk); // Should detect here + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + data <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_flag; + int errortime_flag; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic data; + logic flag_ref; + logic flag_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, data, flag_ref, flag_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst, + .data ); + + RefModule good1 ( + .clk, + .rst, + .data, + .flag(flag_ref) ); + + TopModule top_module1 ( + .clk, + .rst, + .data, + .flag(flag_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_flag) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "flag", stats1.errors_flag, stats1.errortime_flag); + else $display("Hint: Output '%s' has no mismatches.", "flag"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { flag_ref } === ( { flag_ref } ^ { flag_dut } ^ { flag_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (flag_ref !== ( flag_ref ^ flag_dut ^ flag_ref )) + begin if (stats1.errors_flag == 0) stats1.errortime_flag = $time; + stats1.errors_flag = stats1.errors_flag+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob013_least_common_multiple_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob013_least_common_multiple_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..bd817d9ddfebd1afa0e5843e48c72ce5a5d3fdf5 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob013_least_common_multiple_prompt.txt @@ -0,0 +1,125 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input A (8 bits) + - input B (8 bits) + - input vld_in + - output lcm_out (16 bits) + - output mcd_out (8 bits) + - output vld_out + +Design a sequential circuit that takes two unsigned numbers as input and +outputs their least common multiple (LCM) and greatest common divisor (GCD). +The bit width can be determined by parameter DATA_W. For this implementation, +DATA_W is 8, so A, B, and mcd_out are 8 bits while lcm_out is 16 bits. + +The circuit should operate as follows: +- When vld_in is asserted, the input data A and B are valid and should be + processed +- The circuit computes both the LCM and GCD of the two input numbers +- When the computation is complete, vld_out should be asserted to indicate + that the output data is valid +- Before the output is valid, lcm_out should be driven to high impedance + ('hz). This is required in this design; otherwise there will be a problem +- The rst_n signal is an active-low reset signal + +Assume all sequential logic is triggered on the positive edge of the clock. + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule#( +parameter DATA_W = 8) +( +input [DATA_W-1:0] A, +input [DATA_W-1:0] B, +input vld_in, +input rst_n, +input clk, +output wire [DATA_W*2-1:0] lcm_out, +output wire [DATA_W-1:0] mcd_out, +output reg vld_out +); + reg [DATA_W*2-1:0] A_reg; + reg [DATA_W*2-1:0] B_reg; + reg [DATA_W*2-1:0] mcd_out_r1; + reg [DATA_W*2-1:0] mult_reg; + reg [1:0] c_state, n_state; + parameter IDLE = 'd0, lcm1 = 'd1, Finish = 'd1; + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) c_state <= IDLE; + else c_state <= n_state; + end + + always @(*) begin + case(c_state) + IDLE : if(vld_in) n_state = lcm1; + else n_state = IDLE; + lcm1 : if(A_reg == B_reg) n_state = Finish; + else n_state = lcm1; + Finish : n_state = IDLE; + default : n_state = IDLE; + endcase + end + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + else begin + case(c_state) + IDLE : + if(vld_in) begin + A_reg <= A; + B_reg <= B; + mult_reg <= A * B; + mcd_out_r1 <= 0; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + mult_reg <= mult_reg; + mcd_out_r1 <= 0; + end + lcm1 : + if(A_reg > B_reg) begin + A_reg <= A_reg - B_reg; + B_reg <= B_reg; + end + else if(A_reg < B_reg) begin + B_reg <= B_reg - A_reg; + A_reg <= A_reg; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + end + Finish : mcd_out_r1 <= A_reg; + + default : begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + endcase + end + end + assign mcd_out = mcd_out_r1; + assign lcm_out = (mcd_out_r1 == 'd0) ? 'hz : (mult_reg / mcd_out_r1); // If the GCD register is 0, output high impedance, otherwise the circuit will have problems + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) vld_out <= 1'b0; + else if(c_state == Finish) vld_out <= 1'b1; + else vld_out <= 1'b0; + end + +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob013_least_common_multiple_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob013_least_common_multiple_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..c6c378d54ca460b83b669849fc4f89e7a2d7b746 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob013_least_common_multiple_ref.sv @@ -0,0 +1,92 @@ + +module RefModule#( +parameter DATA_W = 8) +( +input [DATA_W-1:0] A, +input [DATA_W-1:0] B, +input vld_in, +input rst_n, +input clk, +output wire [DATA_W*2-1:0] lcm_out, +output wire [DATA_W-1:0] mcd_out, +output reg vld_out +); + reg [DATA_W*2-1:0] A_reg; + reg [DATA_W*2-1:0] B_reg; + reg [DATA_W*2-1:0] mcd_out_r1; + reg [DATA_W*2-1:0] mult_reg; + reg [1:0] c_state, n_state; + parameter IDLE = 'd0, lcm1 = 'd1, Finish = 'd2; + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) c_state <= IDLE; + else c_state <= n_state; + end + + always @(*) begin + case(c_state) + IDLE : if(vld_in) n_state = lcm1; + else n_state = IDLE; + lcm1 : if(A_reg == B_reg) n_state = Finish; + else n_state = lcm1; + Finish : n_state = IDLE; + default : n_state = IDLE; + endcase + end + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + else begin + case(c_state) + IDLE : + if(vld_in) begin + A_reg <= A; + B_reg <= B; + mult_reg <= A * B; + mcd_out_r1 <= 0; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + mult_reg <= mult_reg; + mcd_out_r1 <= 0; + end + lcm1 : + if(A_reg > B_reg) begin + A_reg <= A_reg - B_reg; + B_reg <= B_reg; + end + else if(A_reg < B_reg) begin + B_reg <= B_reg - A_reg; + A_reg <= A_reg; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + end + Finish : mcd_out_r1 <= A_reg; + + default : begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + endcase + end + end + assign mcd_out = mcd_out_r1; + assign lcm_out = (mcd_out_r1 == 'd0) ? 'hz : (mult_reg / mcd_out_r1); // If the GCD register is 0, output high impedance, otherwise the circuit will have problems + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) vld_out <= 1'b0; + else if(c_state == Finish) vld_out <= 1'b1; + else vld_out <= 1'b0; + end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob013_least_common_multiple_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob013_least_common_multiple_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..ca87e1e8e7186a4ecb91b6bfe6e51215fbae87f6 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob013_least_common_multiple_test.sv @@ -0,0 +1,237 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [7:0] A, + output logic [7:0] B, + output logic vld_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n <= 0; + A <= 0; + B <= 0; + vld_in <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic cases + A <= 8'd12; + B <= 8'd8; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); // Wait for computation + + A <= 8'd15; + B <= 8'd25; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd7; + B <= 8'd11; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd16; + B <= 8'd24; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + + // Test edge cases + A <= 8'd1; + B <= 8'd1; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd255; + B <= 8'd1; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd0; + B <= 8'd5; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + // Random test sequence + repeat(50) @(posedge clk) begin + A <= $random & 255; // 8-bit random + B <= $random & 255; // 8-bit random + vld_in <= $random & 1; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_lcm_out; + int errortime_lcm_out; + int errors_mcd_out; + int errortime_mcd_out; + int errors_vld_out; + int errortime_vld_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [7:0] A; + logic [7:0] B; + logic vld_in; + logic [15:0] lcm_out_ref; + logic [15:0] lcm_out_dut; + logic [7:0] mcd_out_ref; + logic [7:0] mcd_out_dut; + logic vld_out_ref; + logic vld_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, A, B, vld_in, lcm_out_ref, lcm_out_dut, mcd_out_ref, mcd_out_dut, vld_out_ref, vld_out_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .A, + .B, + .vld_in + ); + + RefModule good1 ( + .A, + .B, + .vld_in, + .rst_n, + .clk, + .lcm_out(lcm_out_ref), + .mcd_out(mcd_out_ref), + .vld_out(vld_out_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .A, + .B, + .vld_in, + .lcm_out(lcm_out_dut), + .mcd_out(mcd_out_dut), + .vld_out(vld_out_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_lcm_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "lcm_out", stats1.errors_lcm_out, stats1.errortime_lcm_out); + else $display("Hint: Output '%s' has no mismatches.", "lcm_out"); + if (stats1.errors_mcd_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "mcd_out", stats1.errors_mcd_out, stats1.errortime_mcd_out); + else $display("Hint: Output '%s' has no mismatches.", "mcd_out"); + if (stats1.errors_vld_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "vld_out", stats1.errors_vld_out, stats1.errortime_vld_out); + else $display("Hint: Output '%s' has no mismatches.", "vld_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + // Where the reference drives lcm_out='hz (the spec requires 'hz while the output is not valid) the DUT must drive + // 'hz too; the X-only tolerance below would turn a 'z reference into 'x and reject every design. + wire lcm_ok = (lcm_out_ref === 16'bz) ? (lcm_out_dut === 16'bz) : (lcm_out_ref === ( lcm_out_ref ^ lcm_out_dut ^ lcm_out_ref )); + assign tb_match = lcm_ok && ( { mcd_out_ref, vld_out_ref } === ( { mcd_out_ref, vld_out_ref } ^ { mcd_out_dut, vld_out_dut } ^ { mcd_out_ref, vld_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (!lcm_ok) + begin if (stats1.errors_lcm_out == 0) stats1.errortime_lcm_out = $time; + stats1.errors_lcm_out = stats1.errors_lcm_out+1'b1; end + if (mcd_out_ref !== ( mcd_out_ref ^ mcd_out_dut ^ mcd_out_ref )) + begin if (stats1.errors_mcd_out == 0) stats1.errortime_mcd_out = $time; + stats1.errors_mcd_out = stats1.errors_mcd_out+1'b1; end + if (vld_out_ref !== ( vld_out_ref ^ vld_out_dut ^ vld_out_ref )) + begin if (stats1.errors_vld_out == 0) stats1.errortime_vld_out = $time; + stats1.errors_vld_out = stats1.errors_vld_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob014_sequence_generator_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob014_sequence_generator_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..8fbc46c4d82680948319588446f1195ef01258f8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob014_sequence_generator_prompt.txt @@ -0,0 +1,46 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output data + +Implement a module that continuously outputs the sequence 001011 in a +cyclic manner. + +The circuit should generate the pattern as follows: +- Output '0', then '0', then '1', then '0', then '1', then '1' +- After outputting the last '1', the sequence repeats from the beginning +- The sequence 001011 repeats indefinitely + +The rst_n signal is an active-low reset signal. When reset is asserted, +the sequence should restart from the beginning. The output is 0 while +rst_n is low, and the first rising clock edge after release outputs the +sequence's first '0'. Assume all sequential +logic is triggered on the positive edge of the clock. + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + output reg data + ); + + reg [5:0] q; + + always@(posedge clk or negedge rst_n) + if (!rst_n) + q <= 6'b010110; + else + q <= {q[4:0],q[5]}; + + always@(posedge clk or negedge rst_n) + if (!rst_n) + data <= 1'd0; + else + data <= q[5]; +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob014_sequence_generator_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob014_sequence_generator_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..139d5513975bbb32f359727bfb417bd3d7338b02 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob014_sequence_generator_ref.sv @@ -0,0 +1,21 @@ + +module RefModule( + input clk, + input rst_n, + output reg data + ); + + reg [5:0] q; + + always@(posedge clk or negedge rst_n) + if (!rst_n) + q <= 6'b001011; + else + q <= {q[4:0],q[5]}; + + always@(posedge clk or negedge rst_n) + if (!rst_n) + data <= 1'd0; + else + data <= q[5]; +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob014_sequence_generator_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob014_sequence_generator_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..d674f42565304d0f2bcf1950261451fbe1fe02a7 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob014_sequence_generator_test.sv @@ -0,0 +1,139 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Let it run for multiple cycles + repeat(200) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_data; + int errortime_data; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic data_ref; + logic data_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data_ref, data_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n + ); + + RefModule good1 ( + .clk, + .rst_n, + .data(data_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .data(data_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_data) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data", stats1.errors_data, stats1.errortime_data); + else $display("Hint: Output '%s' has no mismatches.", "data"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { data_ref } === ( { data_ref } ^ { data_dut } ^ { data_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (data_ref !== ( data_ref ^ data_dut ^ data_ref )) + begin if (stats1.errors_data == 0) stats1.errortime_data = $time; + stats1.errors_data = stats1.errors_data+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob015_vending_machine2_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob015_vending_machine2_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..c070de58d8d1283867d3de9905a40ff3f7d9f1fd --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob015_vending_machine2_prompt.txt @@ -0,0 +1,102 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst + - input d1 + - input d2 + - input sel + - output reg out1 + - output reg out2 + - output reg out3 + +The module should implement a vending machine that accepts two types of +currency (0.5 yuan and 1 yuan) and dispenses two types of beverages +(1.5 yuan and 2.5 yuan) with change dispensing capability. +reset is active low + +The vending machine should work as follows: +- Input currency: d1 represents 0.5 yuan, d2 represents 1 yuan +- Beverage selection: sel chooses between two beverages +- Beverage prices: Beverage 1 costs 1.5 yuan, Beverage 2 costs 2.5 yuan +- Change dispensing: Only 0.5 yuan coins are dispensed as change + +Currency input behavior: +- Input currency signals (d1, d2) are automatically edge-detected +- Each currency input produces a pulse signal that goes high on clock + rising edge and low on clock falling edge +- The machine accumulates the total amount of money inserted + +Beverage dispensing: +- out1: Dispenses beverage 1 (1.5 yuan) +- out2: Dispenses beverage 2 (2.5 yuan) +- out3: Dispenses change (0.5 yuan coins only) + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input wire clk , + input wire rst , + input wire d1 , + input wire d2 , + input wire sel , + + output reg out1, + output reg out2, + output reg out3 +); +//*************code***********// + + parameter S0=0, S0_5=1, S1=2, S1_5=3, S2=4, S2_5=5, S3=6; + reg[2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= 0; + else + state <= nstate; + end + + always@(*) begin + case(state) + S0 : nstate = d1? S0_5: + d2? S1: + nstate; + S0_5 : nstate = d1? S1: + d2? S1_5: + nstate; + S1 : nstate = d1? S1_5: + d2? S2: + nstate; + S1_5 : nstate = ~sel? S0: + d1? S2: + d2? S2_5: + nstate; + S2 : nstate = ~sel? S0: + d1? S2_5: + d2? S3: + nstate; + default: nstate = S0; + endcase + end + + always@(*) begin + if(~rst) begin + {out1, out2, out3} = 3'b000; + end + else begin + case(state) + S0, S0_5, S1: {out1, out2, out3} = 0; + S1_5 : {out1, out2, out3} = ~sel? 3'b100: 3'b000; + S2 : {out1, out2, out3} = ~sel? 3'b101: 3'b000; + S2_5 : {out1, out2, out3} = ~sel? 3'b101: 3'b011; + S3 : {out1, out2, out3} = ~sel? 3'b101: 3'b001; + default : {out1, out2, out3} = 3'b000; + endcase + end + end +//*************code***********// +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob015_vending_machine2_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob015_vending_machine2_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..a1ac1b88ccb70a8aeabfb77968ec0cc89b517377 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob015_vending_machine2_ref.sv @@ -0,0 +1,64 @@ + +module RefModule( + input wire clk , + input wire rst , + input wire d1 , + input wire d2 , + input wire sel , + + output reg out1, + output reg out2, + output reg out3 +); +//*************code***********// + + parameter S0=0, S0_5=1, S1=2, S1_5=3, S2=4, S2_5=5, S3=6; + reg[2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= 0; + else + state <= nstate; + end + + always@(*) begin + case(state) + S0 : nstate = d1? S0_5: + d2? S1: + nstate; + S0_5 : nstate = d1? S1: + d2? S1_5: + nstate; + S1 : nstate = d1? S1_5: + d2? S2: + nstate; + S1_5 : nstate = ~sel? S0: + d1? S2: + d2? S2_5: + nstate; + S2 : nstate = ~sel? S0: + d1? S2_5: + d2? S3: + nstate; + default: nstate = S0; + endcase + end + + always@(*) begin + if(~rst) begin + {out1, out2, out3} = 3'b000; + end + else begin + case(state) + S0, S0_5, S1: {out1, out2, out3} = 0; + S1_5 : {out1, out2, out3} = ~sel? 3'b100: 3'b000; + S2 : {out1, out2, out3} = ~sel? 3'b101: 3'b000; + S2_5 : {out1, out2, out3} = ~sel? 3'b101: 3'b010; + S3 : {out1, out2, out3} = ~sel? 3'b101: 3'b011; + default : {out1, out2, out3} = 3'b000; + endcase + end + end +//*************code***********// +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob015_vending_machine2_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob015_vending_machine2_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..17d6dee4540eb7c8bbb9f648823598a0a643812f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob015_vending_machine2_test.sv @@ -0,0 +1,214 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst, + output logic d1, + output logic d2, + output logic sel + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + d1 = 0; + d2 = 0; + sel = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test case 1: Insert 1.5 yuan and select beverage 1 (sel=0) + d1 = 1; @(posedge clk); // Insert 0.5 yuan + d1 = 0; @(posedge clk); + d1 = 1; @(posedge clk); // Insert another 0.5 yuan + d1 = 0; @(posedge clk); + d1 = 1; @(posedge clk); // Insert third 0.5 yuan + d1 = 0; @(posedge clk); + sel = 0; @(posedge clk); // Select beverage 1 + sel = 0; @(posedge clk); // Should dispense beverage 1 + + // Test case 2: Insert 2.5 yuan and select beverage 2 (sel=1) + d2 = 1; @(posedge clk); // Insert 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert another 1 yuan + d2 = 0; @(posedge clk); + d1 = 1; @(posedge clk); // Insert 0.5 yuan + d1 = 0; @(posedge clk); + sel = 1; @(posedge clk); // Select beverage 2 + sel = 1; @(posedge clk); // Should dispense beverage 2 + + // Test case 3: Insert 2 yuan and select beverage 1 (sel=0) - should get change + d2 = 1; @(posedge clk); // Insert 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert another 1 yuan + d2 = 0; @(posedge clk); + sel = 0; @(posedge clk); // Select beverage 1 + sel = 0; @(posedge clk); // Should dispense beverage 1 + 0.5 yuan change + + // Test case 4: Insert 3 yuan and select beverage 2 (sel=1) - should get change + d2 = 1; @(posedge clk); // Insert 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert another 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert third 1 yuan + d2 = 0; @(posedge clk); + sel = 1; @(posedge clk); // Select beverage 2 + sel = 1; @(posedge clk); // Should dispense beverage 2 + 0.5 yuan change + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + d1 <= $random; + d2 <= $random; + sel <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_out1; + int errortime_out1; + int errors_out2; + int errortime_out2; + int errors_out3; + int errortime_out3; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic d1; + logic d2; + logic sel; + logic out1_ref; + logic out2_ref; + logic out3_ref; + logic out1_dut; + logic out2_dut; + logic out3_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, d1, d2, sel, out1_ref, out2_ref, out3_ref, out1_dut, out2_dut, out3_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst, + .d1, + .d2, + .sel ); + + RefModule good1 ( + .clk, + .rst, + .d1, + .d2, + .sel, + .out1(out1_ref), + .out2(out2_ref), + .out3(out3_ref) ); + + TopModule top_module1 ( + .clk, + .rst, + .d1, + .d2, + .sel, + .out1(out1_dut), + .out2(out2_dut), + .out3(out3_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_out1) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out1", stats1.errors_out1, stats1.errortime_out1); + else $display("Hint: Output '%s' has no mismatches.", "out1"); + if (stats1.errors_out2) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out2", stats1.errors_out2, stats1.errortime_out2); + else $display("Hint: Output '%s' has no mismatches.", "out2"); + if (stats1.errors_out3) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out3", stats1.errors_out3, stats1.errortime_out3); + else $display("Hint: Output '%s' has no mismatches.", "out3"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { out1_ref, out2_ref, out3_ref } === ( { out1_ref, out2_ref, out3_ref } ^ { out1_dut, out2_dut, out3_dut } ^ { out1_ref, out2_ref, out3_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (out1_ref !== ( out1_ref ^ out1_dut ^ out1_ref )) + begin if (stats1.errors_out1 == 0) stats1.errortime_out1 = $time; + stats1.errors_out1 = stats1.errors_out1+1'b1; end + if (out2_ref !== ( out2_ref ^ out2_dut ^ out2_ref )) + begin if (stats1.errors_out2 == 0) stats1.errortime_out2 = $time; + stats1.errors_out2 = stats1.errors_out2+1'b1; end + if (out3_ref !== ( out3_ref ^ out3_dut ^ out3_ref )) + begin if (stats1.errors_out3 == 0) stats1.errortime_out3 = $time; + stats1.errors_out3 = stats1.errors_out3+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob016_odd-number_division_with_a_duty_cycle_of_half_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob016_odd-number_division_with_a_duty_cycle_of_half_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..798e6a35a5957c33864008f145d2d4506397a7c1 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob016_odd-number_division_with_a_duty_cycle_of_half_prompt.txt @@ -0,0 +1,81 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk_in + - input rst + - output clk_out7 + +The module should implement a clock divider that produces a 7:1 frequency +division with a 50% duty cycle output. reset is active low. + +The clock divider should work as follows: +- Input clock: clk_in (reference clock signal) +- Output clock: clk_out7 (divided clock signal) +- Division ratio: 7:1 (output frequency is 1/7 of input frequency) +- Duty cycle: 50% (output is high for half the period, low for half the period) + +Clock division behavior: +- The output clk_out7 should toggle at 1/7 the frequency of clk_in +- The output should maintain a 50% duty cycle (equal high and low times) +- The output should be synchronized to the input clock clk_in +- The division should be continuous and stable + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule + #(parameter N = 7) + ( + input wire rst , + input wire clk_in, + output wire clk_out7 + ); + + + + reg [3:0] cnt ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + cnt <= 'b0 ; + end + else if (cnt == N-1) begin + cnt <= 'b0 ; + end + else begin + cnt <= cnt + 1'b1 ; + end + end + + reg clkp ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + clkp <= 1'b0 ; + end + else if (cnt == (N>>1)) begin + clkp <= 1 ; + end + else if (cnt == N-1) begin + clkp <= 0 ; + end + end + + + reg clkn ; + always @(negedge clk_in or negedge rst) begin + if (!rst) begin + clkn <= 1'b0 ; + end + else if (cnt == (N>>2) ) begin + clkn <= 1 ; + end + else if (cnt == N-1) begin + clkn <= 0 ; + end + end + + + assign clk_out7 = clkp | clkn ; + + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob016_odd-number_division_with_a_duty_cycle_of_half_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob016_odd-number_division_with_a_duty_cycle_of_half_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..8c5863988ee32c234c730e1f807a42794cb70c74 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob016_odd-number_division_with_a_duty_cycle_of_half_ref.sv @@ -0,0 +1,56 @@ + +module RefModule + #(parameter N = 7) + ( + input wire rst , + input wire clk_in, + output wire clk_out7 + ); + + + + reg [3:0] cnt ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + cnt <= 'b0 ; + end + else if (cnt == N-1) begin + cnt <= 'b0 ; + end + else begin + cnt <= cnt + 1'b1 ; + end + end + + reg clkp ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + clkp <= 1'b0 ; + end + else if (cnt == (N>>1)) begin + clkp <= 1 ; + end + else if (cnt == N-1) begin + clkp <= 0 ; + end + end + + + reg clkn ; + always @(negedge clk_in or negedge rst) begin + if (!rst) begin + clkn <= 1'b0 ; + end + else if (cnt == (N>>1) ) begin + clkn <= 1 ; + end + else if (cnt == N-1) begin + clkn <= 0 ; + end + end + + + assign clk_out7 = clkp | clkn ; + + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob016_odd-number_division_with_a_duty_cycle_of_half_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob016_odd-number_division_with_a_duty_cycle_of_half_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..f9718e4354229f62c57df0869666fd678dadda81 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob016_odd-number_division_with_a_duty_cycle_of_half_test.sv @@ -0,0 +1,133 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test clock divider for multiple cycles + repeat(300) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_clk_out7; + int errortime_clk_out7; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic clk_out7_ref; + logic clk_out7_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, clk_out7_ref, clk_out7_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst ); + + RefModule good1 ( + .rst, + .clk_in(clk), + .clk_out7(clk_out7_ref) ); + + TopModule top_module1 ( + .clk_in(clk), + .rst, + .clk_out7(clk_out7_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_clk_out7) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "clk_out7", stats1.errors_clk_out7, stats1.errortime_clk_out7); + else $display("Hint: Output '%s' has no mismatches.", "clk_out7"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { clk_out7_ref } === ( { clk_out7_ref } ^ { clk_out7_dut } ^ { clk_out7_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (clk_out7_ref !== ( clk_out7_ref ^ clk_out7_dut ^ clk_out7_ref )) + begin if (stats1.errors_clk_out7 == 0) stats1.errortime_clk_out7 = $time; + stats1.errors_clk_out7 = stats1.errors_clk_out7+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob017_arbitrary_fractional_frequency_division_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob017_arbitrary_fractional_frequency_division_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..f31ba18c6e8284ff190abef5277ab2070aa2dca6 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob017_arbitrary_fractional_frequency_division_prompt.txt @@ -0,0 +1,89 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk_in + - input rst + - output clk_out + +The module should implement an arbitrary fractional frequency divider that +can achieve non-integer division ratios, such as 8.7x frequency division. + +The fractional frequency divider should work as follows: +- Input clock: clk_in (reference clock signal) +- Output clock: clk_out (divided clock signal) +- Division ratio: 8.7x (output frequency is 1/8.7 of input frequency) +- Reset: rst is active low + +Fractional division principle: +Since hardware cannot perform fractional operations (e.g., 2.1 clock cycles +or 3.3 registers), fractional division cannot achieve exact fractional +periods for each output clock cycle. Instead, the implementation uses +the least common multiple approach to achieve the desired average frequency. + +For 8.7x division: +- Over 10 input clock cycles, the output should produce 53 output clock + cycles to achieve an average 8.7x division ratio +- This means: 53 output cycles / 10 input cycles = 5.3x, which when + inverted gives 1/5.3 ≈ 1/8.7x frequency division + +I give u some useful parameter: +parameter M_N = 8'd87; +parameter c89 = 8'd24; // 8/9 clock switching point +parameter div_e = 5'd8; //even period +parameter div_o = 5'd9; //odd period + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input wire clk_in, + input wire rst, + output wire clk_out +); + parameter M_N = 8'd87; + parameter c89 = 8'd24; // 8/9 clock switching point + parameter div_e = 5'd8; //Even period + parameter div_o = 5'd9; //Odd period +//*************code***********// + reg [3:0] clk_cnt; + reg [6:0] cyc_cnt; + reg div_flag; + reg clk_out_r; + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_cnt <= 0; + else if(~div_flag) + clk_cnt <= clk_cnt==(div_e-1)? 0: clk_cnt+1; + else + clk_cnt <= clk_cnt==(div_o-1)? 0: clk_cnt+1; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + cyc_cnt <= 0; + else + cyc_cnt <= cyc_cnt==(M_N-1)? 0: cyc_cnt+1; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + div_flag <= 0; + else + div_flag <= cyc_cnt==(M_N-1)||cyc_cnt==(c89-1)? ~div_flag: div_flag; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_out_r <= 0; + else if(~div_flag) + clk_out_r <= clk_cnt<=((div_e>>3)+1); + else + clk_out_r <= clk_cnt<=((div_o>>2)+1); + end + + assign clk_out = clk_out_r; +//*************code***********// +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob017_arbitrary_fractional_frequency_division_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob017_arbitrary_fractional_frequency_division_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..350db63a669018308988298e429b2b98dd23d2f0 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob017_arbitrary_fractional_frequency_division_ref.sv @@ -0,0 +1,51 @@ + +module RefModule( + input wire clk_in, + input wire rst, + output wire clk_out +); + parameter M_N = 8'd87; + parameter c89 = 8'd24; // 8/9 clock switching point + parameter div_e = 5'd8; //Even period + parameter div_o = 5'd9; //Odd period +//*************code***********// + reg [3:0] clk_cnt; + reg [6:0] cyc_cnt; + reg div_flag; + reg clk_out_r; + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_cnt <= 0; + else if(~div_flag) + clk_cnt <= clk_cnt==(div_e-1)? 0: clk_cnt+1; + else + clk_cnt <= clk_cnt==(div_o-1)? 0: clk_cnt+1; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + cyc_cnt <= 0; + else + cyc_cnt <= cyc_cnt==(M_N-1)? 0: cyc_cnt+1; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + div_flag <= 0; + else + div_flag <= cyc_cnt==(M_N-1)||cyc_cnt==(c89-1)? ~div_flag: div_flag; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_out_r <= 0; + else if(~div_flag) + clk_out_r <= clk_cnt<=((div_e>>2)+1); + else + clk_out_r <= clk_cnt<=((div_o>>2)+1); + end + + assign clk_out = clk_out_r; +//*************code***********// +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob017_arbitrary_fractional_frequency_division_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob017_arbitrary_fractional_frequency_division_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..ae750fdf45dc2f5ccc2f04a6e939bad25001a9e8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob017_arbitrary_fractional_frequency_division_test.sv @@ -0,0 +1,149 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test fractional frequency divider for multiple cycles + // The divider should achieve 8.7x division ratio + repeat(1000) @(posedge clk); + + // === Additional checks: active-low (asynchronous, per the quoted code) reset in mid-operation === + // Earlier stimulus only resets at start-up; rst changes at posedge+2 (no edge race). + #2 rst = 0; + repeat(3) @(posedge clk); + #2 rst = 1; + repeat(150) @(posedge clk); // reset again inside the 9-cycle (div_flag=1) region + #2 rst = 0; + @(posedge clk); + #2 rst = 1; + repeat(53) @(posedge clk); + #2 rst = 0; + repeat(2) @(posedge clk); + #2 rst = 1; + repeat(300) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_clk_out; + int errortime_clk_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic clk_out_ref; + logic clk_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, clk_out_ref, clk_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst ); + + RefModule good1 ( + .clk_in(clk), + .rst, + .clk_out(clk_out_ref) ); + + TopModule top_module1 ( + .clk_in(clk), + .rst, + .clk_out(clk_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_clk_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "clk_out", stats1.errors_clk_out, stats1.errortime_clk_out); + else $display("Hint: Output '%s' has no mismatches.", "clk_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { clk_out_ref } === ( { clk_out_ref } ^ { clk_out_dut } ^ { clk_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (clk_out_ref !== ( clk_out_ref ^ clk_out_dut ^ clk_out_ref )) + begin if (stats1.errors_clk_out == 0) stats1.errortime_clk_out = $time; + stats1.errors_clk_out = stats1.errors_clk_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob020_write_state_machine_two_stage_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob020_write_state_machine_two_stage_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..d88665b8e11cfc3d4c61ec49426386f22233aa82 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob020_write_state_machine_two_stage_prompt.txt @@ -0,0 +1,85 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst + - input data + - output reg flag + +The module should implement a Moore state machine using a two-stage +description method. The state machine has five states (S0, S1, S2, S3, S4) +and transitions based on the input data signal. + +The state machine should work as follows: +- Input signal: data (wire type) +- Output signal: flag (reg type) +- Reset: rst is active low +- Clock: clk for synchronous operation + +State transition table: + data +current state 1 0 + S0 next state S1 S0 + S1 S2 S1 + S2 S3 S2 + S3 S4 S3 + S4 S1 S0 + +Output logic: +- When current state is S4, the output flag should be 1 +- For all other states (S0, S1, S2, S3), the output flag should be 0 + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input wire clk , + input wire rst , + input wire data , + output reg flag +); + + parameter S0 = 'd0, S1 = 'd1, S2 = 'd2, S3 = 'd2 ,S4 = 'd4 ; + reg [2:0] current_state; + reg [2:0] next_state; + + always@(posedge clk or negedge rst)begin + if(rst == 1'b0)begin + current_state <= S0; + end + else begin + current_state <= next_state; + end + end + + always@(*)begin + case(current_state) + S0:begin + next_state = data ? S1 : S0; + flag = 1'b0; + end + S1:begin + next_state = data ? S2 : S1; + flag = 1'b0; + end + S2:begin + next_state = data ? S3 : S2; + flag = 1'b0; + end + S3:begin + next_state = data ? S4 : S3; + flag = 1'b0; + end + S4:begin + next_state = data ? S1 : S0; + flag = 1'b1; + end + default:begin + next_state = S0; + flag = 1'b0; + end + endcase + end + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob020_write_state_machine_two_stage_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob020_write_state_machine_two_stage_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..36947644c473bb11cbd0926164312fdc9d7305f3 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob020_write_state_machine_two_stage_ref.sv @@ -0,0 +1,51 @@ + +module RefModule( + input wire clk , + input wire rst , + input wire data , + output reg flag +); + + parameter S0 = 'd0, S1 = 'd1, S2 = 'd2, S3 = 'd3 ,S4 = 'd4 ; + reg [2:0] current_state; + reg [2:0] next_state; + + always@(posedge clk or negedge rst)begin + if(rst == 1'b0)begin + current_state <= S0; + end + else begin + current_state <= next_state; + end + end + + always@(*)begin + case(current_state) + S0:begin + next_state = data ? S1 : S0; + flag = 1'b0; + end + S1:begin + next_state = data ? S2 : S1; + flag = 1'b0; + end + S2:begin + next_state = data ? S3 : S2; + flag = 1'b0; + end + S3:begin + next_state = data ? S4 : S3; + flag = 1'b0; + end + S4:begin + next_state = data ? S1 : S0; + flag = 1'b1; + end + default:begin + next_state = S0; + flag = 1'b0; + end + endcase + end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob020_write_state_machine_two_stage_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob020_write_state_machine_two_stage_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..84ae9288b92210b890dc070ecc6902848b26b92f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob020_write_state_machine_two_stage_test.sv @@ -0,0 +1,174 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst, + output logic data + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + data = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test state transitions: S0->S1->S2->S3->S4 (flag=1) + data = 1; @(posedge clk); // S0->S1 + data = 1; @(posedge clk); // S1->S2 + data = 1; @(posedge clk); // S2->S3 + data = 1; @(posedge clk); // S3->S4 (flag should be 1) + + // Test S4->S1 transition (data=1) + data = 1; @(posedge clk); // S4->S1 + + // Test S4->S0 transition (data=0) + data = 1; @(posedge clk); // S1->S2 + data = 1; @(posedge clk); // S2->S3 + data = 1; @(posedge clk); // S3->S4 + data = 0; @(posedge clk); // S4->S0 + + // Test staying in S0 (data=0) + data = 0; @(posedge clk); // S0->S0 + data = 0; @(posedge clk); // S0->S0 + + // Test staying in S1 (data=0) + data = 1; @(posedge clk); // S0->S1 + data = 0; @(posedge clk); // S1->S1 + data = 0; @(posedge clk); // S1->S1 + + // Test staying in S2 (data=0) + data = 1; @(posedge clk); // S1->S2 + data = 0; @(posedge clk); // S2->S2 + data = 0; @(posedge clk); // S2->S2 + + // Test staying in S3 (data=0) + data = 1; @(posedge clk); // S2->S3 + data = 0; @(posedge clk); // S3->S3 + data = 0; @(posedge clk); // S3->S3 + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + data <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_flag; + int errortime_flag; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic data; + logic flag_ref; + logic flag_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, data, flag_ref, flag_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst, + .data ); + + RefModule good1 ( + .clk, + .rst, + .data, + .flag(flag_ref) ); + + TopModule top_module1 ( + .clk, + .rst, + .data, + .flag(flag_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_flag) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "flag", stats1.errors_flag, stats1.errortime_flag); + else $display("Hint: Output '%s' has no mismatches.", "flag"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { flag_ref } === ( { flag_ref } ^ { flag_dut } ^ { flag_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (flag_ref !== ( flag_ref ^ flag_dut ^ flag_ref )) + begin if (stats1.errors_flag == 0) stats1.errortime_flag = $time; + stats1.errors_flag = stats1.errors_flag+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob021_asynchronous_FIFO_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob021_asynchronous_FIFO_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..670188bd9082bbb0f95b1d1c69154e1a40946b90 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob021_asynchronous_FIFO_prompt.txt @@ -0,0 +1,211 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input wclk + - input rclk + - input wrstn + - input rrstn + - input winc + - input rinc + - input wdata (WIDTH bits) + - output wfull + - output rempty + - output rdata (WIDTH bits) + +The module should implement an asynchronous FIFO (First-In-First-Out) buffer +with parameterizable width and depth. The FIFO uses a dual-port RAM for +data storage and implements proper read/write pointer management with +clock domain crossing. + +The asynchronous FIFO should work as follows: +- Write interface: Uses wclk domain with winc (write enable) and wdata +- Read interface: Uses rclk domain with rinc (read enable) and rdata +- Reset: wrstn (write domain reset) and rrstn (read domain reset) +- Status signals: wfull (write full) and rempty (read empty) + +FIFO operation: +- Data is written when winc is high and wfull is low +- Data is read when rinc is high and rempty is low +- wfull indicates the FIFO is full and cannot accept more writes +- rempty indicates the FIFO is empty and has no data to read +- The FIFO should handle clock domain crossing properly + + +Dual-port RAM module (provided): +```verilog +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk +,input wenc +,input [$clog2(DEPTH)-1:0] waddr +,input [WIDTH-1:0] wdata +,input rclk +,input renc +,input [$clog2(DEPTH)-1:0] raddr +,output reg [WIDTH-1:0] rdata +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin +if(wenc) +RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin +if(renc) +rdata <= RAM_MEM[raddr]; +end + +endmodule +``` + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input wclk , + input rclk , + input wrstn , + input rrstn , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wire wfull , + output wire rempty , + output wire [WIDTH-1:0] rdata +); + +parameter ADDR_WIDTH = $clog2(DEPTH); + +/**********************addr bin gen*************************/ +reg [ADDR_WIDTH:0] waddr_bin; +reg [ADDR_WIDTH:0] raddr_bin; + +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + waddr_bin <= 'd0; + end + else if(!wfull && winc)begin + waddr_bin <= waddr_bin + 1'd2; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + raddr_bin <= 'd0; + end + else if(!rempty && rinc)begin + raddr_bin <= raddr_bin + 1'd1; + end +end + +/**********************addr gray gen*************************/ +wire [ADDR_WIDTH:0] waddr_gray; +wire [ADDR_WIDTH:0] raddr_gray; +reg [ADDR_WIDTH:0] wptr; +reg [ADDR_WIDTH:0] rptr; +assign waddr_gray = waddr_bin ^ (waddr_bin>>1); +assign raddr_gray = raddr_bin ^ (raddr_bin>>1); +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + wptr <= 'd0; + end + else begin + wptr <= waddr_gray; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + rptr <= 'd0; + end + else begin + rptr <= raddr_gray; + end +end +/**********************syn addr gray*************************/ +reg [ADDR_WIDTH:0] wptr_buff; +reg [ADDR_WIDTH:0] wptr_syn; +reg [ADDR_WIDTH:0] rptr_buff; +reg [ADDR_WIDTH:0] rptr_syn; +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + rptr_buff <= 'd0; + rptr_syn <= 'd0; + end + else begin + rptr_buff <= rptr; + rptr_syn <= rptr_buff; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + wptr_buff <= 'd0; + wptr_syn <= 'd0; + end + else begin + wptr_buff <= wptr; + wptr_syn <= wptr_buff; + end +end +/**********************full empty gen*************************/ +assign wfull = (wptr == {~rptr_syn[ADDR_WIDTH:ADDR_WIDTH-1],rptr_syn[ADDR_WIDTH-2:0]}); +assign rempty = (rptr == wptr_syn); + +/**********************RAM*************************/ +wire wen ; +wire ren ; +wire wren;//high write +wire [ADDR_WIDTH-1:0] waddr; +wire [ADDR_WIDTH-1:0] raddr; +assign wen = winc & !wfull; +assign ren = rinc & !rempty; +assign waddr = waddr_bin[ADDR_WIDTH-1:0]; +assign raddr = raddr_bin[ADDR_WIDTH-1:0]; + +dual_port_RAM #(.DEPTH(DEPTH), + .WIDTH(WIDTH) +)dual_port_RAM( + .wclk (wclk), + .wenc (wen), + .waddr(waddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .wdata(wdata), //Data write + .rclk (rclk), + .renc (ren), + .raddr(raddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .rdata(rdata) //Data output +); + +endmodule + +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk + ,input wenc + ,input [$clog2(DEPTH)-1:0] waddr //Take log2 of depth to get address bit width. + ,input [WIDTH-1:0] wdata //Data write + ,input rclk + ,input renc + ,input [$clog2(DEPTH)-1:0] raddr //Take log2 of depth to get address bit width. + ,output reg [WIDTH-1:0] rdata //Data output +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin + if(wenc) + RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin + if(renc) + rdata <= RAM_MEM[raddr]; +end + +endmodule +``` + diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob021_asynchronous_FIFO_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob021_asynchronous_FIFO_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..d53f12a1f5e860d81138adfafbebe9e082e46f5c --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob021_asynchronous_FIFO_ref.sv @@ -0,0 +1,118 @@ + +module RefModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input wclk , + input rclk , + input wrstn , + input rrstn , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wire wfull , + output wire rempty , + output wire [WIDTH-1:0] rdata +); + +parameter ADDR_WIDTH = $clog2(DEPTH); + +/**********************addr bin gen*************************/ +reg [ADDR_WIDTH:0] waddr_bin; +reg [ADDR_WIDTH:0] raddr_bin; + +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + waddr_bin <= 'd0; + end + else if(!wfull && winc)begin + waddr_bin <= waddr_bin + 1'd1; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + raddr_bin <= 'd0; + end + else if(!rempty && rinc)begin + raddr_bin <= raddr_bin + 1'd1; + end +end + +/**********************addr gray gen*************************/ +wire [ADDR_WIDTH:0] waddr_gray; +wire [ADDR_WIDTH:0] raddr_gray; +reg [ADDR_WIDTH:0] wptr; +reg [ADDR_WIDTH:0] rptr; +assign waddr_gray = waddr_bin ^ (waddr_bin>>1); +assign raddr_gray = raddr_bin ^ (raddr_bin>>1); +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + wptr <= 'd0; + end + else begin + wptr <= waddr_gray; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + rptr <= 'd0; + end + else begin + rptr <= raddr_gray; + end +end +/**********************syn addr gray*************************/ +reg [ADDR_WIDTH:0] wptr_buff; +reg [ADDR_WIDTH:0] wptr_syn; +reg [ADDR_WIDTH:0] rptr_buff; +reg [ADDR_WIDTH:0] rptr_syn; +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + rptr_buff <= 'd0; + rptr_syn <= 'd0; + end + else begin + rptr_buff <= rptr; + rptr_syn <= rptr_buff; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + wptr_buff <= 'd0; + wptr_syn <= 'd0; + end + else begin + wptr_buff <= wptr; + wptr_syn <= wptr_buff; + end +end +/**********************full empty gen*************************/ +assign wfull = (wptr == {~rptr_syn[ADDR_WIDTH:ADDR_WIDTH-1],rptr_syn[ADDR_WIDTH-2:0]}); +assign rempty = (rptr == wptr_syn); + +/**********************RAM*************************/ +wire wen ; +wire ren ; +wire wren;//high write +wire [ADDR_WIDTH-1:0] waddr; +wire [ADDR_WIDTH-1:0] raddr; +assign wen = winc & !wfull; +assign ren = rinc & !rempty; +assign waddr = waddr_bin[ADDR_WIDTH-1:0]; +assign raddr = raddr_bin[ADDR_WIDTH-1:0]; + +dual_port_RAM #(.DEPTH(DEPTH), + .WIDTH(WIDTH) +)dual_port_RAM( + .wclk (wclk), + .wenc (wen), + .waddr(waddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .wdata(wdata), //Data write + .rclk (rclk), + .renc (ren), + .raddr(raddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .rdata(rdata) //Data output +); + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob021_asynchronous_FIFO_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob021_asynchronous_FIFO_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..f1aeed44facc973689edaa5b4d63a9b6a84f574d --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob021_asynchronous_FIFO_test.sv @@ -0,0 +1,252 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic wclk, + output logic rclk, + output logic wrstn, + output logic rrstn, + output logic winc, + output logic rinc, + output logic [7:0] wdata + // === End your code here === +); + // === Start your code here === + reg wclk_reg = 0; + reg rclk_reg = 0; + assign wclk = wclk_reg; + assign rclk = rclk_reg; + + initial forever #10 wclk_reg = ~wclk_reg; // 50MHz write clock + initial forever #15 rclk_reg = ~rclk_reg; // 33.3MHz read clock + + initial begin + wrstn = 0; + rrstn = 0; + winc = 0; + rinc = 0; + wdata = 0; + repeat(20) @(posedge wclk); @(posedge rclk); + wrstn <= 1; + rrstn <= 1; + @(posedge wclk); @(posedge rclk); + + // Test basic write and read operations + winc <= 1; + wdata <= 8'hAA; @(posedge wclk); + wdata <= 8'h55; @(posedge wclk); + wdata <= 8'h33; @(posedge wclk); + wdata <= 8'hCC; @(posedge wclk); + winc <= 0; + @(posedge wclk); + + // Test read operations + rinc <= 1; + repeat(4) @(posedge rclk); + rinc <= 0; + @(posedge rclk); + + // Test simultaneous write and read + winc <= 1; + rinc <= 1; + wdata <= 8'h11; @(posedge wclk); @(posedge rclk); + wdata <= 8'h22; @(posedge wclk); @(posedge rclk); + wdata <= 8'h44; @(posedge wclk); @(posedge rclk); + wdata <= 8'h88; @(posedge wclk); @(posedge rclk); + winc <= 0; + rinc <= 0; + @(posedge wclk); + + // Test FIFO full condition + winc <= 1; + wdata <= 8'hF0; @(posedge wclk); + wdata <= 8'hF1; @(posedge wclk); + wdata <= 8'hF2; @(posedge wclk); + wdata <= 8'hF3; @(posedge wclk); + wdata <= 8'hF4; @(posedge wclk); + wdata <= 8'hF5; @(posedge wclk); + wdata <= 8'hF6; @(posedge wclk); + wdata <= 8'hF7; @(posedge wclk); + wdata <= 8'hF8; @(posedge wclk); + wdata <= 8'hF9; @(posedge wclk); + wdata <= 8'hFA; @(posedge wclk); + wdata <= 8'hFB; @(posedge wclk); + wdata <= 8'hFC; @(posedge wclk); + wdata <= 8'hFD; @(posedge wclk); + wdata <= 8'hFE; @(posedge wclk); + wdata <= 8'hFF; @(posedge wclk); + winc <= 0; + @(posedge wclk); + + // Test FIFO empty condition + rinc <= 1; + repeat(20) @(posedge rclk); // Should trigger rempty + rinc <= 0; + @(posedge rclk); + + // Random testing + repeat(200) @(posedge wclk, posedge rclk) begin + winc <= $random; + rinc <= $random; + wdata <= $random % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_wfull; + int errortime_wfull; + int errors_rempty; + int errortime_rempty; + int errors_rdata; + int errortime_rdata; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic wclk; + logic rclk; + logic wrstn; + logic rrstn; + logic winc; + logic rinc; + logic [7:0] wdata; + logic wfull_ref; + logic rempty_ref; + logic [7:0] rdata_ref; + logic wfull_dut; + logic rempty_dut; + logic [7:0] rdata_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, wclk, rclk, wrstn, rrstn, winc, rinc, wdata, wfull_ref, rempty_ref, rdata_ref, wfull_dut, rempty_dut, rdata_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .wclk, + .rclk, + .wrstn, + .rrstn, + .winc, + .rinc, + .wdata ); + + RefModule good1 ( + .wclk, + .rclk, + .wrstn, + .rrstn, + .winc, + .rinc, + .wdata, + .wfull(wfull_ref), + .rempty(rempty_ref), + .rdata(rdata_ref) ); + + TopModule top_module1 ( + .wclk, + .rclk, + .wrstn, + .rrstn, + .winc, + .rinc, + .wdata, + .wfull(wfull_dut), + .rempty(rempty_dut), + .rdata(rdata_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_wfull) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "wfull", stats1.errors_wfull, stats1.errortime_wfull); + else $display("Hint: Output '%s' has no mismatches.", "wfull"); + if (stats1.errors_rempty) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rempty", stats1.errors_rempty, stats1.errortime_rempty); + else $display("Hint: Output '%s' has no mismatches.", "rempty"); + if (stats1.errors_rdata) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rdata", stats1.errors_rdata, stats1.errortime_rdata); + else $display("Hint: Output '%s' has no mismatches.", "rdata"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { wfull_ref, rempty_ref, rdata_ref } === ( { wfull_ref, rempty_ref, rdata_ref } ^ { wfull_dut, rempty_dut, rdata_dut } ^ { wfull_ref, rempty_ref, rdata_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (wfull_ref !== ( wfull_ref ^ wfull_dut ^ wfull_ref )) + begin if (stats1.errors_wfull == 0) stats1.errortime_wfull = $time; + stats1.errors_wfull = stats1.errors_wfull+1'b1; end + if (rempty_ref !== ( rempty_ref ^ rempty_dut ^ rempty_ref )) + begin if (stats1.errors_rempty == 0) stats1.errortime_rempty = $time; + stats1.errors_rempty = stats1.errors_rempty+1'b1; end + if (rdata_ref !== ( rdata_ref ^ rdata_dut ^ rdata_ref )) + begin if (stats1.errors_rdata == 0) stats1.errortime_rdata = $time; + stats1.errors_rdata = stats1.errors_rdata+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob022_synchronous_FIFO_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob022_synchronous_FIFO_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..ffb754932f4521adc743a5858f274d5d46268a22 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob022_synchronous_FIFO_prompt.txt @@ -0,0 +1,156 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input winc + - input rinc + - input wdata (WIDTH bits) + - output wfull + - output rempty + - output rdata (WIDTH bits) + +The module should implement a synchronous FIFO (First In, First Out) buffer +that uses a dual-port RAM for data storage. The FIFO should be parameterizable +in both WIDTH and DEPTH, with configurable bit WIDTH and DEPTH parameters. + +The synchronous FIFO should work as follows: +- Input interface: + - clk: Single clock signal for both read and write operations + - rst_n: Active low reset signal + - winc: Write enable signal (pulse to write data) + - rinc: Read enable signal (pulse to read data) + - wdata: 8-bit input data to be written +- Output interface: + - wfull: FIFO full flag (high when FIFO is full) + - rempty: FIFO empty flag (high when FIFO is empty) + - rdata: 8-bit output data read from FIFO + +FIFO operation: +- Write operations occur on the positive edge of clk when winc is high +- Read operations occur on the positive edge of clk when rinc is high +- The FIFO uses a dual-port RAM for data storage +- Write and read pointers are managed to track FIFO status +- Full and empty conditions are detected based on pointer comparison +- The FIFO should handle simultaneous read and write operations correctly + +Dual-port RAM module (provided): +``` +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk +,input wenc +,input [$clog2(DEPTH)-1:0] waddr +,input [WIDTH-1:0] wdata +,input rclk +,input renc +,input [$clog2(DEPTH)-1:0] raddr +,output reg [WIDTH-1:0] rdata +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin + if(wenc) + RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin + if(renc) + rdata <= RAM_MEM[raddr]; +end + +endmodule +``` + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input clk , + input rst_n , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wfull , + output rempty , + output wire [WIDTH-1:0] rdata +); + +localparam DP_WD = $clog2(DEPTH); + +reg [DP_WD :0]waddr; +wire wenc; +wire waddr_d_h; +wire [DP_WD -1:0]waddr_d_l; +assign wenc = winc & (!wfull); +assign waddr_d_h = (waddr[DP_WD-1:0] == DEPTH-1) ? ~waddr[DP_WD] : waddr[DP_WD]; +assign waddr_d_l = (waddr[DP_WD-1:0] == DEPTH-1) ? 0 : waddr[DP_WD-1:0] + 1; +always @(posedge clk or negedge rst_n)begin + if(~rst_n) waddr <= 0; + else if(wenc) waddr <= {waddr_d_h, waddr_d_l}; +end + +reg [DP_WD :0]raddr; +wire renc; +wire raddr_d_h; +wire [DP_WD -1:0]raddr_d_l; +assign renc = rinc & (!rempty); +assign raddr_d_h = (raddr[DP_WD-1:0] == DEPTH) ? ~raddr[DP_WD] : raddr[DP_WD]; +assign raddr_d_l = (raddr[DP_WD-1:0] == DEPTH-1) ? 0 : raddr[DP_WD-1:0] + 1; +always @(posedge clk or negedge rst_n)begin + if(~rst_n) raddr <= 0; + else if(renc) raddr <= {raddr_d_h, raddr_d_l}; +end + +wire [DP_WD :0]fifo_cnt = (waddr[DP_WD] == raddr[DP_WD]) ? waddr[DP_WD-1:0] - raddr[DP_WD-1:0]: + (waddr[DP_WD-1:0] + DEPTH - raddr[DP_WD-1:0]); + +assign rempty = (fifo_cnt == 0); +assign wfull = (fifo_cnt == DEPTH); + +dual_port_RAM #(.DEPTH(DEPTH), .WIDTH(WIDTH)) +u_ram ( + .wclk (clk), + .wenc (wenc), + .waddr (waddr[$clog2(DEPTH)-1:0]), + .wdata (wdata), + .rclk (clk), + .renc (renc), + .raddr (raddr[$clog2(DEPTH)-1:0]), + .rdata (rdata) +); + +endmodule + +/**********************************RAM************************************/ +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk + ,input wenc + ,input [$clog2(DEPTH)-1:0] waddr //Take log2 of depth to get address bit width. + ,input [WIDTH-1:0] wdata //Data write + ,input rclk + ,input renc + ,input [$clog2(DEPTH)-1:0] raddr //Take log2 of depth to get address bit width. + ,output reg [WIDTH-1:0] rdata //Data output +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin + if(wenc) + RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin + if(renc) + rdata <= RAM_MEM[raddr]; +end + +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob022_synchronous_FIFO_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob022_synchronous_FIFO_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..b3194008457a02e29fc6d3a7d787b6c008fbb7da --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob022_synchronous_FIFO_ref.sv @@ -0,0 +1,61 @@ + +module RefModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input clk , + input rst_n , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wfull , + output rempty , + output wire [WIDTH-1:0] rdata +); + +localparam DP_WD = $clog2(DEPTH); + +reg [DP_WD :0]waddr; +wire wenc; +wire waddr_d_h; +wire [DP_WD -1:0]waddr_d_l; +assign wenc = winc & (!wfull); +assign waddr_d_h = (waddr[DP_WD-1:0] == DEPTH-1) ? ~waddr[DP_WD] : waddr[DP_WD]; +assign waddr_d_l = (waddr[DP_WD-1:0] == DEPTH-1) ? 0 : waddr[DP_WD-1:0] + 1; +always @(posedge clk or negedge rst_n)begin + if(~rst_n) waddr <= 0; + else if(wenc) waddr <= {waddr_d_h, waddr_d_l}; +end + +reg [DP_WD :0]raddr; +wire renc; +wire raddr_d_h; +wire [DP_WD -1:0]raddr_d_l; +assign renc = rinc & (!rempty); +assign raddr_d_h = (raddr[DP_WD-1:0] == DEPTH-1) ? ~raddr[DP_WD] : raddr[DP_WD]; +assign raddr_d_l = (raddr[DP_WD-1:0] == DEPTH-1) ? 0 : raddr[DP_WD-1:0] + 1; +always @(posedge clk or negedge rst_n)begin + if(~rst_n) raddr <= 0; + else if(renc) raddr <= {raddr_d_h, raddr_d_l}; +end + +wire [DP_WD :0]fifo_cnt = (waddr[DP_WD] == raddr[DP_WD]) ? waddr[DP_WD-1:0] - raddr[DP_WD-1:0]: + (waddr[DP_WD-1:0] + DEPTH - raddr[DP_WD-1:0]); + +assign rempty = (fifo_cnt == 0); +assign wfull = (fifo_cnt == DEPTH); + +dual_port_RAM #(.DEPTH(DEPTH), .WIDTH(WIDTH)) +u_ram ( + .wclk (clk), + .wenc (wenc), + .waddr (waddr[$clog2(DEPTH)-1:0]), + .wdata (wdata), + .rclk (clk), + .renc (renc), + .raddr (raddr[$clog2(DEPTH)-1:0]), + .rdata (rdata) +); + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob022_synchronous_FIFO_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob022_synchronous_FIFO_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..ce3e41170df8cd9e2bb3160877e307951fb99970 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob022_synchronous_FIFO_test.sv @@ -0,0 +1,229 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic winc, + output logic rinc, + output logic [7:0] wdata + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + winc = 0; + rinc = 0; + wdata = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic write operations + winc <= 1; + wdata <= 8'hAA; @(posedge clk); + wdata <= 8'h55; @(posedge clk); + wdata <= 8'h33; @(posedge clk); + wdata <= 8'hCC; @(posedge clk); + winc <= 0; + @(posedge clk); + + // Test basic read operations + rinc <= 1; + repeat(4) @(posedge clk); + rinc <= 0; + @(posedge clk); + + // Test simultaneous write and read + winc <= 1; + rinc <= 1; + wdata <= 8'h11; @(posedge clk); + wdata <= 8'h22; @(posedge clk); + wdata <= 8'h44; @(posedge clk); + wdata <= 8'h88; @(posedge clk); + winc <= 0; + rinc <= 0; + @(posedge clk); + + // Test FIFO full condition + winc <= 1; + wdata <= 8'hF0; @(posedge clk); + wdata <= 8'hF1; @(posedge clk); + wdata <= 8'hF2; @(posedge clk); + wdata <= 8'hF3; @(posedge clk); + wdata <= 8'hF4; @(posedge clk); + wdata <= 8'hF5; @(posedge clk); + wdata <= 8'hF6; @(posedge clk); + wdata <= 8'hF7; @(posedge clk); + wdata <= 8'hF8; @(posedge clk); + wdata <= 8'hF9; @(posedge clk); + wdata <= 8'hFA; @(posedge clk); + wdata <= 8'hFB; @(posedge clk); + wdata <= 8'hFC; @(posedge clk); + wdata <= 8'hFD; @(posedge clk); + wdata <= 8'hFE; @(posedge clk); + wdata <= 8'hFF; @(posedge clk); // Should trigger wfull + winc <= 0; + @(posedge clk); + + // Test FIFO empty condition + rinc <= 1; + repeat(20) @(posedge clk); // Should trigger rempty + rinc <= 0; + @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + winc <= $random; + rinc <= $random; + wdata <= $random % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_wfull; + int errortime_wfull; + int errors_rempty; + int errortime_rempty; + int errors_rdata; + int errortime_rdata; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic winc; + logic rinc; + logic [7:0] wdata; + logic wfull_ref; + logic rempty_ref; + logic [7:0] rdata_ref; + logic wfull_dut; + logic rempty_dut; + logic [7:0] rdata_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, winc, rinc, wdata, wfull_ref, rempty_ref, rdata_ref, wfull_dut, rempty_dut, rdata_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .winc, + .rinc, + .wdata ); + + RefModule good1 ( + .clk, + .rst_n, + .winc, + .rinc, + .wdata, + .wfull(wfull_ref), + .rempty(rempty_ref), + .rdata(rdata_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .winc, + .rinc, + .wdata, + .wfull(wfull_dut), + .rempty(rempty_dut), + .rdata(rdata_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_wfull) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "wfull", stats1.errors_wfull, stats1.errortime_wfull); + else $display("Hint: Output '%s' has no mismatches.", "wfull"); + if (stats1.errors_rempty) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rempty", stats1.errors_rempty, stats1.errortime_rempty); + else $display("Hint: Output '%s' has no mismatches.", "rempty"); + if (stats1.errors_rdata) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rdata", stats1.errors_rdata, stats1.errortime_rdata); + else $display("Hint: Output '%s' has no mismatches.", "rdata"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { wfull_ref, rempty_ref, rdata_ref } === ( { wfull_ref, rempty_ref, rdata_ref } ^ { wfull_dut, rempty_dut, rdata_dut } ^ { wfull_ref, rempty_ref, rdata_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (wfull_ref !== ( wfull_ref ^ wfull_dut ^ wfull_ref )) + begin if (stats1.errors_wfull == 0) stats1.errortime_wfull = $time; + stats1.errors_wfull = stats1.errors_wfull+1'b1; end + if (rempty_ref !== ( rempty_ref ^ rempty_dut ^ rempty_ref )) + begin if (stats1.errors_rempty == 0) stats1.errortime_rempty = $time; + stats1.errors_rempty = stats1.errors_rempty+1'b1; end + if (rdata_ref !== ( rdata_ref ^ rdata_dut ^ rdata_ref )) + begin if (stats1.errors_rdata == 0) stats1.errortime_rdata = $time; + stats1.errors_rdata = stats1.errors_rdata+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob023_gray_code_counter_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob023_gray_code_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..e2fb056e0c4f7754b9ef971c601d9a086811a5c8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob023_gray_code_counter_prompt.txt @@ -0,0 +1,74 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output reg [3:0] gray_out + +The module should implement a 4-bit Gray code counter. Assume all sequential +logic is triggered on the positive edge of the clock. The reset input is active +low asynchronous, and should reset the counter to 0. + +The Gray code counter should work as follows: +- The counter should generate a 4-bit Gray code sequence +- The counter increments every two clock cycles +- The implementation should follow this process: + 1. Input a Gray code value (use an internal wire variable initialized to 0) + 2. Convert the Gray code to binary representation + 3. Increment the binary value by 1 + 4. Convert the incremented binary back to Gray code + 5. Output the resulting Gray code +- Gray code is a binary numeral system where two successive values differ + in only one bit position +- The counter should cycle through all 16 possible 4-bit Gray code values + and then repeat +- When rst_n is low, the counter should reset to 0000 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + + output reg [3:0] gray_out +); +//Gray code to binary conversion +reg [3:0] bin_out; +wire [3:0] gray_wire; + +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_out <= 4'b0; + end + else begin + bin_out[3] = gray_wire[3]; + bin_out[2] = gray_wire[2]^bin_out[3]; + bin_out[1] = gray_wire[1]^bin_out[2]; + bin_out[0] = gray_wire[0]^bin_out[3]; + end +end +//Binary increment +reg [3:0] bin_add_wire; +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_add_wire <= 4'b0; + end + else begin + bin_add_wire <= bin_out + 1'b1; + end +end +//Binary to gray code conversion +assign gray_wire = (bin_add_wire >> 1) ^ bin_add_wire; + +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + gray_out <= 4'b0; + end + else begin + gray_out <= gray_wire; + end +end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob023_gray_code_counter_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob023_gray_code_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..894e7d68646d0b1562e7cdf67d0a96597dac0e4f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob023_gray_code_counter_ref.sv @@ -0,0 +1,44 @@ + +module RefModule( + input clk, + input rst_n, + + output reg [3:0] gray_out +); +//Gray code to binary conversion +reg [3:0] bin_out; +wire [3:0] gray_wire; + +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_out <= 4'b0; + end + else begin + bin_out[3] = gray_wire[3]; + bin_out[2] = gray_wire[2]^bin_out[3]; + bin_out[1] = gray_wire[1]^bin_out[2]; + bin_out[0] = gray_wire[0]^bin_out[1]; + end +end +//Binary increment +reg [3:0] bin_add_wire; +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_add_wire <= 4'b0; + end + else begin + bin_add_wire <= bin_out + 1'b1; + end +end +//Binary to gray code conversion +assign gray_wire = (bin_add_wire >> 1) ^ bin_add_wire; + +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + gray_out <= 4'b0; + end + else begin + gray_out <= gray_wire; + end +end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob023_gray_code_counter_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob023_gray_code_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..71832152abeadfdf1876bddadd268eb2ce375283 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob023_gray_code_counter_test.sv @@ -0,0 +1,132 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test Gray code counter for multiple cycles + repeat(200) @(posedge clk); + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_gray_out; + int errortime_gray_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [3:0] gray_out_ref; + logic [3:0] gray_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, gray_out_ref, gray_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .gray_out(gray_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .gray_out(gray_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_gray_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "gray_out", stats1.errors_gray_out, stats1.errortime_gray_out); + else $display("Hint: Output '%s' has no mismatches.", "gray_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { gray_out_ref } === ( { gray_out_ref } ^ { gray_out_dut } ^ { gray_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (gray_out_ref !== ( gray_out_ref ^ gray_out_dut ^ gray_out_ref )) + begin if (stats1.errors_gray_out == 0) stats1.errortime_gray_out = $time; + stats1.errors_gray_out = stats1.errors_gray_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob024_multiplication_and_bitwise_operations_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob024_multiplication_and_bitwise_operations_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..13586afbbe8b4038789691843785c60a8df4d92a --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob024_multiplication_and_bitwise_operations_prompt.txt @@ -0,0 +1,26 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input A (8 bits) + - output B (16 bits) + +Design a computational unit circuit that multiplies A[7:0] by the constant +0xFB (binary 11111011). You should implement this using as few resources +as possible. + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input [7:0] A, + output [15:0] B + ); + +//*************code***********// + + assign B = (A<<8)-(A<<2)+A; +//*************code***********// + +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob024_multiplication_and_bitwise_operations_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob024_multiplication_and_bitwise_operations_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..e4f0d68117fad73983a4ce341fc8f75e4dbc3c54 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob024_multiplication_and_bitwise_operations_ref.sv @@ -0,0 +1,12 @@ + +module RefModule( + input [7:0] A, + output [15:0] B + ); + +//*************code***********// + + assign B = (A<<8)-(A<<2)-A; +//*************code***********// + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob024_multiplication_and_bitwise_operations_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob024_multiplication_and_bitwise_operations_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..ed0a3fd059cc6bcba35d77a1f961ec9015cbb5ae --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob024_multiplication_and_bitwise_operations_test.sv @@ -0,0 +1,160 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic [7:0] A + // === End your code here === +); + // === Start your code here === + initial begin + A <= 0; + + // Test basic cases + @(posedge clk); + A <= 8'd0; + @(posedge clk); + + + A <= 8'd200; + @(posedge clk); + + A <= 8'd250; + @(posedge clk); + + A <= 8'd255; + @(posedge clk); + + // Test edge cases + A <= 8'd128; + @(posedge clk); + + A <= 8'd64; + @(posedge clk); + + A <= 8'd32; + @(posedge clk); + + A <= 8'd16; + @(posedge clk); + + // Random test sequence + repeat(200) @(posedge clk) begin + A <= $random & 255; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_B; + int errortime_B; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic [7:0] A; + logic [15:0] B_ref; + logic [15:0] B_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, A, B_ref, B_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .A + ); + + RefModule good1 ( + .A, + .B(B_ref) + ); + + TopModule top_module1 ( + .A, + .B(B_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_B) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "B", stats1.errors_B, stats1.errortime_B); + else $display("Hint: Output '%s' has no mismatches.", "B"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { B_ref } === ( { B_ref } ^ { B_dut } ^ { B_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (B_ref !== ( B_ref ^ B_dut ^ B_ref )) + begin if (stats1.errors_B == 0) stats1.errortime_B = $time; + stats1.errors_B = stats1.errors_B+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob026_simple_stopwatch_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob026_simple_stopwatch_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..451f6b5e76849bc0cdacb86020a281f4a3fc843f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob026_simple_stopwatch_prompt.txt @@ -0,0 +1,61 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output reg [5:0] second + - output reg [5:0] minute + +The module should implement a simple stopwatch functionality. Assume all +sequential logic is triggered on the positive edge of the clock. The reset +input is active low asynchronous, and should reset both counters to 0. + +The simple stopwatch should work as follows: +- The second counter counts from 1 to 60 in a cycle +- When the second counter reaches 60, it resets to 1 and the minute counter + increments by 1 +- The minute counter counts from 0 to 60 +- When the minute counter reaches 60, the stopwatch pauses counting +- Both counters should reset to 0 when rst_n is low + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + + output reg [5:0]second, + output reg [5:0]minute + ); + + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + minute <= 6'd0; + end + else if (second == 6'd60) + begin + minute <= minute+1; + end + else + begin + minute <= minute; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + second <= 6'd0; + end + else if(second == 6'd59) + begin + second <= 6'd1; + end + else if (minute == 59) + second <= second; + else + second <= second+1'd1; +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob026_simple_stopwatch_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob026_simple_stopwatch_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..bd35c5aaeb9b14ce97cf12d1d4138ffc5c7beb7f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob026_simple_stopwatch_ref.sv @@ -0,0 +1,38 @@ + +module RefModule( + input clk, + input rst_n, + + output reg [5:0]second, + output reg [5:0]minute + ); + + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + minute <= 6'd0; + end + else if (second == 6'd60) + begin + minute <= minute+1; + end + else + begin + minute <= minute; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + second <= 6'd0; + end + else if(second == 6'd60) + begin + second <= 6'd1; + end + else if (minute == 60) + second <= second; + else + second <= second+1'd1; +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob026_simple_stopwatch_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob026_simple_stopwatch_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..2e9249e83fe18d332a6bc787b4165e264d25c592 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob026_simple_stopwatch_test.sv @@ -0,0 +1,146 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test stopwatch for multiple cycles + // The stopwatch counts seconds from 1-60, then increments minutes + // When minute reaches 60, the stopwatch pauses + repeat(5000) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_second; + int errortime_second; + int errors_minute; + int errortime_minute; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [5:0] second_ref; + logic [5:0] minute_ref; + logic [5:0] second_dut; + logic [5:0] minute_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, second_ref, minute_ref, second_dut, minute_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .second(second_ref), + .minute(minute_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .second(second_dut), + .minute(minute_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_second) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "second", stats1.errors_second, stats1.errortime_second); + else $display("Hint: Output '%s' has no mismatches.", "second"); + if (stats1.errors_minute) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "minute", stats1.errors_minute, stats1.errortime_minute); + else $display("Hint: Output '%s' has no mismatches.", "minute"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { second_ref, minute_ref } === ( { second_ref, minute_ref } ^ { second_dut, minute_dut } ^ { second_ref, minute_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (second_ref !== ( second_ref ^ second_dut ^ second_ref )) + begin if (stats1.errors_second == 0) stats1.errortime_second = $time; + stats1.errors_second = stats1.errors_second+1'b1; end + if (minute_ref !== ( minute_ref ^ minute_dut ^ minute_ref )) + begin if (stats1.errors_minute == 0) stats1.errortime_minute = $time; + stats1.errors_minute = stats1.errors_minute+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob027_settable_counter_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob027_settable_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..94d1ae8c1f773161b63bffc7c9a0ed66c958b6a8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob027_settable_counter_prompt.txt @@ -0,0 +1,84 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input set + - input [3:0] set_num + - output reg zero + - output reg [3:0] number + +The module should implement a hexadecimal settable counter. Assume all +sequential logic is triggered on the positive edge of the clock. The reset +input is active low asynchronous, and should reset the counter to 0. + +The settable counter should work as follows: +- The counter increments on each clock cycle and cycles through hexadecimal + values (0 to F, then back to 0) +- When the counter reaches 0, the zero signal is asserted (goes high) +- When the set signal is active, the counter output is forced to the value + of set_num instead of incrementing +- The zero signal is high only when the number equals 0, and low at all + other times +- When rst_n is low, the counter should reset to 0 +- the output zero and number will be update on the next rising edge of the clock; number shows set_num two clock cycles after set is high. + +Counter operation: +- Normal counting: Counter increments each clock cycle (0, 1, 2, ..., F, 0, 1, ...). +- Set operation: When set is active, number is set to set_num value +- Zero detection: The zero signal is high only when number equals 0 +- Reset behavior: Active low reset to 0 + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input set, + input [3:0] set_num, + output reg [3:0]number, + output reg zero + ); + reg [3:0]num; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + zero <= 1'd0; + end + else if (num == 4'd1) + begin + zero <= 1'b1; + end + else + begin + zero <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + num <= 4'b0; + end + else if(set) + begin + num <= set_num; + end + else + begin + num <= num + 1'd1; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + number <= 1'd0; + end + else + begin + number <= num; + end + +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob027_settable_counter_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob027_settable_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..f9cf9eb7c182e1264308d1d243de4012ff9a028c --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob027_settable_counter_ref.sv @@ -0,0 +1,50 @@ + +module RefModule( + input clk, + input rst_n, + input set, + input [3:0] set_num, + output reg [3:0]number, + output reg zero + ); + reg [3:0]num; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + zero <= 1'd0; + end + else if (num == 4'd0) + begin + zero <= 1'b1; + end + else + begin + zero <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + num <= 4'b0; + end + else if(set) + begin + num <= set_num; + end + else + begin + num <= num + 1'd1; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + number <= 1'd0; + end + else + begin + number <= num; + end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob027_settable_counter_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob027_settable_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..c6249e9b80c00ef43e9b590baab344971408bed8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob027_settable_counter_test.sv @@ -0,0 +1,175 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic set, + output logic [3:0] set_num + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + set = 0; + set_num = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test normal counting + set = 0; + repeat(20) @(posedge clk); // Count: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,0,1,2,3 + + // Test set operation + set = 1; + set_num = 4'hA; @(posedge clk); // Set to A + set_num = 4'h5; @(posedge clk); // Set to 5 + set_num = 4'hF; @(posedge clk); // Set to F + set = 0; + @(posedge clk); + // Test set to 0 (should trigger zero signal) + set = 1; + set_num = 4'h0; @(posedge clk); // Set to 0 + set = 0; + @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + set <= $random; + set_num <= $random % 16; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_number; + int errortime_number; + int errors_zero; + int errortime_zero; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic set; + logic [3:0] set_num; + logic [3:0] number_ref; + logic zero_ref; + logic [3:0] number_dut; + logic zero_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, set, set_num, number_ref, zero_ref, number_dut, zero_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .set, + .set_num ); + + RefModule good1 ( + .clk, + .rst_n, + .set, + .set_num, + .number(number_ref), + .zero(zero_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .set, + .set_num, + .number(number_dut), + .zero(zero_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_number) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "number", stats1.errors_number, stats1.errortime_number); + else $display("Hint: Output '%s' has no mismatches.", "number"); + if (stats1.errors_zero) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "zero", stats1.errors_zero, stats1.errortime_zero); + else $display("Hint: Output '%s' has no mismatches.", "zero"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { number_ref, zero_ref } === ( { number_ref, zero_ref } ^ { number_dut, zero_dut } ^ { number_ref, zero_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (number_ref !== ( number_ref ^ number_dut ^ number_ref )) + begin if (stats1.errors_number == 0) stats1.errortime_number = $time; + stats1.errors_number = stats1.errors_number+1'b1; end + if (zero_ref !== ( zero_ref ^ zero_dut ^ zero_ref )) + begin if (stats1.errors_zero == 0) stats1.errortime_zero = $time; + stats1.errors_zero = stats1.errors_zero+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob028_up_and_down_counter_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob028_up_and_down_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..25210333ee2801ccffa0b1068cd26634d77da390 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob028_up_and_down_counter_prompt.txt @@ -0,0 +1,75 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input mode + - output reg [3:0] number + - output reg zero + +The module should implement a decimal up/down counter. Assume all sequential +logic is triggered on the positive edge of the clock. The reset input is active +low asynchronous, and should reset the counter to 0, and zero is also be 0. + +The up/down counter should work as follows: +- When mode signal is 1, the counter increments (counts up) each clock cycle +- When mode signal is 0, the counter decrements (counts down) each clock cycle +- The counter output (number) is a 4-bit value representing the current count +- The zero signal is asserted (goes high) for the next cycle when the number reaches 0, and is + low at all other times +- When rst_n is low, the counter should reset to 0 + +Counter operation: +- Mode = 1: Counter increments each clock cycle (0, 1, 2, 3, ..., 9, 0, 1, ...), only up to 9 +- Mode = 0: Counter decrements each clock cycle (9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, ...) +- Zero detection: The zero signal is high for next cycle only when number equals 0 +- Reset behavior: Active low reset to 0 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input mode, + output reg [3:0]number, + output reg zero + ); + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + zero <= 1'd0; + end + else if (number == 4'd0) + begin + zero <= 1'b1; + end + else + begin + zero <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + number <= 4'b0; + end + else if(mode) + begin + if(number == 10) + number <= 0; + else + number <= number + 1'd1; + end + else if(!mode) + begin + if(number == 0) + number <= 9; + else + number <= number - 1'd1; + end + else number <= number; +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob028_up_and_down_counter_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob028_up_and_down_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..3f4a2006e216d481bf02975a552c29d203eb8309 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob028_up_and_down_counter_ref.sv @@ -0,0 +1,44 @@ + +module RefModule( + input clk, + input rst_n, + input mode, + output reg [3:0]number, + output reg zero + ); + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + zero <= 1'd0; + end + else if (number == 4'd0) + begin + zero <= 1'b1; + end + else + begin + zero <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + number <= 4'b0; + end + else if(mode) + begin + if(number == 9) + number <= 0; + else + number <= number + 1'd1; + end + else if(!mode) + begin + if(number == 0) + number <= 9; + else + number <= number - 1'd1; + end + else number <= number; +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob028_up_and_down_counter_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob028_up_and_down_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..9c8d87a866b931fe81a5cb15a4f3ba531c1391a7 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob028_up_and_down_counter_test.sv @@ -0,0 +1,165 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic mode + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + mode = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test up counting (mode = 1) + mode <= 1; + repeat(15) @(posedge clk); // Count up: 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4 + + // Test down counting (mode = 0) + mode <= 0; + repeat(15) @(posedge clk); // Count down: 5,4,3,2,1,0,9,8,7,6,5,4,3,2,1 + + // Test mode switching + mode <= 1; @(posedge clk); // Count up + mode <= 0; @(posedge clk); // Count down + mode <= 1; @(posedge clk); // Count up + mode <= 0; @(posedge clk); // Count down + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + mode <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_number; + int errortime_number; + int errors_zero; + int errortime_zero; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic mode; + logic [3:0] number_ref; + logic zero_ref; + logic [3:0] number_dut; + logic zero_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, mode, number_ref, zero_ref, number_dut, zero_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .mode ); + + RefModule good1 ( + .clk, + .rst_n, + .mode, + .number(number_ref), + .zero(zero_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .mode, + .number(number_dut), + .zero(zero_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_number) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "number", stats1.errors_number, stats1.errortime_number); + else $display("Hint: Output '%s' has no mismatches.", "number"); + if (stats1.errors_zero) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "zero", stats1.errors_zero, stats1.errortime_zero); + else $display("Hint: Output '%s' has no mismatches.", "zero"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { number_ref, zero_ref } === ( { number_ref, zero_ref } ^ { number_dut, zero_dut } ^ { number_ref, zero_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (number_ref !== ( number_ref ^ number_dut ^ number_ref )) + begin if (stats1.errors_number == 0) stats1.errortime_number = $time; + stats1.errors_number = stats1.errors_number+1'b1; end + if (zero_ref !== ( zero_ref ^ zero_dut ^ zero_ref )) + begin if (stats1.errors_zero == 0) stats1.errortime_zero = $time; + stats1.errors_zero = stats1.errors_zero+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob030_simple_implementation_RAM_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob030_simple_implementation_RAM_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..2cf952c92521cce2b5364e5920ca6e1b387174b9 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob030_simple_implementation_RAM_prompt.txt @@ -0,0 +1,60 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input read_en + - input write_en + - input read_addr (8 bits) + - input write_addr (8 bits) + - input write_data (4 bits) + - output read_data (4 bits) + +Implement a dual-port RAM with depth of 8 and data width of 4 bits. All +data should be initialized to 0000. The RAM has two separate ports: one +for reading data and one for writing data. Read and write operations can +be performed simultaneously. + +When the read enable signal read_en is asserted, the module should read +data from the location specified by read_addr and output it on read_data. +When the write enable signal write_en is asserted, the module should write +write_data to the location specified by write_addr. + +The rst_n signal is an active-low asynchronous reset signal. Assume all +sequential logic is triggered on the positive edge of the clock. + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + + input write_en, + input [7:0]write_addr, + input [3:0]write_data, + + input read_en, + input [7:0]read_addr, + output reg [3:0]read_data +); + reg [3:0] myRAM [7:0]; + + reg [8:0] i; + // integer i; // Not synthesizable + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + for(i=128;i<256;i=i+1) + myRAM[i] <= 0; + else + myRAM[write_addr] <= write_en? write_data: myRAM[write_addr]; + end + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + read_data <= 0; + else + read_data <= read_en? myRAM[read_addr]: read_data; + end +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob030_simple_implementation_RAM_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob030_simple_implementation_RAM_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..c979fc6d858b9b6871182cfcc04ae6454d16a591 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob030_simple_implementation_RAM_ref.sv @@ -0,0 +1,31 @@ + +module RefModule( + input clk, + input rst_n, + + input write_en, + input [7:0]write_addr, + input [3:0]write_data, + + input read_en, + input [7:0]read_addr, + output reg [3:0]read_data +); + reg [3:0] myRAM [7:0]; + + reg [8:0] i; + // integer i; // Not synthesizable + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + for(i=0;i<256;i=i+1) + myRAM[i] <= 0; + else + myRAM[write_addr] <= write_en? write_data: myRAM[write_addr]; + end + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + read_data <= 0; + else + read_data <= read_en? myRAM[read_addr]: read_data; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob030_simple_implementation_RAM_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob030_simple_implementation_RAM_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..4a7724435caa96d00f660504d0abcb1bd06f2c5e --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob030_simple_implementation_RAM_test.sv @@ -0,0 +1,250 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic read_en, + output logic write_en, + output logic [7:0] read_addr, + output logic [7:0] write_addr, + output logic [3:0] write_data + // === End your code here === +); + // === Start your code here === + initial begin + rst_n <= 0; + read_en <= 0; + write_en <= 0; + read_addr <= 0; + write_addr <= 0; + write_data <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic write operations + write_en <= 1; + write_addr <= 0; + write_data <= 4'b1010; + @(posedge clk); + + write_addr <= 1; + write_data <= 4'b0101; + @(posedge clk); + + write_addr <= 2; + write_data <= 4'b1111; + @(posedge clk); + + write_en <= 0; + @(posedge clk); + + // Test basic read operations + read_en <= 1; + read_addr <= 0; + @(posedge clk); // 1010 + + read_addr <= 1; + @(posedge clk); // 0101 + + read_addr <= 2; + @(posedge clk); // 1111 + + read_en <= 0; + @(posedge clk); + + // Test simultaneous read and write + read_en <= 1; + write_en <= 1; + read_addr <= 0; + write_addr <= 3; + write_data <= 4'b1100; + @(posedge clk); + + read_addr <= 1; + write_addr <= 4; + write_data <= 4'b0011; + @(posedge clk); + + read_en <= 0; + write_en <= 0; + @(posedge clk); + + // Random test sequence + repeat(200) @(posedge clk) begin + read_en <= $random & 1; + write_en <= $random & 1; + read_addr <= $random & 7; // Only use addresses 0-7 for depth 8 + write_addr <= $random & 7; + write_data <= $random & 15; // 4-bit data + end + + // === Additional checks: asynchronous active-low reset in mid-operation === + // Spec: "All data should be initialized to 0000"; "The rst_n signal is an active-low asynchronous reset + // signal." Earlier stimulus only resets at start-up. Fill every location with non-zero data, reset + // between edges, then read every location back (addresses 0..7 only). + @(posedge clk); + read_en <= 1; write_en <= 1; + for (int a = 0; a < 8; a++) begin + write_addr <= a; write_data <= 4'(a + 8); read_addr <= 7 - a; + @(posedge clk); + end + write_en <= 0; read_addr <= 5; + @(posedge clk); // read_data != 0 here + #2 rst_n <= 0; + repeat(2) @(posedge clk); + #2 rst_n <= 1; + @(posedge clk); + for (int a = 0; a < 8; a++) begin + read_addr <= a; + @(posedge clk); + end + repeat(100) @(posedge clk) begin + read_en <= $random & 1; + write_en <= $random & 1; + read_addr <= $random & 7; + write_addr <= $random & 7; + write_data <= $random & 15; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_read_data; + int errortime_read_data; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic read_en; + logic write_en; + logic [7:0] read_addr; + logic [7:0] write_addr; + logic [3:0] write_data; + logic [3:0] read_data_ref; + logic [3:0] read_data_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, read_en, write_en, read_addr, write_addr, write_data, read_data_ref, read_data_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .read_en, + .write_en, + .read_addr, + .write_addr, + .write_data + ); + + RefModule good1 ( + .clk, + .rst_n, + .write_en, + .write_addr, + .write_data, + .read_en, + .read_addr, + .read_data(read_data_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .read_en, + .write_en, + .read_addr, + .write_addr, + .write_data, + .read_data(read_data_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_read_data) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "read_data", stats1.errors_read_data, stats1.errortime_read_data); + else $display("Hint: Output '%s' has no mismatches.", "read_data"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { read_data_ref } === ( { read_data_ref } ^ { read_data_dut } ^ { read_data_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (read_data_ref !== ( read_data_ref ^ read_data_dut ^ read_data_ref )) + begin if (stats1.errors_read_data == 0) stats1.errortime_read_data = $time; + stats1.errors_read_data = stats1.errors_read_data+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob031_johnson_counter_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob031_johnson_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..a51a48e78733e9dc15ada9c873b1aa9fe4974d7a --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob031_johnson_counter_prompt.txt @@ -0,0 +1,35 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output reg [3:0] Q + +The module should implement a 4-bit Johnson counter (also known as a twisted +ring counter). Assume all sequential logic is triggered on the positive edge +of the clock. The reset input is active low asynchronous, and should reset +the counter to 0000. + +The Johnson counter should work as follows: +- The counter follows a specific sequence of states in a circular pattern +- The state sequence is: 0000 -> 1000 -> 1100 -> 1110 -> 1111 -> 0111 -> 0011 -> 0001 -> 0000 +- This creates a cycle of 8 states before repeating +- On each positive clock edge, the counter advances to the next state in the sequence +- When rst_n is low, the counter should reset to 0000 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n, + + output reg [3:0] Q +); + always@(posedge clk or negedge rst_n)begin + if(!rst_n) Q <= 'd0; + else Q <= {~Q[1], Q[3 : 1]}; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob031_johnson_counter_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob031_johnson_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..1f68a1ac386fe58d6d32038dd39d05c6c255c978 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob031_johnson_counter_ref.sv @@ -0,0 +1,12 @@ + +module RefModule( + input clk , + input rst_n, + + output reg [3:0] Q +); + always@(posedge clk or negedge rst_n)begin + if(!rst_n) Q <= 'd0; + else Q <= {~Q[0], Q[3 : 1]}; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob031_johnson_counter_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob031_johnson_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..dc33439851b599a6017ec24dc89ff6eab20774ae --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob031_johnson_counter_test.sv @@ -0,0 +1,133 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test Johnson counter for multiple cycles + // The counter cycles through 8 states: 0000->1000->1100->1110->1111->0111->0011->0001->0000 + repeat(200) @(posedge clk); + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_Q; + int errortime_Q; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [3:0] Q_ref; + logic [3:0] Q_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, Q_ref, Q_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .Q(Q_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .Q(Q_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_Q) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "Q", stats1.errors_Q, stats1.errortime_Q); + else $display("Hint: Output '%s' has no mismatches.", "Q"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { Q_ref } === ( { Q_ref } ^ { Q_dut } ^ { Q_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (Q_ref !== ( Q_ref ^ Q_dut ^ Q_ref )) + begin if (stats1.errors_Q == 0) stats1.errortime_Q = $time; + stats1.errors_Q = stats1.errors_Q+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob032_pipeline_multiplier_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob032_pipeline_multiplier_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..dc84c101d60087a028e25f5047cf4421cc600990 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob032_pipeline_multiplier_prompt.txt @@ -0,0 +1,68 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input [3:0] mul_a + - input [3:0] mul_b + - output reg [7:0] mul_out + +The module should implement a 4-bit unsigned pipelined multiplier. Assume all +sequential logic is triggered on the positive edge of the clock. The reset +input is active low asynchronous, and should reset the multiplier to 0. + +The pipelined multiplier should work as follows: +- The multiplier performs unsigned multiplication of two 4-bit inputs (mul_a and mul_b) +- The result is an 8-bit output (mul_out) representing the product +- The multiplication is implemented using a pipelined architecture for improved + performance and throughput +- The pipeline allows new inputs to be processed every clock cycle while + previous multiplications are still being completed +- When rst_n is low, the multiplier should reset to 0 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule#( + parameter size = 4 +)( + input clk , + input rst_n , + input [size-1:0] mul_a , + input [size-1:0] mul_b , + + output reg [size*2-1:0] mul_out +); + //parameter + parameter N = size * 2; + //defination + wire [N - 1 : 0] temp [0 : 3]; + + reg [N - 1 : 0] adder_0; + reg [N - 1 : 0] adder_1; + + //output + genvar i; + generate + for(i = 0; i < 4; i = i + 1)begin : loop + assign temp[i] = mul_b[i] ? mul_a << i : 'd0; + end + endgenerate + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_0 <= 'd0; + else adder_0 <= temp[0] + temp[1]; + end + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_1 <= 'd0; + else adder_1 <= temp[2] + temp[2]; + end + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) mul_out <= 'd0; + else mul_out <= adder_0 + adder_1; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob032_pipeline_multiplier_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob032_pipeline_multiplier_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..4686788d7af68d2fe694cf911d4d8452976defa0 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob032_pipeline_multiplier_ref.sv @@ -0,0 +1,42 @@ + +module RefModule#( + parameter size = 4 +)( + input clk , + input rst_n , + input [size-1:0] mul_a , + input [size-1:0] mul_b , + + output reg [size*2-1:0] mul_out +); + //parameter + parameter N = size * 2; + //defination + wire [N - 1 : 0] temp [0 : 3]; + + reg [N - 1 : 0] adder_0; + reg [N - 1 : 0] adder_1; + + //output + genvar i; + generate + for(i = 0; i < 4; i = i + 1)begin : loop + assign temp[i] = mul_b[i] ? mul_a << i : 'd0; + end + endgenerate + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_0 <= 'd0; + else adder_0 <= temp[0] + temp[1]; + end + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_1 <= 'd0; + else adder_1 <= temp[2] + temp[3]; + end + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) mul_out <= 'd0; + else mul_out <= adder_0 + adder_1; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob032_pipeline_multiplier_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob032_pipeline_multiplier_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..1dce2b7fad20e13c6d4e6b7019b17061acb1401f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob032_pipeline_multiplier_test.sv @@ -0,0 +1,156 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [3:0] mul_a, + output logic [3:0] mul_b + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + mul_a = 0; + mul_b = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic multiplication cases + mul_a <= 4'd2; mul_b <= 4'd3; @(posedge clk); // 2 * 3 = 6 + mul_a <= 4'd4; mul_b <= 4'd5; @(posedge clk); // 4 * 5 = 20 + mul_a <= 4'd7; mul_b <= 4'd8; @(posedge clk); // 7 * 8 = 56 + mul_a <= 4'd9; mul_b <= 4'd10; @(posedge clk); // 9 * 10 = 90 + mul_a <= 4'd12; mul_b <= 4'd13; @(posedge clk); // 12 * 13 = 156 + mul_a <= 4'd15; mul_b <= 4'd15; @(posedge clk); // 15 * 15 = 225 + + // Test edge cases + mul_a <= 4'd0; mul_b <= 4'd5; @(posedge clk); // 0 * 5 = 0 + mul_a <= 4'd3; mul_b <= 4'd0; @(posedge clk); // 3 * 0 = 0 + mul_a <= 4'd1; mul_b <= 4'd1; @(posedge clk); // 1 * 1 = 1 + mul_a <= 4'd15; mul_b <= 4'd1; @(posedge clk); // 15 * 1 = 15 + mul_a <= 4'd1; mul_b <= 4'd15; @(posedge clk); // 1 * 15 = 15 + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_mul_out; + int errortime_mul_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [3:0] mul_a; + logic [3:0] mul_b; + logic [7:0] mul_out_ref; + logic [7:0] mul_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, mul_a, mul_b, mul_out_ref, mul_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .mul_a, + .mul_b ); + + RefModule good1 ( + .clk, + .rst_n, + .mul_a, + .mul_b, + .mul_out(mul_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .mul_a, + .mul_b, + .mul_out(mul_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_mul_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "mul_out", stats1.errors_mul_out, stats1.errortime_mul_out); + else $display("Hint: Output '%s' has no mismatches.", "mul_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { mul_out_ref } === ( { mul_out_ref } ^ { mul_out_dut } ^ { mul_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (mul_out_ref !== ( mul_out_ref ^ mul_out_dut ^ mul_out_ref )) + begin if (stats1.errors_mul_out == 0) stats1.errortime_mul_out = $time; + stats1.errors_mul_out = stats1.errors_mul_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob033_traffic_lights_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob033_traffic_lights_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..9bea3399d36003851ed4919980744982f374adf5 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob033_traffic_lights_prompt.txt @@ -0,0 +1,148 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input pass_request + - output reg [7:0] clock + - output reg red + - output reg yellow + - output reg green + +The module should implement a traffic light controller with pedestrian +request functionality. Assume all sequential logic is triggered on the +positive edge of the clock. The reset input is active low asynchronous, +and should reset the traffic light to red state. + +The traffic light controller should work as follows: +- Normal operation: Traffic light cycles through red (10 clock cycles), + green (60 clock cycles), and yellow (5 clock cycles) in sequence +- Pedestrian request: When pass_request is active, if remaining green time + is greater than 10 clock cycles, it should be shortened to 10 clock + cycles; if remaining green time is 10 clock cycles or less, it remains + unchanged +- Light sequence: Red → Green → Yellow → Red (repeating) +- Clock counter: The clock output shows the countdown timer for the current + light state +- Light outputs: Only one light should be active at a time (red, yellow, + or green) + +Traffic light operation: +- Red light: 10 clock cycles, clock counts down from 10 to 1 +- Green light: 60 clock cycles normally, or 10 clock cycles if pedestrian + request is active and remaining time > 10 +- Yellow light: 5 clock cycles, clock counts down from 5 to 1 +- Pedestrian request: Shortens green time to 10 cycles if remaining time > 10 +- Reset behavior: Active low reset to red state with clock = 10 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule + ( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input pass_request, + output wire[7:0]clock, + output reg red, + output reg yellow, + output reg green + ); + + parameter idle = 2'd0, + s1_red = 2'd1, + s2_green = 2'd2, + s3_yellow = 2'd3; + reg [7:0] cnt; + reg [1:0] state; + reg p_red,p_yellow,p_green; //Buffer previous signal light values for rising edge detection + + +always @(posedge clk or negedge rst_n) + begin + if(!rst_n) + begin + state <= idle; + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + end + else case(state) + idle: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + state <= s1_red; + end + s1_red: + begin + p_red <= 1'b1; + p_green <= 1'b1; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s2_green; + else + state <= s1_red; + end + s2_green: + begin + p_red <= 1'b0; + p_green <= 1'b1; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s3_yellow; + else + state <= s2_green; + end + s3_yellow: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b1; + if (cnt == 3) + state <= s1_red; + else + state <= s3_yellow; + end + endcase + end + +always @(posedge clk or negedge rst_n) + if(!rst_n) + cnt <= 7'd10; + else if (pass_request&&green&&(cnt>10)) + cnt <= 7'd10; + else if (!green&&p_green) + cnt <= 7'd60; + else if (!yellow&&p_yellow) + cnt <= 7'd5; + else if (!red&&p_red) + cnt <= 7'd10; + else cnt <= cnt -1; + assign clock = cnt; + +always @(posedge clk or negedge rst_n) + if(!rst_n) + begin + yellow <= 1'd0; + red <= 1'd0; + green <= 1'd0; + end + else + begin + yellow <= p_yellow; + red <= p_red; + green <= p_green; + end + +endmodule + + +// notice: the why we set if (cnt == 3) for state transition, because at cnt 3, the next cycle +// cnt 2 the state will be updated +// cnt 1 the p_yellow, p_red, p_green will be updated +// cnt 0 the yellow, red, green will be updated +// next cycle, the traffic light will change +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob033_traffic_lights_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob033_traffic_lights_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..3f0649950bde0cffb62e5e6de99d06676e0caa8e --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob033_traffic_lights_ref.sv @@ -0,0 +1,107 @@ + +module RefModule + ( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input pass_request, + output wire[7:0]clock, + output reg red, + output reg yellow, + output reg green + ); + + parameter idle = 2'd0, + s1_red = 2'd1, + s2_green = 2'd2, + s3_yellow = 2'd3; + reg [7:0] cnt; + reg [1:0] state; + reg p_red,p_yellow,p_green; //Buffer previous signal light values for rising edge detection + + +always @(posedge clk or negedge rst_n) + begin + if(!rst_n) + begin + state <= idle; + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + end + else case(state) + idle: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + state <= s1_red; + end + s1_red: + begin + p_red <= 1'b1; + p_green <= 1'b0; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s2_green; + else + state <= s1_red; + end + s2_green: + begin + p_red <= 1'b0; + p_green <= 1'b1; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s3_yellow; + else + state <= s2_green; + end + s3_yellow: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b1; + if (cnt == 3) + state <= s1_red; + else + state <= s3_yellow; + end + endcase + end + +always @(posedge clk or negedge rst_n) + if(!rst_n) + cnt <= 7'd10; + else if (pass_request&&green&&(cnt>10)) + cnt <= 7'd10; + else if (!green&&p_green) + cnt <= 7'd60; + else if (!yellow&&p_yellow) + cnt <= 7'd5; + else if (!red&&p_red) + cnt <= 7'd10; + else cnt <= cnt -1; + assign clock = cnt; + +always @(posedge clk or negedge rst_n) + if(!rst_n) + begin + yellow <= 1'd0; + red <= 1'd0; + green <= 1'd0; + end + else + begin + yellow <= p_yellow; + red <= p_red; + green <= p_green; + end + +endmodule + + +// notice: the why we set if (cnt == 3) for state transition, because at cnt 3, the next cycle +// cnt 2 the state will be updated +// cnt 1 the p_yellow, p_red, p_green will be updated +// cnt 0 the yellow, red, green will be updated +// next cycle, the traffic light will change \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob033_traffic_lights_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob033_traffic_lights_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..b3b7eda5538208585526f69ada1c07a7459e47e2 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob033_traffic_lights_test.sv @@ -0,0 +1,195 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic pass_request + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + pass_request = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test normal traffic light cycle + pass_request = 0; + repeat(100) @(posedge clk); // Let it cycle through red->green->yellow->red + + // Test pedestrian request during green phase + pass_request = 1; + repeat(20) @(posedge clk); // Request during green + pass_request = 0; + @(posedge clk); + + // Test pedestrian request during red phase (should not affect) + pass_request = 1; + repeat(15) @(posedge clk); // Request during red + pass_request = 0; + @(posedge clk); + + // Test pedestrian request during yellow phase (should not affect) + pass_request = 1; + repeat(10) @(posedge clk); // Request during yellow + pass_request = 0; + @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + pass_request <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_clock; + int errortime_clock; + int errors_red; + int errortime_red; + int errors_yellow; + int errortime_yellow; + int errors_green; + int errortime_green; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic pass_request; + logic [7:0] clock_ref; + logic red_ref; + logic yellow_ref; + logic green_ref; + logic [7:0] clock_dut; + logic red_dut; + logic yellow_dut; + logic green_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, pass_request, clock_ref, red_ref, yellow_ref, green_ref, clock_dut, red_dut, yellow_dut, green_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .pass_request ); + + RefModule good1 ( + .clk, + .rst_n, + .pass_request, + .clock(clock_ref), + .red(red_ref), + .yellow(yellow_ref), + .green(green_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .pass_request, + .clock(clock_dut), + .red(red_dut), + .yellow(yellow_dut), + .green(green_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_clock) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "clock", stats1.errors_clock, stats1.errortime_clock); + else $display("Hint: Output '%s' has no mismatches.", "clock"); + if (stats1.errors_red) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "red", stats1.errors_red, stats1.errortime_red); + else $display("Hint: Output '%s' has no mismatches.", "red"); + if (stats1.errors_yellow) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "yellow", stats1.errors_yellow, stats1.errortime_yellow); + else $display("Hint: Output '%s' has no mismatches.", "yellow"); + if (stats1.errors_green) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "green", stats1.errors_green, stats1.errortime_green); + else $display("Hint: Output '%s' has no mismatches.", "green"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { clock_ref, red_ref, yellow_ref, green_ref } === ( { clock_ref, red_ref, yellow_ref, green_ref } ^ { clock_dut, red_dut, yellow_dut, green_dut } ^ { clock_ref, red_ref, yellow_ref, green_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (clock_ref !== ( clock_ref ^ clock_dut ^ clock_ref )) + begin if (stats1.errors_clock == 0) stats1.errortime_clock = $time; + stats1.errors_clock = stats1.errors_clock+1'b1; end + if (red_ref !== ( red_ref ^ red_dut ^ red_ref )) + begin if (stats1.errors_red == 0) stats1.errortime_red = $time; + stats1.errors_red = stats1.errors_red+1'b1; end + if (yellow_ref !== ( yellow_ref ^ yellow_dut ^ yellow_ref )) + begin if (stats1.errors_yellow == 0) stats1.errortime_yellow = $time; + stats1.errors_yellow = stats1.errors_yellow+1'b1; end + if (green_ref !== ( green_ref ^ green_dut ^ green_ref )) + begin if (stats1.errors_green == 0) stats1.errortime_green = $time; + stats1.errors_green = stats1.errors_green+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob034_gaming_machine_billing_program_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob034_gaming_machine_billing_program_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..4cd9b0294fcb2829f886c72ae5cf6f72658ee70e --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob034_gaming_machine_billing_program_prompt.txt @@ -0,0 +1,79 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input [9:0] money + - input set + - input boost + - output reg [9:0] remain + - output reg yellow + - output reg red + +The module should implement a gaming machine billing system with +prepaid charging functionality. Assume all sequential logic is triggered +on the positive edge of the clock. The reset input is active low +asynchronous, and should reset the system to initial state. + +The gaming machine billing system should work as follows: +- Prepaid charging: When set signal is active, the money value is added + to the remaining balance (remain) +- Game modes: Normal mode (1 yuan per minute) and boost mode (2 yuan per + minute) +- Default mode: Normal mode (boost = 0) +- Boost mode: Activated when boost = 1, charges 2 yuan per clock cycle +- Balance deduction: Each clock cycle deducts 1 yuan (normal) or 2 yuan + (boost) from remain +- Warning indicators: Yellow light when remain > 0 and < 10, red light + when remain < 1 in normal mode, when remain < 2 in boost mode. +- Continuous charging: Users can recharge during gameplay via set signal + +Gaming machine operation: +- Normal mode: remain decreases by 1 each clock cycle +- Boost mode: remain decreases by 2 each clock cycle +- Charging: When set = 1, money value is added to remain +- Yellow warning: Active when 0 < remain < 10 +- Red warning: Active when remain < 1 for normal mode, remaon < 2 for boost mode + (insufficient funds) +- Reset behavior: Active low reset to initial state + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule +( + input rst_n, + input clk, + input [9:0]money, + input set, + input boost, + output reg[9:0]remain, + output reg yellow, + output reg red +); + always@(posedge clk or negedge rst_n) begin + if(~rst_n) begin + yellow <= 0; + red <= 0; + end + else begin + yellow <= remain<10&&remain; + red <= boost? remain<3: remain<1; + end + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + remain <= 0; + else if(boost) + remain <= set ? remain+money: + remain<2? remain: + remain-2; + else + remain <= set ? remain+money: + remain<1? remain: + remain-1; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob034_gaming_machine_billing_program_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob034_gaming_machine_billing_program_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..84d31ece4f078c42fce9b0073d9ff77540935471 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob034_gaming_machine_billing_program_ref.sv @@ -0,0 +1,36 @@ + +module RefModule +( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input [9:0]money, + input set, + input boost, + output reg[9:0]remain, + output reg yellow, + output reg red +); + always@(posedge clk or negedge rst_n) begin + if(~rst_n) begin + yellow <= 0; + red <= 0; + end + else begin + yellow <= remain<10&&remain; + red <= boost? remain<2: remain<1; + end + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + remain <= 0; + else if(boost) + remain <= set ? remain+money: + remain<2? remain: + remain-2; + else + remain <= set ? remain+money: + remain<1? remain: + remain-1; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob034_gaming_machine_billing_program_test.sv b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob034_gaming_machine_billing_program_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..e447ea3b14ac0f37fb8bc116b201da5c61489f87 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_assignment/Prob034_gaming_machine_billing_program_test.sv @@ -0,0 +1,208 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [9:0] money, + output logic set, + output logic boost + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + money = 0; + set = 0; + boost = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test normal mode charging + set = 1; + money = 10'd50; @(posedge clk); // Charge 50 yuan + set = 0; + @(posedge clk); + + // Test normal mode consumption (1 yuan per cycle) + boost = 0; + repeat(20) @(posedge clk); // Consume 20 yuan + + // Test charging during gameplay + boost = 0; + set = 1; + money = 10'd20; @(posedge clk); // Charge while playing + set = 0; + @(posedge clk); + + boost = 1; + repeat(25) @(posedge clk); // Consume 50 yuan + + // Test insufficient funds in normal mode + boost = 0; + repeat(10) @(posedge clk); // Try to consume more than available + + // Test insufficient funds in boost mode + set = 1; + money = 10'd3; @(posedge clk); // Charge small amount + set = 0; + @(posedge clk); + boost = 1; + repeat(5) @(posedge clk); // Try to consume in boost mode + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + set <= $random; + boost <= $random; + money <= $random % 1024; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_remain; + int errortime_remain; + int errors_yellow; + int errortime_yellow; + int errors_red; + int errortime_red; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [9:0] money; + logic set; + logic boost; + logic [9:0] remain_ref; + logic yellow_ref; + logic red_ref; + logic [9:0] remain_dut; + logic yellow_dut; + logic red_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, money, set, boost, remain_ref, yellow_ref, red_ref, remain_dut, yellow_dut, red_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .money, + .set, + .boost ); + + RefModule good1 ( + .clk, + .rst_n, + .money, + .set, + .boost, + .remain(remain_ref), + .yellow(yellow_ref), + .red(red_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .money, + .set, + .boost, + .remain(remain_dut), + .yellow(yellow_dut), + .red(red_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_remain) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "remain", stats1.errors_remain, stats1.errortime_remain); + else $display("Hint: Output '%s' has no mismatches.", "remain"); + if (stats1.errors_yellow) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "yellow", stats1.errors_yellow, stats1.errortime_yellow); + else $display("Hint: Output '%s' has no mismatches.", "yellow"); + if (stats1.errors_red) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "red", stats1.errors_red, stats1.errortime_red); + else $display("Hint: Output '%s' has no mismatches.", "red"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { remain_ref, yellow_ref, red_ref } === ( { remain_ref, yellow_ref, red_ref } ^ { remain_dut, yellow_dut, red_dut } ^ { remain_ref, yellow_ref, red_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (remain_ref !== ( remain_ref ^ remain_dut ^ remain_ref )) + begin if (stats1.errors_remain == 0) stats1.errortime_remain = $time; + stats1.errors_remain = stats1.errors_remain+1'b1; end + if (yellow_ref !== ( yellow_ref ^ yellow_dut ^ yellow_ref )) + begin if (stats1.errors_yellow == 0) stats1.errortime_yellow = $time; + stats1.errors_yellow = stats1.errors_yellow+1'b1; end + if (red_ref !== ( red_ref ^ red_dut ^ red_ref )) + begin if (stats1.errors_red == 0) stats1.errortime_red = $time; + stats1.errors_red = stats1.errors_red+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob003_non-overlapping_sequence_detect_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob003_non-overlapping_sequence_detect_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..abef12e4ad2f95b24935d805d623b8775a3ee6dc --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob003_non-overlapping_sequence_detect_prompt.txt @@ -0,0 +1,126 @@ +I would like you to implement a module named `TopModule` with the following interface. All input and output ports are one bit unless otherwise specified. + +### Inputs: +- `clk`: System clock signal. +- `rst_n`: Asynchronous reset signal, active low. +- `data`: Single-bit input signal, representing the data to be checked. + +### Outputs: +- `match`: A signal that outputs `1` when the input signal `data` matches the target sequence `011100`. It remains `0` at all other times. +- `not_match`: A signal that outputs `1` when the input signal `data` does not match the target sequence `011100`. It remains `0` at all other times. + +### Module Description: +The module must detect the sequence `011100` from the input signal `data`. Please use the state machine to solve this problem. The detection rules are as follows: +1. The sequence check begins with every sixth bit of input data, meaning the module processes the input in groups of six bits and does not overlap or reuse bits between groups. +2. If the first bit of a group does not match the sequence, the module skips the entire group and starts checking from the next group beginning at the seventh bit. +3. When the sequence `011100` is detected, the `match` output should assert (go high). +4. If the sequence is not detected in a group, the `not_match` output should assert (go high). + +### Output Logic: +The output signals `match` and `not_match` should be implemented as follows: +- Declare both outputs as `reg` type: `output reg match, output reg not_match` +- Use combinational logic with Wildcard Sensitivity List block to control the outputs +- The outputs are mutually exclusive - only one can be high at a time + +### Timing and Behavior: +- When `rst_n` is low, the module should reset to its initial state and begin sequence detection from the start. +- On every positive clock edge (`clk`), the module should evaluate the input signal `data` and transition states accordingly. + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input data, + output reg match, + output reg not_match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1=4'd1, + s2=4'd2, + s3=4'd3, + s4=4'd4, + s5=4'd5, + s6=4'd6, + sf1=4'd7, + sf2=4'd8, + sf3=4'd9, + sf4=4'd10, + sf5=4'd11, + sf6=4'd12; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data) +begin + case(pstate) + idle: + if(data==0) + nstate=s1; //First bit matched + else + nstate=sf1; + s1: + nstate=data?s2:sf2; + s2: + nstate=data?s3:sf3; + s3: + nstate=data?s4:sf4; + s4: + nstate=data?sf5:s5; + s5: + nstate=data?sf6:s6; + s6: + nstate=data?sf1:s1; + sf1: + nstate=sf2; + sf2: + nstate=sf3; + sf3: + nstate=sf4; + sf4: + nstate=sf5; + sf5: + nstate=sf6; + sf6: + nstate=s1; + default: + nstate=idle; + endcase +end + +always @(pstate or data or rst_n) +begin + if(!rst_n==1) + begin + match=1'b0; + not_match = 1'b0; + end + else if(pstate==s6) + begin + match=1'b1; + not_match = 1'b0; + end + else if(pstate==sf6) + begin + match=1'b0; + not_match = 1'b1; + end + else + begin + match=1'b0; + not_match = 1'b0; + end +end + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob003_non-overlapping_sequence_detect_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob003_non-overlapping_sequence_detect_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..10012372821357962ecd8de990a1bdcfe95a7ef1 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob003_non-overlapping_sequence_detect_ref.sv @@ -0,0 +1,95 @@ +module RefModule( + input clk, + input rst_n, + input data, + output reg match, + output reg not_match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1=4'd1, + s2=4'd2, + s3=4'd3, + s4=4'd4, + s5=4'd5, + s6=4'd6, + sf1=4'd7, + sf2=4'd8, + sf3=4'd9, + sf4=4'd10, + sf5=4'd11, + sf6=4'd12; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data) +begin + case(pstate) + idle: + if(data==0) + nstate=s1; //First bit matched + else + nstate=sf1; + s1: + nstate=data?s2:sf2; + s2: + nstate=data?s3:sf3; + s3: + nstate=data?s4:sf4; + s4: + nstate=data?sf5:s5; + s5: + nstate=data?sf6:s6; + s6: + nstate=data?sf1:s1; + sf1: + nstate=sf2; + sf2: + nstate=sf3; + sf3: + nstate=sf4; + sf4: + nstate=sf5; + sf5: + nstate=sf6; + sf6: + nstate=data?sf1:s1; + default: + nstate=idle; + endcase +end + +always @(pstate or data or rst_n) +begin + if(!rst_n==1) + begin + match=1'b0; + not_match = 1'b0; + end + else if(pstate==s6) + begin + match=1'b1; + not_match = 1'b0; + end + else if(pstate==sf6) + begin + match=1'b0; + not_match = 1'b1; + end + else + begin + match=1'b0; + not_match = 1'b0; + end +end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob003_non-overlapping_sequence_detect_test.sv b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob003_non-overlapping_sequence_detect_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..f7a171a1efc9327b0131c9b4d8f328995dc37c28 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob003_non-overlapping_sequence_detect_test.sv @@ -0,0 +1,150 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + +module stimulus_gen ( + input wire clk, + output reg rst_n, + output reg data +); + + initial begin + // Initialize signals + rst_n = 1'b0; + data = 1'b0; + + // Apply reset + @(posedge clk); + rst_n = 1'b1; + @(posedge clk) + @(posedge clk) + + // Test sequence: Provide the sequence "011100" + @(posedge clk) data = 1'b0; // First bit + @(posedge clk) data = 1'b1; // Second bit + @(posedge clk) data = 1'b1; // Third bit + @(posedge clk) data = 1'b1; // Fourth bit + @(posedge clk) data = 1'b0; // Fifth bit + @(posedge clk) data = 1'b0; // Sixth bit + + // Additional test cases + repeat(500) @(negedge clk) begin + data <= $random; + end + + // End simulation after sufficient cycles + repeat(100) @(posedge clk); + #1 $finish; + end + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + int errors_not_match; + int errortime_not_match; + + int clocks; + } stats; + + stats stats1; + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk = 0; + initial forever + #5 clk = ~clk; + + logic rst_n; + logic data; + logic match_ref; + logic match_dut; + logic not_match_ref; + logic not_match_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data, match_ref, match_dut, not_match_ref, not_match_dut); + end + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // Instantiate stimulus generator + stimulus_gen stim1 ( + .clk(clk), + .rst_n(rst_n), + .data(data) + ); + + // Instantiate reference module + RefModule good1 ( + .clk(clk), + .rst_n(rst_n), + .data(data), + .match(match_ref), + .not_match(not_match_ref) + ); + + // Instantiate DUT (TopModule) + TopModule top_module1 ( + .clk(clk), + .rst_n(rst_n), + .data(data), + .match(match_dut), + .not_match(not_match_dut) + ); + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Delay until the end of the time step + @(strobe); + end + endtask + + final begin + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + if (stats1.errors_not_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "not_match", stats1.errors_not_match, stats1.errortime_not_match); + else $display("Hint: Output '%s' has no mismatches.", "not_match"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: Ensure outputs match the reference module + assign tb_match = ( { match_ref, not_match_ref } === ( { match_ref, not_match_ref } ^ { match_dut, not_match_dut } ^ { match_ref, not_match_ref } ) ); + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (match_ref !== ( match_ref ^ match_dut ^ match_ref )) begin + if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match++; + end + if (not_match_ref !== ( not_match_ref ^ not_match_dut ^ not_match_ref )) begin + if (stats1.errors_not_match == 0) stats1.errortime_not_match = $time; + stats1.errors_not_match++; + end + end + + // Add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob004_continuous_input_sequence_detect2_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob004_continuous_input_sequence_detect2_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..dbe726ed58e047fd141d919ef02578a323d76309 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob004_continuous_input_sequence_detect2_prompt.txt @@ -0,0 +1,111 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input data + - input data_valid + - output match + +The module should implement a sequence detection circuit that detects the +sequence "0110" in the input data stream. The data_valid signal indicates +when the input data is valid and should be considered for sequence detection. +When data_valid is high, the current input data is valid and participates +in sequence detection. When data_valid is low, the current input data is +invalid and should be ignored (not considered for sequence detection). + +The sequence detection should work as follows: +- Use state machine and update it sequentially +- Monitor the input data stream when data_valid is high +- Detect when the sequence "0110" appears in the valid data +- Output match signal should be high when the sequence "0110" is detected +- Output match signal should be low at all other times + +Output Logic for match signal: +- When reset is active (rst_n = 0): match = 0 +- When reset is inactive (rst_n = 1) AND sequence is 0110: match = 1 +- When reset is inactive (rst_n = 1) AND sequence is NOT 0110: match = 0 + + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input data, + input data_valid, + output reg match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1_d0=4'd1, + s2_d01=4'd2, + s3_d011=4'd3, + s4_d0110=4'd4; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data or data_valid) +begin + case(pstate) + idle: + if(data_valid && !data) + nstate=s1_d0; //First bit matched + else + nstate=idle; + s1_d0: + if (data_valid) + begin + if (data) nstate = s2_d01; //Data valid and is 1, first two bits 01 matched, next state is s2_d01 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s1_d0; //Data invalid, stay in s1_d0 + s2_d01: + if (data_valid) + begin + if (data) nstate = s3_d011; //Data valid and is 1, first three bits 011 matched, next state is s3_d011 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s2_d01; //Data invalid, stay in s2_d01 + s3_d011: + if (data_valid) + begin + if (!data) nstate = s4_d0110; //Data valid and is 0, first four bits 0110 matched, next state is s4_d0110 + else nstate = idle; //Data valid but is 1, no match, next state is idle + end + else nstate = idle; //Data invalid, stay in s3_d011 + s4_d0110: + if (data_valid) + begin + if (!data) nstate = s1_d0; //Data valid and is 0, matches first bit 0 of target sequence, next state is s1_d0 + else nstate = idle; //Data valid but is 1, does not match target sequence, next state is idle + end + else nstate = idle; //Data invalid, next state is idle + default: + nstate=idle; + endcase +end + +always @(pstate or rst_n) +begin + if(!rst_n==1) + match=1'b0; + else if(pstate==s4_d0110) //Entering state s4_d0110 means all four bits matched, assert match signal high + match=1'b1; + else + match=1'b0; +end + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob004_continuous_input_sequence_detect2_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob004_continuous_input_sequence_detect2_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..de2de151907bcac48bce85248c54dd0ccded8d1f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob004_continuous_input_sequence_detect2_ref.sv @@ -0,0 +1,78 @@ + +module RefModule( + input clk, + input rst_n, + input data, + input data_valid, + output reg match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1_d0=4'd1, + s2_d01=4'd2, + s3_d011=4'd3, + s4_d0110=4'd4; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data or data_valid) +begin + case(pstate) + idle: + if(data_valid && !data) + nstate=s1_d0; //First bit matched + else + nstate=idle; + s1_d0: + if (data_valid) + begin + if (data) nstate = s2_d01; //Data valid and is 1, first two bits 01 matched, next state is s2_d01 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s1_d0; //Data invalid, stay in s1_d0 + s2_d01: + if (data_valid) + begin + if (data) nstate = s3_d011; //Data valid and is 1, first three bits 011 matched, next state is s3_d011 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s2_d01; //Data invalid, stay in s2_d01 + s3_d011: + if (data_valid) + begin + if (!data) nstate = s4_d0110; //Data valid and is 0, first four bits 0110 matched, next state is s4_d0110 + else nstate = idle; //Data valid but is 1, no match, next state is idle + end + else nstate = s3_d011; //Data invalid, stay in s3_d011 + s4_d0110: + if (data_valid) + begin + if (!data) nstate = s1_d0; //Data valid and is 0, matches first bit 0 of target sequence, next state is s1_d0 + else nstate = idle; //Data valid but is 1, does not match target sequence, next state is idle + end + else nstate = idle; //Data invalid, next state is idle + default: + nstate=idle; + endcase +end + +always @(pstate or rst_n) +begin + if(!rst_n==1) + match=1'b0; + else if(pstate==s4_d0110) //Entering state s4_d0110 means all four bits matched, assert match signal high + match=1'b1; + else + match=1'b0; +end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob004_continuous_input_sequence_detect2_test.sv b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob004_continuous_input_sequence_detect2_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..82239fb98ff463683ce3e113ae82b524feb863f9 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob004_continuous_input_sequence_detect2_test.sv @@ -0,0 +1,163 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + output logic rst_n, + output logic data, + output logic data_valid +); + + initial begin + rst_n = 0; + data = 0; + data_valid = 0; + @(posedge clk); + @(posedge clk); + rst_n = 1; + @(posedge clk); + @(posedge clk); + + // Test specific sequence: 0110 + data_valid = 1; + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + data_valid = 0; + @(posedge clk); + @(posedge clk); + + // Test with data_valid low (should ignore data) + data_valid = 0; + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + @(posedge clk); + + // Test overlapping sequences + data_valid = 1; + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + @(posedge clk); + + // Random testing + repeat(500) @(negedge clk) begin + data_valid <= $random; + data <= $random; + end + + #1 $finish; + end + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + + int clocks; + } stats; + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + logic rst_n; + logic data; + logic data_valid; + logic match_ref; + logic match_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data, data_valid, match_ref, match_dut ); + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + stimulus_gen stim1 ( + .clk, + .* , + .rst_n, + .data, + .data_valid ); + RefModule good1 ( + .clk, + .rst_n, + .data, + .data_valid, + .match(match_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .data, + .data_valid, + .match(match_dut) ); + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { match_ref } === ( { match_ref } ^ { match_dut } ^ { match_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (match_ref !== ( match_ref ^ match_dut ^ match_ref )) + begin if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match = stats1.errors_match+1'b1; end + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob012_state_machine_overlapping_sequence_detection_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob012_state_machine_overlapping_sequence_detection_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..954a1021785154d839b3b13936ca1fa1899e9755 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob012_state_machine_overlapping_sequence_detection_prompt.txt @@ -0,0 +1,65 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst + - input data + - output reg flag + +The module should implement a state machine that detects the sequence "1011" +with overlapping detection capability. The state machine should detect +multiple occurrences of the sequence even when they overlap. reset is active low. + +The sequence detection should work as follows: +- Monitor the input data stream continuously +- Detect when the sequence "1011" appears in the input +- Support overlapping detection: "10110111" should be detected twice +- Output flag signal should be high for one clock cycle when the sequence + is detected, starting from the next clock cycle after detection, that is + two clock cycles after the final '1' is on data + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input wire clk , + input wire rst , + input wire data , + output reg flag +); +//*************code***********// + parameter S0=0, S1=1, S2=2, S3=3, S4=4; + reg [2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= S0; + else + state <= nstate; + end + + always@(*) begin + if(~rst) + nstate <= S0; + else + case(state) + S0 : nstate <= data? S1: S0; + S1 : nstate <= data? S1: S2; + S2 : nstate <= data? S3: S0; + S3 : nstate <= data? S4: S0; + S4 : nstate <= data? S1: S2; + default: nstate <= S0; + endcase + end + + always@(posedge clk or negedge rst) begin + if(~rst) + flag <= 0; + else + flag <= state==S4; + end + +//*************code***********// +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob012_state_machine_overlapping_sequence_detection_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob012_state_machine_overlapping_sequence_detection_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..9e6792cac0abf39247e8718dc9d59f9e638ff7b8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob012_state_machine_overlapping_sequence_detection_ref.sv @@ -0,0 +1,40 @@ +module RefModule( + input wire clk , + input wire rst , + input wire data , + output reg flag +); +//*************code***********// + parameter S0=0, S1=1, S2=2, S3=3, S4=4; + reg [2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= S0; + else + state <= nstate; + end + + always@(*) begin + if(~rst) + nstate <= S0; + else + case(state) + S0 : nstate <= data? S1: S0; + S1 : nstate <= data? S1: S2; + S2 : nstate <= data? S3: S0; + S3 : nstate <= data? S4: S2; + S4 : nstate <= data? S1: S2; + default: nstate <= S0; + endcase + end + + always@(posedge clk or negedge rst) begin + if(~rst) + flag <= 0; + else + flag <= state==S4; + end + +//*************code***********// +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob012_state_machine_overlapping_sequence_detection_test.sv b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob012_state_machine_overlapping_sequence_detection_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..a837d96a98935a96d3a2d8d9b3e47d692a7e56ee --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob012_state_machine_overlapping_sequence_detection_test.sv @@ -0,0 +1,164 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst, + output logic data + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + data = 0; + repeat(10) @(posedge clk); + rst <= 1; + @(posedge clk); + + // Test sequence "1011" - should detect once + data <= 1; @(posedge clk); // First bit + data <= 0; @(posedge clk); // Second bit + data <= 1; @(posedge clk); // Third bit + data <= 1; @(posedge clk); // Fourth bit - should detect here + + // Test overlapping sequences "10110111" - should detect twice + data <= 0; @(posedge clk); // Fifth bit + data <= 1; @(posedge clk); // Sixth bit + data <= 1; @(posedge clk); // Seventh bit - should detect here + data <= 1; @(posedge clk); // Eighth bit + + // Test non-matching sequence + data <= 0; @(posedge clk); + data <= 0; @(posedge clk); + data <= 1; @(posedge clk); + data <= 0; @(posedge clk); + + // Test another "1011" sequence + data <= 1; @(posedge clk); + data <= 0; @(posedge clk); + data <= 1; @(posedge clk); + data <= 1; @(posedge clk); // Should detect here + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + data <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_flag; + int errortime_flag; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic data; + logic flag_ref; + logic flag_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, data, flag_ref, flag_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst, + .data ); + + RefModule good1 ( + .clk, + .rst, + .data, + .flag(flag_ref) ); + + TopModule top_module1 ( + .clk, + .rst, + .data, + .flag(flag_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_flag) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "flag", stats1.errors_flag, stats1.errortime_flag); + else $display("Hint: Output '%s' has no mismatches.", "flag"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { flag_ref } === ( { flag_ref } ^ { flag_dut } ^ { flag_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (flag_ref !== ( flag_ref ^ flag_dut ^ flag_ref )) + begin if (stats1.errors_flag == 0) stats1.errortime_flag = $time; + stats1.errors_flag = stats1.errors_flag+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob015_vending_machine2_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob015_vending_machine2_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..efd8261f327cde5a3b3d8c07af409a69f6b9328f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob015_vending_machine2_prompt.txt @@ -0,0 +1,102 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst + - input d1 + - input d2 + - input sel + - output reg out1 + - output reg out2 + - output reg out3 + +The module should implement a vending machine that accepts two types of +currency (0.5 yuan and 1 yuan) and dispenses two types of beverages +(1.5 yuan and 2.5 yuan) with change dispensing capability. +reset is active low + +The vending machine should work as follows: +- Input currency: d1 represents 0.5 yuan, d2 represents 1 yuan +- Beverage selection: sel chooses between two beverages +- Beverage prices: Beverage 1 costs 1.5 yuan, Beverage 2 costs 2.5 yuan +- Change dispensing: Only 0.5 yuan coins are dispensed as change + +Currency input behavior: +- Input currency signals (d1, d2) are automatically edge-detected +- Each currency input produces a pulse signal that goes high on clock + rising edge and low on clock falling edge +- The machine accumulates the total amount of money inserted + +Beverage dispensing: +- out1: Dispenses beverage 1 (1.5 yuan) +- out2: Dispenses beverage 2 (2.5 yuan) +- out3: Dispenses change (0.5 yuan coins only) + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input wire clk , + input wire rst , + input wire d1 , + input wire d2 , + input wire sel , + + output reg out1, + output reg out2, + output reg out3 +); +//*************code***********// + + parameter S0=0, S0_5=1, S1=2, S1_5=3, S2=4, S2_5=5, S3=6; + reg[2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= 0; + else + state <= nstate; + end + + always@(*) begin + case(state) + S0 : nstate = d1? S0_5: + d2? S1: + nstate; + S0_5 : nstate = d1? S1: + d2? S1_5: + nstate; + S1 : nstate = d1? S1_5: + d2? S3: + nstate; + S1_5 : nstate = ~sel? S0: + d1? S2: + d2? S1: + nstate; + S2 : nstate = ~sel? S0: + d1? S2_5: + d2? S3: + nstate; + default: nstate = S0; + endcase + end + + always@(*) begin + if(~rst) begin + {out1, out2, out3} = 3'b000; + end + else begin + case(state) + S0, S0_5, S1: {out1, out2, out3} = 0; + S1_5 : {out1, out2, out3} = ~sel? 3'b100: 3'b000; + S2 : {out1, out2, out3} = ~sel? 3'b101: 3'b000; + S2_5 : {out1, out2, out3} = ~sel? 3'b101: 3'b010; + S3 : {out1, out2, out3} = ~sel? 3'b101: 3'b011; + default : {out1, out2, out3} = 3'b000; + endcase + end + end +//*************code***********// +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob015_vending_machine2_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob015_vending_machine2_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..a1ac1b88ccb70a8aeabfb77968ec0cc89b517377 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob015_vending_machine2_ref.sv @@ -0,0 +1,64 @@ + +module RefModule( + input wire clk , + input wire rst , + input wire d1 , + input wire d2 , + input wire sel , + + output reg out1, + output reg out2, + output reg out3 +); +//*************code***********// + + parameter S0=0, S0_5=1, S1=2, S1_5=3, S2=4, S2_5=5, S3=6; + reg[2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= 0; + else + state <= nstate; + end + + always@(*) begin + case(state) + S0 : nstate = d1? S0_5: + d2? S1: + nstate; + S0_5 : nstate = d1? S1: + d2? S1_5: + nstate; + S1 : nstate = d1? S1_5: + d2? S2: + nstate; + S1_5 : nstate = ~sel? S0: + d1? S2: + d2? S2_5: + nstate; + S2 : nstate = ~sel? S0: + d1? S2_5: + d2? S3: + nstate; + default: nstate = S0; + endcase + end + + always@(*) begin + if(~rst) begin + {out1, out2, out3} = 3'b000; + end + else begin + case(state) + S0, S0_5, S1: {out1, out2, out3} = 0; + S1_5 : {out1, out2, out3} = ~sel? 3'b100: 3'b000; + S2 : {out1, out2, out3} = ~sel? 3'b101: 3'b000; + S2_5 : {out1, out2, out3} = ~sel? 3'b101: 3'b010; + S3 : {out1, out2, out3} = ~sel? 3'b101: 3'b011; + default : {out1, out2, out3} = 3'b000; + endcase + end + end +//*************code***********// +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob015_vending_machine2_test.sv b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob015_vending_machine2_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..17d6dee4540eb7c8bbb9f648823598a0a643812f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob015_vending_machine2_test.sv @@ -0,0 +1,214 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst, + output logic d1, + output logic d2, + output logic sel + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + d1 = 0; + d2 = 0; + sel = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test case 1: Insert 1.5 yuan and select beverage 1 (sel=0) + d1 = 1; @(posedge clk); // Insert 0.5 yuan + d1 = 0; @(posedge clk); + d1 = 1; @(posedge clk); // Insert another 0.5 yuan + d1 = 0; @(posedge clk); + d1 = 1; @(posedge clk); // Insert third 0.5 yuan + d1 = 0; @(posedge clk); + sel = 0; @(posedge clk); // Select beverage 1 + sel = 0; @(posedge clk); // Should dispense beverage 1 + + // Test case 2: Insert 2.5 yuan and select beverage 2 (sel=1) + d2 = 1; @(posedge clk); // Insert 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert another 1 yuan + d2 = 0; @(posedge clk); + d1 = 1; @(posedge clk); // Insert 0.5 yuan + d1 = 0; @(posedge clk); + sel = 1; @(posedge clk); // Select beverage 2 + sel = 1; @(posedge clk); // Should dispense beverage 2 + + // Test case 3: Insert 2 yuan and select beverage 1 (sel=0) - should get change + d2 = 1; @(posedge clk); // Insert 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert another 1 yuan + d2 = 0; @(posedge clk); + sel = 0; @(posedge clk); // Select beverage 1 + sel = 0; @(posedge clk); // Should dispense beverage 1 + 0.5 yuan change + + // Test case 4: Insert 3 yuan and select beverage 2 (sel=1) - should get change + d2 = 1; @(posedge clk); // Insert 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert another 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert third 1 yuan + d2 = 0; @(posedge clk); + sel = 1; @(posedge clk); // Select beverage 2 + sel = 1; @(posedge clk); // Should dispense beverage 2 + 0.5 yuan change + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + d1 <= $random; + d2 <= $random; + sel <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_out1; + int errortime_out1; + int errors_out2; + int errortime_out2; + int errors_out3; + int errortime_out3; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic d1; + logic d2; + logic sel; + logic out1_ref; + logic out2_ref; + logic out3_ref; + logic out1_dut; + logic out2_dut; + logic out3_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, d1, d2, sel, out1_ref, out2_ref, out3_ref, out1_dut, out2_dut, out3_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst, + .d1, + .d2, + .sel ); + + RefModule good1 ( + .clk, + .rst, + .d1, + .d2, + .sel, + .out1(out1_ref), + .out2(out2_ref), + .out3(out3_ref) ); + + TopModule top_module1 ( + .clk, + .rst, + .d1, + .d2, + .sel, + .out1(out1_dut), + .out2(out2_dut), + .out3(out3_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_out1) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out1", stats1.errors_out1, stats1.errortime_out1); + else $display("Hint: Output '%s' has no mismatches.", "out1"); + if (stats1.errors_out2) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out2", stats1.errors_out2, stats1.errortime_out2); + else $display("Hint: Output '%s' has no mismatches.", "out2"); + if (stats1.errors_out3) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out3", stats1.errors_out3, stats1.errortime_out3); + else $display("Hint: Output '%s' has no mismatches.", "out3"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { out1_ref, out2_ref, out3_ref } === ( { out1_ref, out2_ref, out3_ref } ^ { out1_dut, out2_dut, out3_dut } ^ { out1_ref, out2_ref, out3_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (out1_ref !== ( out1_ref ^ out1_dut ^ out1_ref )) + begin if (stats1.errors_out1 == 0) stats1.errortime_out1 = $time; + stats1.errors_out1 = stats1.errors_out1+1'b1; end + if (out2_ref !== ( out2_ref ^ out2_dut ^ out2_ref )) + begin if (stats1.errors_out2 == 0) stats1.errortime_out2 = $time; + stats1.errors_out2 = stats1.errors_out2+1'b1; end + if (out3_ref !== ( out3_ref ^ out3_dut ^ out3_ref )) + begin if (stats1.errors_out3 == 0) stats1.errortime_out3 = $time; + stats1.errors_out3 = stats1.errors_out3+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob020_write_state_machine_two_stage_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob020_write_state_machine_two_stage_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..7ef5d8b2d95fd5ed4eb607afb67fa0ad3e750e23 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob020_write_state_machine_two_stage_prompt.txt @@ -0,0 +1,85 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst + - input data + - output reg flag + +The module should implement a Moore state machine using a two-stage +description method. The state machine has five states (S0, S1, S2, S3, S4) +and transitions based on the input data signal. + +The state machine should work as follows: +- Input signal: data (wire type) +- Output signal: flag (reg type) +- Reset: rst is active low +- Clock: clk for synchronous operation + +State transition table: + data +current state 1 0 + S0 next state S1 S0 + S1 S2 S1 + S2 S3 S2 + S3 S4 S3 + S4 S1 S0 + +Output logic: +- When current state is S4, the output flag should be 1 +- For all other states (S0, S1, S2, S3), the output flag should be 0 + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input wire clk , + input wire rst , + input wire data , + output reg flag +); + + parameter S0 = 'd0, S1 = 'd1, S2 = 'd2, S3 = 'd3 ,S4 = 'd4 ; + reg [2:0] current_state; + reg [2:0] next_state; + + always@(posedge clk or negedge rst)begin + if(rst == 1'b0)begin + current_state <= S0; + end + else begin + current_state <= next_state; + end + end + + always@(*)begin + case(current_state) + S0:begin + next_state = data ? S1 : S0; + flag = 1'b0; + end + S1:begin + next_state = data ? S2 : S1; + flag = 1'b0; + end + S2:begin + next_state = data ? S3 : S2; + flag = 1'b0; + end + S3:begin + next_state = data ? S4 : S3; + flag = 1'b0; + end + S4:begin + next_state = data ? S1 : S1; + flag = 1'b1; + end + default:begin + next_state = S0; + flag = 1'b0; + end + endcase + end + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob020_write_state_machine_two_stage_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob020_write_state_machine_two_stage_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..36947644c473bb11cbd0926164312fdc9d7305f3 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob020_write_state_machine_two_stage_ref.sv @@ -0,0 +1,51 @@ + +module RefModule( + input wire clk , + input wire rst , + input wire data , + output reg flag +); + + parameter S0 = 'd0, S1 = 'd1, S2 = 'd2, S3 = 'd3 ,S4 = 'd4 ; + reg [2:0] current_state; + reg [2:0] next_state; + + always@(posedge clk or negedge rst)begin + if(rst == 1'b0)begin + current_state <= S0; + end + else begin + current_state <= next_state; + end + end + + always@(*)begin + case(current_state) + S0:begin + next_state = data ? S1 : S0; + flag = 1'b0; + end + S1:begin + next_state = data ? S2 : S1; + flag = 1'b0; + end + S2:begin + next_state = data ? S3 : S2; + flag = 1'b0; + end + S3:begin + next_state = data ? S4 : S3; + flag = 1'b0; + end + S4:begin + next_state = data ? S1 : S0; + flag = 1'b1; + end + default:begin + next_state = S0; + flag = 1'b0; + end + endcase + end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob020_write_state_machine_two_stage_test.sv b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob020_write_state_machine_two_stage_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..84ae9288b92210b890dc070ecc6902848b26b92f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob020_write_state_machine_two_stage_test.sv @@ -0,0 +1,174 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst, + output logic data + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + data = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test state transitions: S0->S1->S2->S3->S4 (flag=1) + data = 1; @(posedge clk); // S0->S1 + data = 1; @(posedge clk); // S1->S2 + data = 1; @(posedge clk); // S2->S3 + data = 1; @(posedge clk); // S3->S4 (flag should be 1) + + // Test S4->S1 transition (data=1) + data = 1; @(posedge clk); // S4->S1 + + // Test S4->S0 transition (data=0) + data = 1; @(posedge clk); // S1->S2 + data = 1; @(posedge clk); // S2->S3 + data = 1; @(posedge clk); // S3->S4 + data = 0; @(posedge clk); // S4->S0 + + // Test staying in S0 (data=0) + data = 0; @(posedge clk); // S0->S0 + data = 0; @(posedge clk); // S0->S0 + + // Test staying in S1 (data=0) + data = 1; @(posedge clk); // S0->S1 + data = 0; @(posedge clk); // S1->S1 + data = 0; @(posedge clk); // S1->S1 + + // Test staying in S2 (data=0) + data = 1; @(posedge clk); // S1->S2 + data = 0; @(posedge clk); // S2->S2 + data = 0; @(posedge clk); // S2->S2 + + // Test staying in S3 (data=0) + data = 1; @(posedge clk); // S2->S3 + data = 0; @(posedge clk); // S3->S3 + data = 0; @(posedge clk); // S3->S3 + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + data <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_flag; + int errortime_flag; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic data; + logic flag_ref; + logic flag_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, data, flag_ref, flag_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst, + .data ); + + RefModule good1 ( + .clk, + .rst, + .data, + .flag(flag_ref) ); + + TopModule top_module1 ( + .clk, + .rst, + .data, + .flag(flag_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_flag) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "flag", stats1.errors_flag, stats1.errortime_flag); + else $display("Hint: Output '%s' has no mismatches.", "flag"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { flag_ref } === ( { flag_ref } ^ { flag_dut } ^ { flag_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (flag_ref !== ( flag_ref ^ flag_dut ^ flag_ref )) + begin if (stats1.errors_flag == 0) stats1.errortime_flag = $time; + stats1.errors_flag = stats1.errors_flag+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob033_traffic_lights_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob033_traffic_lights_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..22f0d36d033bf8ee9fb633d53dcb4b5d89ea3834 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob033_traffic_lights_prompt.txt @@ -0,0 +1,149 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input pass_request + - output reg [7:0] clock + - output reg red + - output reg yellow + - output reg green + +The module should implement a traffic light controller with pedestrian +request functionality. Assume all sequential logic is triggered on the +positive edge of the clock. The reset input is active low asynchronous, +and red is the first light lit after reset. + +The traffic light controller should work as follows: +- Normal operation: Traffic light cycles through red (10 clock cycles), + green (60 clock cycles), and yellow (5 clock cycles) in sequence +- Pedestrian request: When pass_request is active, if remaining green time + is greater than 10 clock cycles, it should be shortened to 10 clock + cycles; if remaining green time is 10 clock cycles or less, it remains + unchanged +- Light sequence: Red → Green → Yellow → Red (repeating) +- Clock counter: The clock output shows the countdown timer for the current + light state +- Light outputs: Only one light should be active at a time (red, yellow, + or green) + +Traffic light operation: +- Red light: 10 clock cycles, clock counts down from 10 to 1 +- Green light: 60 clock cycles normally, or 10 clock cycles if pedestrian + request is active and remaining time > 10 +- Yellow light: 5 clock cycles, clock counts down from 5 to 1 +- Pedestrian request: Shortens green time to 10 cycles if remaining time > 10 +- Reset behavior: Active low reset with clock = 10; all lights stay off during + reset and for two clock edges after release, then red turns on with clock = 10 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule + ( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input pass_request, + output wire[7:0]clock, + output reg red, + output reg yellow, + output reg green + ); + + parameter idle = 2'd0, + s1_red = 2'd1, + s2_green = 2'd2, + s3_yellow = 2'd3; + reg [7:0] cnt; + reg [1:0] state; + reg p_red,p_yellow,p_green; //Buffer previous signal light values for rising edge detection + + +always @(posedge clk or negedge rst_n) + begin + if(!rst_n) + begin + state <= idle; + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + end + else case(state) + idle: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + state <= s1_red; + end + s1_red: + begin + p_red <= 1'b1; + p_green <= 1'b0; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s3_yellow; + else + state <= s1_red; + end + s2_green: + begin + p_red <= 1'b0; + p_green <= 1'b1; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s1_red; + else + state <= s2_green; + end + s3_yellow: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b1; + if (cnt == 3) + state <= s1_red; + else + state <= s3_yellow; + end + endcase + end + +always @(posedge clk or negedge rst_n) + if(!rst_n) + cnt <= 7'd10; + else if (pass_request&&green&&(cnt>10)) + cnt <= 7'd10; + else if (!green&&p_green) + cnt <= 7'd60; + else if (!yellow&&p_yellow) + cnt <= 7'd5; + else if (!red&&p_red) + cnt <= 7'd10; + else cnt <= cnt -1; + assign clock = cnt; + +always @(posedge clk or negedge rst_n) + if(!rst_n) + begin + yellow <= 1'd0; + red <= 1'd0; + green <= 1'd0; + end + else + begin + yellow <= p_yellow; + red <= p_red; + green <= p_green; + end + +endmodule + + +// notice: the why we set if (cnt == 3) for state transition, because at cnt 3, the next cycle +// cnt 2 the state will be updated +// cnt 1 the p_yellow, p_red, p_green will be updated +// cnt 0 the yellow, red, green will be updated +// next cycle, the traffic light will change +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob033_traffic_lights_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob033_traffic_lights_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..5845b4a83ab78acf2eb4237c0294cd70d5d1b8fd --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob033_traffic_lights_ref.sv @@ -0,0 +1,108 @@ +`timescale 1ns/1ns + +module RefModule + ( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input pass_request, + output wire[7:0]clock, + output reg red, + output reg yellow, + output reg green + ); + + parameter idle = 2'd0, + s1_red = 2'd1, + s2_green = 2'd2, + s3_yellow = 2'd3; + reg [7:0] cnt; + reg [1:0] state; + reg p_red,p_yellow,p_green; //Buffer previous signal light values for rising edge detection + + +always @(posedge clk or negedge rst_n) + begin + if(!rst_n) + begin + state <= idle; + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + end + else case(state) + idle: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + state <= s1_red; + end + s1_red: + begin + p_red <= 1'b1; + p_green <= 1'b0; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s2_green; + else + state <= s1_red; + end + s2_green: + begin + p_red <= 1'b0; + p_green <= 1'b1; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s3_yellow; + else + state <= s2_green; + end + s3_yellow: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b1; + if (cnt == 3) + state <= s1_red; + else + state <= s3_yellow; + end + endcase + end + +always @(posedge clk or negedge rst_n) + if(!rst_n) + cnt <= 7'd10; + else if (pass_request&&green&&(cnt>10)) + cnt <= 7'd10; + else if (!green&&p_green) + cnt <= 7'd60; + else if (!yellow&&p_yellow) + cnt <= 7'd5; + else if (!red&&p_red) + cnt <= 7'd10; + else cnt <= cnt -1; + assign clock = cnt; + +always @(posedge clk or negedge rst_n) + if(!rst_n) + begin + yellow <= 1'd0; + red <= 1'd0; + green <= 1'd0; + end + else + begin + yellow <= p_yellow; + red <= p_red; + green <= p_green; + end + +endmodule + + +// notice: the why we set if (cnt == 3) for state transition, because at cnt 3, the next cycle +// cnt 2 the state will be updated +// cnt 1 the p_yellow, p_red, p_green will be updated +// cnt 0 the yellow, red, green will be updated +// next cycle, the traffic light will change \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob033_traffic_lights_test.sv b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob033_traffic_lights_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..ab0d2d912b41d5f0f5370f2823e88596a8430e76 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_state_machine/Prob033_traffic_lights_test.sv @@ -0,0 +1,216 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic pass_request + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + pass_request = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test normal traffic light cycle + pass_request = 0; + repeat(100) @(posedge clk); // Let it cycle through red->green->yellow->red + + // Test pedestrian request during green phase + pass_request = 1; + repeat(20) @(posedge clk); // Request during green + pass_request = 0; + @(posedge clk); + + // Test pedestrian request during red phase (should not affect) + pass_request = 1; + repeat(15) @(posedge clk); // Request during red + pass_request = 0; + @(posedge clk); + + // Test pedestrian request during yellow phase (should not affect) + pass_request = 1; + repeat(10) @(posedge clk); // Request during yellow + pass_request = 0; + @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + pass_request <= $random; + end + + // === Additional checks: asynchronous active-low reset in mid-operation === + // Spec: "The reset input is active low asynchronous, and should reset the traffic light to red state"; + // "Reset behavior: Active low reset to red state with clock = 10; all lights are 0 during reset and for + // the first two clock edges after release". Earlier stimulus only resets at start-up. rst_n changes at + // posedge+2 (no edge race). + @(posedge clk); #2 pass_request <= 0; + rst_n <= 0; + repeat(3) @(posedge clk); + #2 rst_n <= 1; + repeat(40) @(posedge clk); // reset again during the following green phase + #2 rst_n <= 0; + @(posedge clk); + #2 rst_n <= 1; + repeat(17) @(posedge clk); // and once more during red + #2 rst_n <= 0; + repeat(2) @(posedge clk); + #2 rst_n <= 1; + repeat(240) @(posedge clk, negedge clk) begin + pass_request <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_clock; + int errortime_clock; + int errors_red; + int errortime_red; + int errors_yellow; + int errortime_yellow; + int errors_green; + int errortime_green; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic pass_request; + logic [7:0] clock_ref; + logic red_ref; + logic yellow_ref; + logic green_ref; + logic [7:0] clock_dut; + logic red_dut; + logic yellow_dut; + logic green_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, pass_request, clock_ref, red_ref, yellow_ref, green_ref, clock_dut, red_dut, yellow_dut, green_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .pass_request ); + + RefModule good1 ( + .clk, + .rst_n, + .pass_request, + .clock(clock_ref), + .red(red_ref), + .yellow(yellow_ref), + .green(green_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .pass_request, + .clock(clock_dut), + .red(red_dut), + .yellow(yellow_dut), + .green(green_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_clock) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "clock", stats1.errors_clock, stats1.errortime_clock); + else $display("Hint: Output '%s' has no mismatches.", "clock"); + if (stats1.errors_red) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "red", stats1.errors_red, stats1.errortime_red); + else $display("Hint: Output '%s' has no mismatches.", "red"); + if (stats1.errors_yellow) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "yellow", stats1.errors_yellow, stats1.errortime_yellow); + else $display("Hint: Output '%s' has no mismatches.", "yellow"); + if (stats1.errors_green) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "green", stats1.errors_green, stats1.errortime_green); + else $display("Hint: Output '%s' has no mismatches.", "green"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { clock_ref, red_ref, yellow_ref, green_ref } === ( { clock_ref, red_ref, yellow_ref, green_ref } ^ { clock_dut, red_dut, yellow_dut, green_dut } ^ { clock_ref, red_ref, yellow_ref, green_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (clock_ref !== ( clock_ref ^ clock_dut ^ clock_ref )) + begin if (stats1.errors_clock == 0) stats1.errortime_clock = $time; + stats1.errors_clock = stats1.errors_clock+1'b1; end + if (red_ref !== ( red_ref ^ red_dut ^ red_ref )) + begin if (stats1.errors_red == 0) stats1.errortime_red = $time; + stats1.errors_red = stats1.errors_red+1'b1; end + if (yellow_ref !== ( yellow_ref ^ yellow_dut ^ yellow_ref )) + begin if (stats1.errors_yellow == 0) stats1.errortime_yellow = $time; + stats1.errors_yellow = stats1.errors_yellow+1'b1; end + if (green_ref !== ( green_ref ^ green_dut ^ green_ref )) + begin if (stats1.errors_green == 0) stats1.errortime_green = $time; + stats1.errors_green = stats1.errors_green+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob001_continuous_input_sequence_detect_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob001_continuous_input_sequence_detect_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..882093fe152fb30bac1a6d297d4462fac1ba8961 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob001_continuous_input_sequence_detect_prompt.txt @@ -0,0 +1,55 @@ +I would like you to implement a module named `TopModule` with the following interface. All input and output ports are one bit unless otherwise specified. + +- **Input ports**: + - `clk` + - `rst_n` + - `a` + +- **Output ports**: + - `match` + +The module should implement an 8-bit shift register that shifts in the input `a` on every positive edge of the clock. The shift register is named `a_tem` and retains the 8 most recent bits of `a`. The module should perform the following functionality: + +1. **Reset Behavior**: + - When `rst_n` is de-asserted (logic 0), the shift register `a_tem` should be cleared to `8'b0`, and the output `match` should be set to `0`. + +2. **Shift Register Behavior**: + - On every positive edge of the clock, if `rst_n` is asserted (logic 1), the input `a` should be shifted into the least significant bit (LSB) of the shift register `a_tem`, and the previous bits should shift left by one position. + +3. **Matching Logic**: + - The output `match` should be asserted (logic 1) one clock cycle after the value of the shift register `a_tem` matches the specific 8-bit pattern `8'b0111_0001`. + - If the shift register does not match this pattern, the output `match` should remain de-asserted (logic 0). + +The design must operate synchronously with the clock and use a negative edge active reset (`rst_n`). Ensure the module correctly implements this functionality. + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input a, + output reg match + ); + + reg [7:0] a_tem; + + always @(*) begin + if (!rst_n) + match = 1'b0; + else if (a_tem == 8'b0111_0001) + match = 1'b1; + else + match = 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 8'b0; + end + else + begin + a_tem <= {a_tem[6:0],a}; + end +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob001_continuous_input_sequence_detect_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob001_continuous_input_sequence_detect_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..7f331ec29a6c80c9534b2e3dc96a5cc965d76324 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob001_continuous_input_sequence_detect_ref.sv @@ -0,0 +1,33 @@ +module RefModule( + input clk, + input rst_n, + input a, + output reg match + ); + + reg [7:0] a_tem; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match <= 1'b0; + end + else if (a_tem == 8'b0111_0001) + begin + match <= 1'b1; + end + else + begin + match <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 8'b0; + end + else + begin + a_tem <= {a_tem[6:0],a}; + end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob001_continuous_input_sequence_detect_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob001_continuous_input_sequence_detect_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..d2be5472b13f3a43ba2a478f58fccd93ef3d7257 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob001_continuous_input_sequence_detect_test.sv @@ -0,0 +1,165 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 +`default_nettype none + +module stimulus_gen ( + input wire clk, + output reg rst_n, + output reg a, + input wire tb_match, + input wire match_dut +); + bit failed = 0; + + always @(posedge clk, negedge clk) + if (!tb_match) + failed <= 1; + + initial begin + @(posedge clk); + failed <= 0; + rst_n <= 0; + a <= 0; + + // Initial reset + @(posedge clk); + rst_n <= 1; + + // Test Case 1: No match + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + @(posedge clk) a <= 1; + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + + // Test Case 2: Matching sequence (0111_0001) + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + @(posedge clk) a <= 1; + @(posedge clk) a <= 1; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + + // Test Case 3: Randomized input + repeat (100) @(posedge clk) begin + a <= $random; + end + + // Test Case 4: Edge cases with reset + @(posedge clk); + rst_n <= 0; + @(posedge clk); + rst_n <= 1; + repeat (8) @(posedge clk) a <= 1; + + // Add delay to observe outputs + repeat (100) @(posedge clk); + + #1 $finish; + end +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + + int clocks; + } stats; + + stats stats1; + + wire [511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk = 0; + initial forever #5 clk = ~clk; + + logic rst_n; + logic a; + logic match_ref; + logic match_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, a, match_ref, match_dut); + end + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + stimulus_gen stim1 ( + .clk(clk), + .rst_n(rst_n), + .a(a), + .tb_match(tb_match), + .match_dut(match_dut) + ); + + RefModule good1 ( + .clk(clk), + .rst_n(rst_n), + .a(a), + .match(match_ref) + ); + + TopModule top_module1 ( + .clk(clk), + .rst_n(rst_n), + .a(a), + .match(match_dut) + ); + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat (5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + final begin + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( + {match_ref} === + ({match_ref} ^ {match_dut} ^ {match_ref}) + ); + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (match_ref !== (match_ref ^ match_dut ^ match_ref)) begin + if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match++; + end + end + + // Add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob002_extraneous_items_sequence_detect_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob002_extraneous_items_sequence_detect_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..dd004ef3271990a0602c26166b64469ab355607a --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob002_extraneous_items_sequence_detect_prompt.txt @@ -0,0 +1,83 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input a + - output match + +The module should implement a sequence detection circuit that detects the +pattern "011XXX110" in the input data stream. The pattern consists of 9 bits +where the first 3 bits are "011", the last 3 bits are "110", and the middle +3 bits can be any value (don't care). + +The sequence detection should work as follows: +- Monitor the input data stream continuously +- Detect when the pattern "011XXX110" appears in the input +- Output match signal should be high when the pattern is detected +- Output match signal should be low at all other times + +Implementation approach: +The module should use a 9-bit shift register to store the last 9 input bits. +The match signal should be asserted when: +- The first 3 bits (bits 8:6) equal "011" AND +- The last 3 bits (bits 2:0) equal "110" +- The middle 3 bits (bits 5:3) can be any value + +### Timing and Behavior: +- When rst_n is low, the module should reset to its initial state +- On every positive clock edge (clk), the module should shift in the new input bit +- The output match is updated sequentially +- The detection is continuous - the module checks for the pattern on every clock cycle + + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input a, + output match + ); + + reg [8:0] a_tem; + reg match_f; + reg match_b; + + always @(*) begin + if (!rst_n) + match_f = 1'b0; + else if (a_tem[8:6] == 3'b011) + match_f = 1'b1; + else + match_f = 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match_b <= 1'b0; + end + else if (a_tem[2:0] == 3'b110) + begin + match_b <= 1'b1; + end + else + begin + match_b <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 9'b0; + end + else + begin + a_tem <= {a_tem[7:0],a}; + end + + assign match = match_b && match_f; +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob002_extraneous_items_sequence_detect_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob002_extraneous_items_sequence_detect_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..9aa18d6688f374e6ffa77394980b17c15b7107df --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob002_extraneous_items_sequence_detect_ref.sv @@ -0,0 +1,51 @@ +module RefModule( + input clk, + input rst_n, + input a, + output match + ); + + reg [8:0] a_tem; + reg match_f; + reg match_b; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match_f <= 1'b0; + end + else if (a_tem[8:6] == 3'b011) + begin + match_f <= 1'b1; + end + else + begin + match_f <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match_b <= 1'b0; + end + else if (a_tem[2:0] == 3'b110) + begin + match_b <= 1'b1; + end + else + begin + match_b <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 9'b0; + end + else + begin + a_tem <= {a_tem[7:0],a}; + end + + assign match = match_b && match_f; +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob002_extraneous_items_sequence_detect_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob002_extraneous_items_sequence_detect_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..16a887b4b3fea8b79c13dcb153e9429f761f381e --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob002_extraneous_items_sequence_detect_test.sv @@ -0,0 +1,218 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic a, + output logic rst_n, + output reg[511:0] wavedrom_title, + output reg wavedrom_enable, + input tb_match + // === End your code here === +); + // === Start your code here === + reg reset; + assign rst_n = ~reset; + + task wavedrom_start(input[511:0] title = ""); + endtask + + task wavedrom_stop; + #1; + endtask + + initial begin + reset = 1; + a = 0; + @(posedge clk); + reset = 0; + + wavedrom_start("Sequence Detection Test"); + + // Test specific sequences for "011XXX110" pattern + @(posedge clk) a = 0; // Start of 011101110 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 1; // Middle bit 1 + @(posedge clk) a = 0; // Middle bit 0 + @(posedge clk) a = 1; // Middle bit 1 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 0; // Should detect pattern here + + @(posedge clk) a = 0; // Start of 011001110 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 0; // Middle bit 0 + @(posedge clk) a = 0; // Middle bit 0 + @(posedge clk) a = 1; // Middle bit 1 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 0; // Should detect pattern here + + wavedrom_stop(); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + a <= $random; + end + + // Additional checks: "When rst_n is low, the module should reset to its initial state". + // A mid-run reset must clear the 9-bit history, so bits shifted in before the reset cannot + // complete a match afterwards. Inputs change at negedge; reset is asserted and released + // between edges while match is low; the first bits after reset are never "11", so reading + // the cleared history as zeros or as "no data yet" gives the same output. + repeat(12) @(negedge clk) a <= 0; // flush: history all zeros, match low + @(negedge clk) a <= 0; // pre-reset bits 0,1,1,1,0,1 + @(negedge clk) a <= 1; + @(negedge clk) a <= 1; + @(negedge clk) a <= 1; + @(negedge clk) a <= 0; + @(negedge clk) a <= 1; + @(posedge clk); + #2 reset = 1; // assert reset between edges + @(negedge clk) a <= 0; + repeat(2) @(posedge clk); + #2 reset = 0; // release between edges + @(negedge clk) a <= 1; // post-reset bits 1,0,1,1,0: with the old + @(negedge clk) a <= 0; // history kept this reads 011101 + 110 + @(negedge clk) a <= 1; + @(negedge clk) a <= 1; + @(negedge clk) a <= 0; + repeat(12) @(negedge clk) a <= 0; + // second round: pre-reset 0,1,1, reset, then 1,0,1,1,1,0 + @(negedge clk) a <= 0; + @(negedge clk) a <= 1; + @(negedge clk) a <= 1; + @(posedge clk); + #2 reset = 1; + @(negedge clk) a <= 0; + repeat(3) @(posedge clk); + #2 reset = 0; + @(negedge clk) a <= 1; + @(negedge clk) a <= 0; + @(negedge clk) a <= 1; + @(negedge clk) a <= 1; + @(negedge clk) a <= 1; + @(negedge clk) a <= 0; + repeat(12) @(negedge clk) a <= 0; + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic a; + logic rst_n; + logic match_ref; + logic match_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, a, rst_n, match_ref, match_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .* , + .a, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .a, + .match(match_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .a, + .match(match_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { match_ref } === ( { match_ref } ^ { match_dut } ^ { match_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (match_ref !== ( match_ref ^ match_dut ^ match_ref )) + begin if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match = stats1.errors_match+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob003_non-overlapping_sequence_detect_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob003_non-overlapping_sequence_detect_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..135da73f64567017d79261fe5c74519d84be4953 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob003_non-overlapping_sequence_detect_prompt.txt @@ -0,0 +1,121 @@ +I would like you to implement a module named `TopModule` with the following interface. All input and output ports are one bit unless otherwise specified. + +### Inputs: +- `clk`: System clock signal. +- `rst_n`: Asynchronous reset signal, active low. +- `data`: Single-bit input signal, representing the data to be checked. + +### Outputs: +- `match`: A signal that outputs `1` when the input signal `data` matches the target sequence `011100`. It remains `0` at all other times. +- `not_match`: A signal that outputs `1` when the input signal `data` does not match the target sequence `011100`. It remains `0` at all other times. + +### Module Description: +The module must detect the sequence `011100` from the input signal `data`. Please use the state machine to solve this problem. The detection rules are as follows: +1. The sequence check begins with every sixth bit of input data, meaning the module processes the input in groups of six bits and does not overlap or reuse bits between groups. +2. If the first bit of a group does not match the sequence, the module skips the entire group and starts checking from the next group beginning at the seventh bit. +3. When the sequence `011100` is detected, the `match` output should assert (go high). +4. If the sequence is not detected in a group, the `not_match` output should assert (go high). + +### Output Logic: +The output signals `match` and `not_match` should be implemented as follows: +- Declare both outputs as `reg` type: `output reg match, output reg not_match` +- Use combinational logic with Wildcard Sensitivity List block to control the outputs +- The outputs are mutually exclusive - only one can be high at a time + +### Timing and Behavior: +- When `rst_n` is low, the module should reset to its initial state and begin sequence detection from the start. Both outputs are `0` while `rst_n` is low. +- On every positive clock edge (`clk`), the module should evaluate the input signal `data` and transition states accordingly. + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input data, + output reg match, + output reg not_match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1=4'd1, + s2=4'd2, + s3=4'd3, + s4=4'd4, + s5=4'd5, + s6=4'd6, + sf1=4'd7, + sf2=4'd8, + sf3=4'd9, + sf4=4'd10, + sf5=4'd11, + sf6=4'd12; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data) +begin + case(pstate) + idle: + if(data==0) + nstate=s1; //First bit matched + else + nstate=sf1; + s1: + nstate=data?s2:sf2; + s2: + nstate=data?s3:sf3; + s3: + nstate=data?s4:sf4; + s4: + nstate=data?sf5:s5; + s5: + nstate=data?sf6:s6; + s6: + nstate=data?sf1:s1; + sf1: + nstate=sf2; + sf2: + nstate=sf3; + sf3: + nstate=sf4; + sf4: + nstate=sf5; + sf5: + nstate=sf6; + sf6: + nstate=data?sf1:s1; + default: + nstate=idle; + endcase +end + +always @(posedge clk or negedge rst_n) begin + if (!rst_n) begin + match <= 1'b0; + not_match <= 1'b0; + end + else if (pstate == s6) begin + match <= 1'b1; + not_match <= 1'b0; + end + else if (pstate == sf6) begin + match <= 1'b0; + not_match <= 1'b1; + end + else begin + match <= 1'b0; + not_match <= 1'b0; + end +end + +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob003_non-overlapping_sequence_detect_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob003_non-overlapping_sequence_detect_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..10012372821357962ecd8de990a1bdcfe95a7ef1 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob003_non-overlapping_sequence_detect_ref.sv @@ -0,0 +1,95 @@ +module RefModule( + input clk, + input rst_n, + input data, + output reg match, + output reg not_match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1=4'd1, + s2=4'd2, + s3=4'd3, + s4=4'd4, + s5=4'd5, + s6=4'd6, + sf1=4'd7, + sf2=4'd8, + sf3=4'd9, + sf4=4'd10, + sf5=4'd11, + sf6=4'd12; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data) +begin + case(pstate) + idle: + if(data==0) + nstate=s1; //First bit matched + else + nstate=sf1; + s1: + nstate=data?s2:sf2; + s2: + nstate=data?s3:sf3; + s3: + nstate=data?s4:sf4; + s4: + nstate=data?sf5:s5; + s5: + nstate=data?sf6:s6; + s6: + nstate=data?sf1:s1; + sf1: + nstate=sf2; + sf2: + nstate=sf3; + sf3: + nstate=sf4; + sf4: + nstate=sf5; + sf5: + nstate=sf6; + sf6: + nstate=data?sf1:s1; + default: + nstate=idle; + endcase +end + +always @(pstate or data or rst_n) +begin + if(!rst_n==1) + begin + match=1'b0; + not_match = 1'b0; + end + else if(pstate==s6) + begin + match=1'b1; + not_match = 1'b0; + end + else if(pstate==sf6) + begin + match=1'b0; + not_match = 1'b1; + end + else + begin + match=1'b0; + not_match = 1'b0; + end +end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob003_non-overlapping_sequence_detect_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob003_non-overlapping_sequence_detect_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..f7a171a1efc9327b0131c9b4d8f328995dc37c28 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob003_non-overlapping_sequence_detect_test.sv @@ -0,0 +1,150 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + +module stimulus_gen ( + input wire clk, + output reg rst_n, + output reg data +); + + initial begin + // Initialize signals + rst_n = 1'b0; + data = 1'b0; + + // Apply reset + @(posedge clk); + rst_n = 1'b1; + @(posedge clk) + @(posedge clk) + + // Test sequence: Provide the sequence "011100" + @(posedge clk) data = 1'b0; // First bit + @(posedge clk) data = 1'b1; // Second bit + @(posedge clk) data = 1'b1; // Third bit + @(posedge clk) data = 1'b1; // Fourth bit + @(posedge clk) data = 1'b0; // Fifth bit + @(posedge clk) data = 1'b0; // Sixth bit + + // Additional test cases + repeat(500) @(negedge clk) begin + data <= $random; + end + + // End simulation after sufficient cycles + repeat(100) @(posedge clk); + #1 $finish; + end + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + int errors_not_match; + int errortime_not_match; + + int clocks; + } stats; + + stats stats1; + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk = 0; + initial forever + #5 clk = ~clk; + + logic rst_n; + logic data; + logic match_ref; + logic match_dut; + logic not_match_ref; + logic not_match_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data, match_ref, match_dut, not_match_ref, not_match_dut); + end + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // Instantiate stimulus generator + stimulus_gen stim1 ( + .clk(clk), + .rst_n(rst_n), + .data(data) + ); + + // Instantiate reference module + RefModule good1 ( + .clk(clk), + .rst_n(rst_n), + .data(data), + .match(match_ref), + .not_match(not_match_ref) + ); + + // Instantiate DUT (TopModule) + TopModule top_module1 ( + .clk(clk), + .rst_n(rst_n), + .data(data), + .match(match_dut), + .not_match(not_match_dut) + ); + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Delay until the end of the time step + @(strobe); + end + endtask + + final begin + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + if (stats1.errors_not_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "not_match", stats1.errors_not_match, stats1.errortime_not_match); + else $display("Hint: Output '%s' has no mismatches.", "not_match"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: Ensure outputs match the reference module + assign tb_match = ( { match_ref, not_match_ref } === ( { match_ref, not_match_ref } ^ { match_dut, not_match_dut } ^ { match_ref, not_match_ref } ) ); + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (match_ref !== ( match_ref ^ match_dut ^ match_ref )) begin + if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match++; + end + if (not_match_ref !== ( not_match_ref ^ not_match_dut ^ not_match_ref )) begin + if (stats1.errors_not_match == 0) stats1.errortime_not_match = $time; + stats1.errors_not_match++; + end + end + + // Add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob004_continuous_input_sequence_detect2_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob004_continuous_input_sequence_detect2_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..9d323179dfba8f6e890337f1e53e7740ca82bf99 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob004_continuous_input_sequence_detect2_prompt.txt @@ -0,0 +1,108 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input data + - input data_valid + - output match + +The module should implement a sequence detection circuit that detects the +sequence "0110" in the input data stream. The data_valid signal indicates +when the input data is valid and should be considered for sequence detection. +When data_valid is high, the current input data is valid and participates +in sequence detection. When data_valid is low, the current input data is +invalid and should be ignored (not considered for sequence detection). + +The sequence detection should work as follows: +- Use state machine and update it sequentially +- Monitor the input data stream when data_valid is high +- Detect when the sequence "0110" appears in the valid data +- Output match signal should be high when the sequence "0110" is detected +- Output match signal should be low at all other times + +Output Logic for match signal: +- When reset is active (rst_n = 0): match = 0 +- When reset is inactive (rst_n = 1) AND sequence is 0110: match = 1 +- When reset is inactive (rst_n = 1) AND sequence is NOT 0110: match = 0 + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input data, + input data_valid, + output reg match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1_d0=4'd1, + s2_d01=4'd2, + s3_d011=4'd3, + s4_d0110=4'd4; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data or data_valid) +begin + case(pstate) + idle: + if(data_valid && !data) + nstate=s1_d0; //First bit matched + else + nstate=idle; + s1_d0: + if (data_valid) + begin + if (data) nstate = s2_d01; //Data valid and is 1, first two bits 01 matched, next state is s2_d01 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s1_d0; //Data invalid, stay in s1_d0 + s2_d01: + if (data_valid) + begin + if (data) nstate = s3_d011; //Data valid and is 1, first three bits 011 matched, next state is s3_d011 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s2_d01; //Data invalid, stay in s2_d01 + s3_d011: + if (data_valid) + begin + if (!data) nstate = s4_d0110; //Data valid and is 0, first four bits 0110 matched, next state is s4_d0110 + else nstate = idle; //Data valid but is 1, no match, next state is idle + end + else nstate = s3_d011; //Data invalid, stay in s3_d011 + s4_d0110: + if (data_valid) + begin + if (!data) nstate = s1_d0; //Data valid and is 0, matches first bit 0 of target sequence, next state is s1_d0 + else nstate = idle; //Data valid but is 1, does not match target sequence, next state is idle + end + else nstate = idle; //Data invalid, next state is idle + default: + nstate=idle; + endcase +end + +always @(posedge clk or negedge rst_n) begin + if (!rst_n) + match <= 1'b0; + else if (pstate == s4_d0110) // Entering state s4_d0110 means all four bits matched, assert match signal high + match <= 1'b1; + else + match <= 1'b0; +end + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob004_continuous_input_sequence_detect2_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob004_continuous_input_sequence_detect2_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..de2de151907bcac48bce85248c54dd0ccded8d1f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob004_continuous_input_sequence_detect2_ref.sv @@ -0,0 +1,78 @@ + +module RefModule( + input clk, + input rst_n, + input data, + input data_valid, + output reg match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1_d0=4'd1, + s2_d01=4'd2, + s3_d011=4'd3, + s4_d0110=4'd4; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data or data_valid) +begin + case(pstate) + idle: + if(data_valid && !data) + nstate=s1_d0; //First bit matched + else + nstate=idle; + s1_d0: + if (data_valid) + begin + if (data) nstate = s2_d01; //Data valid and is 1, first two bits 01 matched, next state is s2_d01 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s1_d0; //Data invalid, stay in s1_d0 + s2_d01: + if (data_valid) + begin + if (data) nstate = s3_d011; //Data valid and is 1, first three bits 011 matched, next state is s3_d011 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s2_d01; //Data invalid, stay in s2_d01 + s3_d011: + if (data_valid) + begin + if (!data) nstate = s4_d0110; //Data valid and is 0, first four bits 0110 matched, next state is s4_d0110 + else nstate = idle; //Data valid but is 1, no match, next state is idle + end + else nstate = s3_d011; //Data invalid, stay in s3_d011 + s4_d0110: + if (data_valid) + begin + if (!data) nstate = s1_d0; //Data valid and is 0, matches first bit 0 of target sequence, next state is s1_d0 + else nstate = idle; //Data valid but is 1, does not match target sequence, next state is idle + end + else nstate = idle; //Data invalid, next state is idle + default: + nstate=idle; + endcase +end + +always @(pstate or rst_n) +begin + if(!rst_n==1) + match=1'b0; + else if(pstate==s4_d0110) //Entering state s4_d0110 means all four bits matched, assert match signal high + match=1'b1; + else + match=1'b0; +end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob004_continuous_input_sequence_detect2_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob004_continuous_input_sequence_detect2_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..82239fb98ff463683ce3e113ae82b524feb863f9 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob004_continuous_input_sequence_detect2_test.sv @@ -0,0 +1,163 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + output logic rst_n, + output logic data, + output logic data_valid +); + + initial begin + rst_n = 0; + data = 0; + data_valid = 0; + @(posedge clk); + @(posedge clk); + rst_n = 1; + @(posedge clk); + @(posedge clk); + + // Test specific sequence: 0110 + data_valid = 1; + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + data_valid = 0; + @(posedge clk); + @(posedge clk); + + // Test with data_valid low (should ignore data) + data_valid = 0; + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + @(posedge clk); + + // Test overlapping sequences + data_valid = 1; + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + @(posedge clk); + + // Random testing + repeat(500) @(negedge clk) begin + data_valid <= $random; + data <= $random; + end + + #1 $finish; + end + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + + int clocks; + } stats; + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + logic rst_n; + logic data; + logic data_valid; + logic match_ref; + logic match_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data, data_valid, match_ref, match_dut ); + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + stimulus_gen stim1 ( + .clk, + .* , + .rst_n, + .data, + .data_valid ); + RefModule good1 ( + .clk, + .rst_n, + .data, + .data_valid, + .match(match_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .data, + .data_valid, + .match(match_dut) ); + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { match_ref } === ( { match_ref } ^ { match_dut } ^ { match_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (match_ref !== ( match_ref ^ match_dut ^ match_ref )) + begin if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match = stats1.errors_match+1'b1; end + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob005_signal_generator_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob005_signal_generator_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..d58adad7d4a19e7ed0a32bc02bb0268ce19f34f8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob005_signal_generator_prompt.txt @@ -0,0 +1,74 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input wave_choice (2 bits) + - output wave (5 bits) + +The module should implement a signal generator that outputs different +waveforms based on the wave_choice input signal. The generator should +produce three different types of waveforms: + +When wave_choice = 0: Generate a square wave signal +When wave_choice = 1: Generate a sawtooth wave signal +When wave_choice = 2: Generate a triangular wave signal + +The waveform generation should work as follows: +- The wave output should be a 5-bit signal that cycles through different + values to create the desired waveform patterns +- Each waveform should complete one full cycle and then repeat +- The square wave should alternate between high and low values +- The sawtooth wave should increment from 0 to maximum value and then reset +- The triangular wave should increment from 0 to maximum, then decrement back to 0 + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input [1:0] wave_choice, + output reg [4:0]wave + ); + + reg [4:0] cnt; + reg flag; + + always @(*) begin + if (~rst_n) + cnt = 0; + else + cnt = (wave_choice != 0) ? 0 : + (cnt == 19) ? 0 : + cnt + 1; + end + + // Triangle wave mode, flag bit control + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + flag <= 0; + else + flag <= wave_choice!=2 ? 0: + wave ==1 ? 1: + wave ==19? 0: + flag; + end + + + // Update wave signal + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + wave <= 0; + else + case(wave_choice) + 0 : wave <= cnt == 9? 20 : + cnt ==19? 0 : + wave; + 1 : wave <= wave==20? 0 : wave+1; + 2 : wave <= flag==0 ? wave-1: wave+1; + default: wave <= 0; + endcase + end +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob005_signal_generator_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob005_signal_generator_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..3efcd081b82e458cda37fd0b80e77f6f74d848a4 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob005_signal_generator_ref.sv @@ -0,0 +1,48 @@ + +module RefModule( + input clk, + input rst_n, + input [1:0] wave_choice, + output reg [4:0]wave + ); + + reg [4:0] cnt; + reg flag; + + // Square wave mode, counter control + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= wave_choice!=0 ? 0: + cnt ==19? 0: + cnt + 1; + end + + // Triangle wave mode, flag bit control + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + flag <= 0; + else + flag <= wave_choice!=2 ? 0: + wave ==1 ? 1: + wave ==19? 0: + flag; + end + + + // Update wave signal + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + wave <= 0; + else + case(wave_choice) + 0 : wave <= cnt == 9? 20 : + cnt ==19? 0 : + wave; + 1 : wave <= wave==20? 0 : wave+1; + 2 : wave <= flag==0 ? wave-1: wave+1; + default: wave <= 0; + endcase + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob005_signal_generator_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob005_signal_generator_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..bd10db973c22a6fd3ee41da31fe465c604253b2b --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob005_signal_generator_test.sv @@ -0,0 +1,146 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [1:0] wave_choice + // === End your code here === +); + + + initial begin + rst_n = 0; + wave_choice = 0; + repeat(20) @(posedge clk); + rst_n = 1; + @(posedge clk); + // Test square wave (wave_choice = 0) + wave_choice = 0; + repeat(800) @(posedge clk); + + // Test sawtooth wave (wave_choice = 1) + wave_choice = 1; + repeat(800) @(posedge clk); + + // Test triangular wave (wave_choice = 2) + wave_choice = 2; + repeat(800) @(posedge clk); + + wave_choice = 0; + repeat(800) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_wave; + int errortime_wave; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [1:0] wave_choice; + logic [4:0] wave_ref; + logic [4:0] wave_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, wave_choice, wave_ref, wave_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .wave_choice ); + + RefModule good1 ( + .clk, + .rst_n, + .wave_choice(wave_choice), + .wave(wave_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .wave_choice, + .wave(wave_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_wave) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "wave", stats1.errors_wave, stats1.errortime_wave); + else $display("Hint: Output '%s' has no mismatches.", "wave"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { wave_ref } === ( { wave_ref } ^ { wave_dut } ^ { wave_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (wave_ref !== ( wave_ref ^ wave_dut ^ wave_ref )) + begin if (stats1.errors_wave == 0) stats1.errortime_wave = $time; + stats1.errors_wave = stats1.errors_wave+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob006_data_serial-to-parallel_circuit_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob006_data_serial-to-parallel_circuit_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..0547a007bdf7cd6e0955d6062077e21be747c965 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob006_data_serial-to-parallel_circuit_prompt.txt @@ -0,0 +1,89 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_a + - input data_a + - output ready_a + - output valid_b + - output data_b (6 bits) + +The module should implement a serial-to-parallel conversion circuit that +converts single-bit input data into 6-bit parallel output data. The circuit +uses different handshake protocols for input and output interfaces. + +The serial-to-parallel conversion should work as follows: +- Input interface: Uses valid-ready handshake mechanism + - valid_a indicates the validity of input data_a + - ready_a indicates the module is always ready to receive upstream data +- Output interface: Uses valid-only handshake mechanism + - valid_b indicates the validity of output data_b + - data_b is the 6-bit parallel output data + +Data conversion process: +- The module receives single-bit input data_a when valid_a is high +- After receiving 6 input data bits, the module outputs the concatenated + 6-bit data_b with valid_b asserted +- valid_b is high in each cycle after one in which 5 bits of the current + group had been received, even before the 6th bit arrives; data_b then still + holds the previous group +- The first received data bit should be placed in the LSB (bit 0) of data_b +- The last received data bit should be placed in the MSB (bit 5) of data_b +- ready_a is always high indicates the module is always ready to receive input data + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input valid_a , + input data_a , + + output reg ready_a , + output reg valid_b , + output reg [5:0] data_b +); + reg [5:0] data_r; + reg [2:0] cnt; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~ready_a||~valid_a? cnt: + cnt == 5? 0 : + cnt+1; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_r <= 6'b0; + else + data_r <= ready_a&&valid_a? {data_a, data_r[5:1]}: data_r; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_b <= 6'b0; + else + data_b <= cnt==5&&valid_a? {data_a, data_r[5:1]}: data_b; + end + + always @(*) begin + if (~rst_n) + valid_b = 0; + else + valid_b = (cnt == 5); + end + + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + ready_a <= 0; + else + ready_a <= 1; + end +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob006_data_serial-to-parallel_circuit_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob006_data_serial-to-parallel_circuit_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..280616e82b10504600dea0c2e26db481db5b0642 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob006_data_serial-to-parallel_circuit_ref.sv @@ -0,0 +1,50 @@ +module RefModule( + input clk , + input rst_n , + input valid_a , + input data_a , + + output reg ready_a , + output reg valid_b , + output reg [5:0] data_b +); + reg [5:0] data_r; + reg [2:0] cnt; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~ready_a||~valid_a? cnt: + cnt == 5? 0 : + cnt+1; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_r <= 6'b0; + else + data_r <= ready_a&&valid_a? {data_a, data_r[5:1]}: data_r; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_b <= 6'b0; + else + data_b <= cnt==5&&valid_a? {data_a, data_r[5:1]}: data_b; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_b <= 0; + else + valid_b <= cnt==5; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + ready_a <= 0; + else + ready_a <= 1; + end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob006_data_serial-to-parallel_circuit_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob006_data_serial-to-parallel_circuit_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..80960f300c161a5c1c4aa9165937c0313ed072a0 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob006_data_serial-to-parallel_circuit_test.sv @@ -0,0 +1,202 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_a, + output logic data_a + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_a = 0; + data_a = 0; + repeat(4)@(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test serial data input: 6 bits per group + // First group: 101010 + valid_a = 1; + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + + // Second group: 110011 + data_a = 1; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 1; @(posedge clk); + + // Test with valid_a low (should ignore data) + valid_a = 0; + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 1; @(posedge clk); + + // Third group: 111000 + valid_a = 1; + data_a = 1; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 1; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 0; @(posedge clk); + data_a = 0; @(posedge clk); + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + valid_a <= $random; + data_a <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_ready_a; + int errortime_ready_a; + int errors_valid_b; + int errortime_valid_b; + int errors_data_b; + int errortime_data_b; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_a; + logic data_a; + logic ready_a_ref; + logic ready_a_dut; + logic valid_b_ref; + logic valid_b_dut; + logic [5:0] data_b_ref; + logic [5:0] data_b_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_a, data_a, ready_a_ref, ready_a_dut, valid_b_ref, valid_b_dut, data_b_ref, data_b_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_a, + .data_a ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_a, + .data_a, + .ready_a(ready_a_ref), + .valid_b(valid_b_ref), + .data_b(data_b_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_a, + .data_a, + .ready_a(ready_a_dut), + .valid_b(valid_b_dut), + .data_b(data_b_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_ready_a) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "ready_a", stats1.errors_ready_a, stats1.errortime_ready_a); + else $display("Hint: Output '%s' has no mismatches.", "ready_a"); + if (stats1.errors_valid_b) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_b", stats1.errors_valid_b, stats1.errortime_valid_b); + else $display("Hint: Output '%s' has no mismatches.", "valid_b"); + if (stats1.errors_data_b) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_b", stats1.errors_data_b, stats1.errortime_data_b); + else $display("Hint: Output '%s' has no mismatches.", "data_b"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { ready_a_ref, valid_b_ref, data_b_ref } === ( { ready_a_ref, valid_b_ref, data_b_ref } ^ { ready_a_dut, valid_b_dut, data_b_dut } ^ { ready_a_ref, valid_b_ref, data_b_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (ready_a_ref !== ( ready_a_ref ^ ready_a_dut ^ ready_a_ref )) + begin if (stats1.errors_ready_a == 0) stats1.errortime_ready_a = $time; + stats1.errors_ready_a = stats1.errors_ready_a+1'b1; end + if (valid_b_ref !== ( valid_b_ref ^ valid_b_dut ^ valid_b_ref )) + begin if (stats1.errors_valid_b == 0) stats1.errortime_valid_b = $time; + stats1.errors_valid_b = stats1.errors_valid_b+1'b1; end + if (data_b_ref !== ( data_b_ref ^ data_b_dut ^ data_b_ref )) + begin if (stats1.errors_data_b == 0) stats1.errortime_data_b = $time; + stats1.errors_data_b = stats1.errors_data_b+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob007_data_accumulation_output_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob007_data_accumulation_output_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..d52071f9096b412a2cca9c16f07699b3b40c754f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob007_data_accumulation_output_prompt.txt @@ -0,0 +1,82 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input data_in (8 bits) + - input valid_a + - input ready_b + - output ready_a + - output valid_b + - output data_out (10 bits) + +The module should implement a serial data accumulation circuit that receives +8-bit input data and outputs the accumulated sum of every 4 input values. +The circuit uses valid-ready handshake mechanisms for both input and output +interfaces to ensure efficient data transfer without performance loss. + +The data accumulation should work as follows: +- Input interface: Uses valid-ready handshake mechanism + - valid_a indicates the validity of input data_in + - ready_a indicates the module is ready to receive upstream data +- Output interface: Uses valid-ready handshake mechanism + - valid_b indicates the validity of output data_out + - ready_b indicates downstream is ready to receive data + +Data accumulation process: +- The module receives 8-bit input data_in when valid_a is high and ready_a is high +- After receiving 4 input data values, the module outputs the accumulated sum + as 10-bit data_out with valid_b asserted +- The output data_out represents the sum of the 4 most recently received inputs +- Both input and output interfaces must support backpressure through ready signals +- The design must ensure no performance loss when both upstream and downstream + are ready for full-speed transmission (no bubbles in data flow) + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input [7:0] data_in , + input valid_a , + input ready_b , + + output ready_a , + output reg valid_b , + output reg [9:0] data_out +); +reg [1:0] data_cnt; + +assign ready_a = !valid_b | ready_b; + +always @(*) begin + if (!rst_n) + data_cnt = 2'd0; + else if (valid_a && ready_a) + data_cnt = (data_cnt == 2'd3) ? 2'd0 : (data_cnt + 1'd1); + else + data_cnt = data_cnt; // hold value +end + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_b <= 'd0; + else if(data_cnt == 2'd3 && valid_a && ready_a) + valid_b <= 1'd1; + else if(valid_b && ready_b) + valid_b <= 1'd0; +end + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(ready_b && valid_a && ready_a && (data_cnt == 2'd0)) + data_out <= data_in; + else if(valid_a && ready_a) + data_out <= data_out + data_in; + +end + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob007_data_accumulation_output_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob007_data_accumulation_output_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..93c5651738cceb1a0b7892457af7e03a21f71db8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob007_data_accumulation_output_ref.sv @@ -0,0 +1,43 @@ + +module RefModule( + input clk , + input rst_n , + input [7:0] data_in , + input valid_a , + input ready_b , + + output ready_a , + output reg valid_b , + output reg [9:0] data_out +); +reg [1:0] data_cnt; + +assign ready_a = !valid_b | ready_b; + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_cnt <= 'd0; + else if(valid_a && ready_a) + data_cnt <= (data_cnt == 2'd3) ? 'd0 : (data_cnt + 1'd1); +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_b <= 'd0; + else if(data_cnt == 2'd3 && valid_a && ready_a) + valid_b <= 1'd1; + else if(valid_b && ready_b) + valid_b <= 1'd0; +end + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(ready_b && valid_a && ready_a && (data_cnt == 2'd0)) + data_out <= data_in; + else if(valid_a && ready_a) + data_out <= data_out + data_in; + +end + + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob007_data_accumulation_output_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob007_data_accumulation_output_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..99cd1ac83490a6b3f47e9a6f26e024d4a192ddd4 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob007_data_accumulation_output_test.sv @@ -0,0 +1,209 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [7:0] data_in, + output logic valid_a, + output logic ready_b + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + data_in = 0; + valid_a = 0; + ready_b = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test first group of 4 values: 1, 2, 3, 4 (sum = 10) + valid_a <= 1; + data_in <= 8'd1; @(posedge clk); + data_in <= 8'd2; @(posedge clk); + data_in <= 8'd3; @(posedge clk); + data_in <= 8'd4; @(posedge clk); + + data_in <= 8'd5; @(posedge clk); + @(posedge clk); + @(posedge clk); + ready_b <= 1; + + data_in <= 8'd6; @(posedge clk); + data_in <= 8'd7; @(posedge clk); + data_in <= 8'd8; @(posedge clk); + + // Test with valid_a low (should ignore data) + valid_a <= 0; + data_in <= 8'd10; @(posedge clk); + data_in <= 8'd20; @(posedge clk); + + // Test with ready_b low (backpressure) + valid_a <= 1; + ready_b <= 0; + data_in <= 8'd1; @(posedge clk); + data_in <= 8'd2; @(posedge clk); + + // Test third group: 10, 20, 30, 40 (sum = 100) + ready_b <= 1; + data_in <= 8'd10; @(posedge clk); + data_in <= 8'd20; @(posedge clk); + data_in <= 8'd30; @(posedge clk); + data_in <= 8'd40; @(posedge clk); + + // Random testing + repeat(300) @(posedge clk or negedge clk) begin + valid_a <= $random; + ready_b <= $random; + data_in <= $random % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_ready_a; + int errortime_ready_a; + int errors_valid_b; + int errortime_valid_b; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [7:0] data_in; + logic valid_a; + logic ready_b; + logic ready_a_ref; + logic ready_a_dut; + logic valid_b_ref; + logic valid_b_dut; + logic [9:0] data_out_ref; + logic [9:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data_in, valid_a, ready_b, ready_a_ref, ready_a_dut, valid_b_ref, valid_b_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .data_in, + .valid_a, + .ready_b ); + + RefModule good1 ( + .clk, + .rst_n, + .data_in, + .valid_a, + .ready_b, + .ready_a(ready_a_ref), + .valid_b(valid_b_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .data_in, + .valid_a, + .ready_b, + .ready_a(ready_a_dut), + .valid_b(valid_b_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_ready_a) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "ready_a", stats1.errors_ready_a, stats1.errortime_ready_a); + else $display("Hint: Output '%s' has no mismatches.", "ready_a"); + if (stats1.errors_valid_b) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_b", stats1.errors_valid_b, stats1.errortime_valid_b); + else $display("Hint: Output '%s' has no mismatches.", "valid_b"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { ready_a_ref, valid_b_ref, data_out_ref } === ( { ready_a_ref, valid_b_ref, data_out_ref } ^ { ready_a_dut, valid_b_dut, data_out_dut } ^ { ready_a_ref, valid_b_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (ready_a_ref !== ( ready_a_ref ^ ready_a_dut ^ ready_a_ref )) + begin if (stats1.errors_ready_a == 0) stats1.errortime_ready_a = $time; + stats1.errors_ready_a = stats1.errors_ready_a+1'b1; end + if (valid_b_ref !== ( valid_b_ref ^ valid_b_dut ^ valid_b_ref )) + begin if (stats1.errors_valid_b == 0) stats1.errortime_valid_b = $time; + stats1.errors_valid_b = stats1.errors_valid_b+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob008_non-integer_data_width_conversion_24to128_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob008_non-integer_data_width_conversion_24to128_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..9f56716a399dc42f34ea0b997340ed507fe94862 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob008_non-integer_data_width_conversion_24to128_prompt.txt @@ -0,0 +1,79 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_in + - input data_in (24 bits) + - output reg valid_out + - output reg data_out (128 bits) + +The module should implement a data width conversion circuit that converts +24-bit input data into 128-bit output data. The circuit uses valid-only +handshake mechanism for both input and output interfaces. + +The data width conversion should work as follows: +- Input interface: Uses valid-only handshake mechanism + - valid_in indicates the validity of input data_in +- Output interface: Uses valid-only handshake mechanism + - valid_out indicates the validity of output data_out + +Data conversion process: +- The module receives 24-bit input data_in when valid_in is high +- The module accumulates multiple 24-bit inputs to form a 128-bit output +- The first received 24-bit data should be placed in the high bits of data_out +- The last received 24-bit data should be placed in the low bits of data_out +- After accumulating enough data to form a complete 128-bit word, the module + outputs data_out with valid_out asserted + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input valid_in , + input [23:0] data_in , + + output reg valid_out , + output reg [127:0] data_out +); + reg [3:0] cnt; + reg [127:0] data_lock; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~valid_in? cnt:cnt+1; + end + + always @(*) begin + if (~rst_n) + valid_out = 1'b0; + else + valid_out = ((cnt == 5) || (cnt == 10) || (cnt == 15)) && valid_in; + end + + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_lock <= 0; + else + data_lock <= valid_in? {data_lock[103:0], data_in}: data_lock; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else if(cnt==5) + data_out <= valid_in? {data_lock[119:0], data_in[23:16]}: data_out; + else if(cnt==10) + data_out <= valid_in? {data_lock[111:0], data_in[23: 8]}: data_out; + else if(cnt==15) + data_out <= valid_in? {data_lock[103:0], data_in[23: 0]}: data_out; + else + data_out <= data_out; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob008_non-integer_data_width_conversion_24to128_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob008_non-integer_data_width_conversion_24to128_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..1301d527e940e047fcc73d4de472ec628eac3f48 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob008_non-integer_data_width_conversion_24to128_ref.sv @@ -0,0 +1,47 @@ + +module RefModule( + input clk , + input rst_n , + input valid_in , + input [23:0] data_in , + + output reg valid_out , + output reg [127:0] data_out +); + reg [3:0] cnt; + reg [127:0] data_lock; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~valid_in? cnt:cnt+1; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_out <= 0; + else + valid_out <= (cnt==5 || cnt==10 || cnt==15)&&valid_in; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_lock <= 0; + else + data_lock <= valid_in? {data_lock[103:0], data_in}: data_lock; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else if(cnt==5) + data_out <= valid_in? {data_lock[119:0], data_in[23:16]}: data_out; + else if(cnt==10) + data_out <= valid_in? {data_lock[111:0], data_in[23: 8]}: data_out; + else if(cnt==15) + data_out <= valid_in? {data_lock[103:0], data_in[23: 0]}: data_out; + else + data_out <= data_out; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob008_non-integer_data_width_conversion_24to128_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob008_non-integer_data_width_conversion_24to128_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..7256496acfb63697009cadc94c0c275adccae24e --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob008_non-integer_data_width_conversion_24to128_test.sv @@ -0,0 +1,190 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_in, + output logic [23:0] data_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_in = 0; + data_in = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test first group: 6 x 24-bit inputs to form 128-bit output + // Inputs: 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666 + valid_in <= 1; + data_in <= 24'h111111; @(posedge clk); + data_in <= 24'h222222; @(posedge clk); + data_in <= 24'h333333; @(posedge clk); + data_in <= 24'h444444; @(posedge clk); + data_in <= 24'h555555; @(posedge clk); + data_in <= 24'h666666; @(posedge clk); + + // Test second group: 6 x 24-bit inputs + data_in <= 24'h777777; @(posedge clk); + data_in <= 24'h888888; @(posedge clk); + data_in <= 24'h999999; @(posedge clk); + data_in <= 24'haaaaaa; @(posedge clk); + data_in <= 24'hbbbbbb; @(posedge clk); + data_in <= 24'hcccccc; @(posedge clk); + + // Test with valid_in low (should ignore data) + valid_in <= 0; + data_in <= 24'hdddddd; @(posedge clk); + data_in <= 24'heeeeee; @(posedge clk); + + // Test third group: 6 x 24-bit inputs + valid_in <= 1; + data_in <= 24'hffffff; @(posedge clk); + data_in <= 24'h000000; @(posedge clk); + data_in <= 24'h123456; @(posedge clk); + data_in <= 24'h789abc; @(posedge clk); + data_in <= 24'hdef012; @(posedge clk); + data_in <= 24'h345678; @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + valid_in <= $random; + data_in <= $random % (1<<24); + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_valid_out; + int errortime_valid_out; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_in; + logic [23:0] data_in; + logic valid_out_ref; + logic valid_out_dut; + logic [127:0] data_out_ref; + logic [127:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_in, data_in, valid_out_ref, valid_out_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_in, + .data_in ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_valid_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_out", stats1.errors_valid_out, stats1.errortime_valid_out); + else $display("Hint: Output '%s' has no mismatches.", "valid_out"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { valid_out_ref, data_out_ref } === ( { valid_out_ref, data_out_ref } ^ { valid_out_dut, data_out_dut } ^ { valid_out_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (valid_out_ref !== ( valid_out_ref ^ valid_out_dut ^ valid_out_ref )) + begin if (stats1.errors_valid_out == 0) stats1.errortime_valid_out = $time; + stats1.errors_valid_out = stats1.errors_valid_out+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob009_non-integer_data_width_conversion_8to12_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob009_non-integer_data_width_conversion_8to12_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..56a6b4a680c55a2d2ccaa57370f3d44de1883d1f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob009_non-integer_data_width_conversion_8to12_prompt.txt @@ -0,0 +1,83 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_in + - input data_in (8 bits) + - output valid_out + - output data_out (12 bits) + +The module should implement a data width conversion circuit that converts +8-bit input data into 12-bit output data. The circuit uses valid-only +handshake mechanism for both input and output interfaces. + +The data width conversion should work as follows: +- Input interface: Uses valid-only handshake mechanism + - valid_in indicates the validity of input data_in +- Output interface: Uses valid-only handshake mechanism + - valid_out indicates the validity of output data_out + +Data conversion process: +- The module receives 8-bit input data_in when valid_in is high +- The module accumulates two 8-bit inputs to form a 12-bit output +- The first received 8-bit data should be placed in the high bits of data_out +- The second received 8-bit data should be placed in the low bits of data_out +- After receiving exactly 2 input data values, the module outputs data_out + with valid_out asserted in the next clock cycle +- If only one data input is received, the module waits for the second input + before producing any output + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [11:0] data_out +); +reg [7:0] data_lock; +reg [1:0] valid_cnt ; +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_lock <= 'd0; + else if(valid_in ) + data_lock <= data_in; +end +always @(*) begin + if (!rst_n) + valid_cnt = 2'd0; + else if (valid_in) begin + if (valid_cnt == 2'd2) + valid_cnt = 2'd0; + else + valid_cnt = valid_cnt + 1'd1; + end + else + valid_cnt = valid_cnt; // hold current value +end + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + valid_out <= 1'd1; + else if(valid_in && valid_cnt == 2'd2) + valid_out <= 1'd1; + else + valid_out <= 'd0; +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + data_out <= {data_lock, data_in[7:4]}; + else if(valid_in && valid_cnt == 2'd2) + data_out <= {data_lock[3:0], data_in}; +end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob009_non-integer_data_width_conversion_8to12_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob009_non-integer_data_width_conversion_8to12_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..67250e6ddee50ae7ab11c617b4762a2b2e50c1b5 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob009_non-integer_data_width_conversion_8to12_ref.sv @@ -0,0 +1,47 @@ + +module RefModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [11:0] data_out +); +reg [7:0] data_lock; +reg [1:0] valid_cnt ; +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_lock <= 'd0; + else if(valid_in ) + data_lock <= data_in; +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_cnt <= 'd0; + else if(valid_in)begin + if(valid_cnt == 2'd2) + valid_cnt <= 2'd0; + else + valid_cnt <= valid_cnt + 1'd1; + end +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + valid_out <= 1'd1; + else if(valid_in && valid_cnt == 2'd2) + valid_out <= 1'd1; + else + valid_out <= 'd0; +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + data_out <= {data_lock, data_in[7:4]}; + else if(valid_in && valid_cnt == 2'd2) + data_out <= {data_lock[3:0], data_in}; +end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob009_non-integer_data_width_conversion_8to12_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob009_non-integer_data_width_conversion_8to12_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..eb7feeb6a870af7d0f6ae7f10cf400cf0f05562d --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob009_non-integer_data_width_conversion_8to12_test.sv @@ -0,0 +1,181 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_in, + output logic [7:0] data_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_in = 0; + data_in = 0; + repeat(3) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test first pair: 0xAA, 0x55 (should form 0xAA5) + valid_in = 1; + data_in = 8'hAA; @(posedge clk); + data_in = 8'h55; @(posedge clk); + + // Test second pair: 0x12, 0x34 (should form 0x123) + data_in = 8'h12; @(posedge clk); + data_in = 8'h34; @(posedge clk); + + // Test with valid_in low (should ignore data) + valid_in = 0; + data_in = 8'hFF; @(posedge clk); + data_in = 8'hEE; @(posedge clk); + + // Test third pair: 0x78, 0x9A (should form 0x789) + valid_in = 1; + data_in = 8'h78; @(posedge clk); + data_in = 8'h9A; @(posedge clk); + + // Test fourth pair: 0xBC, 0xDE (should form 0xBCD) + data_in = 8'hBC; @(posedge clk); + data_in = 8'hDE; @(posedge clk); + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + valid_in <= $random; + data_in <= $random % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_valid_out; + int errortime_valid_out; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_in; + logic [7:0] data_in; + logic valid_out_ref; + logic valid_out_dut; + logic [11:0] data_out_ref; + logic [11:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_in, data_in, valid_out_ref, valid_out_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_in, + .data_in ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_valid_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_out", stats1.errors_valid_out, stats1.errortime_valid_out); + else $display("Hint: Output '%s' has no mismatches.", "valid_out"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { valid_out_ref, data_out_ref } === ( { valid_out_ref, data_out_ref } ^ { valid_out_dut, data_out_dut } ^ { valid_out_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (valid_out_ref !== ( valid_out_ref ^ valid_out_dut ^ valid_out_ref )) + begin if (stats1.errors_valid_out == 0) stats1.errortime_valid_out = $time; + stats1.errors_valid_out = stats1.errors_valid_out+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob010_integer_multiple_data_bit_width_conversion_8to16_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob010_integer_multiple_data_bit_width_conversion_8to16_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..d510e53bb7e0c0e13394bcaf0bdde4c860b340e7 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob010_integer_multiple_data_bit_width_conversion_8to16_prompt.txt @@ -0,0 +1,76 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_in + - input data_in (8 bits) + - output reg valid_out + - output reg data_out (16 bits) + +The module should implement a data width conversion circuit that converts +8-bit input data into 16-bit output data. The circuit uses valid-only +handshake mechanism for both input and output interfaces. + +The data width conversion should work as follows: +- Input interface: Uses valid-only handshake mechanism + - valid_in indicates the validity of input data_in +- Output interface: Uses valid-only handshake mechanism + - valid_out indicates the validity of output data_out + +Data conversion process: +- The module receives 8-bit input data_in when valid_in is high +- The module accumulates two 8-bit inputs to form a 16-bit output +- The first received 8-bit data should be placed in the high 8 bits of data_out +- The second received 8-bit data should be placed in the low 8 bits of data_out +- After receiving exactly 2 input data values, the module outputs data_out + with valid_out asserted in the next clock cycle +- If only one data input is received, the module waits for the second input + before producing any output + + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [15:0] data_out +); + reg [7:0] data_r; + reg flag; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + flag <= 0; + else + flag <= valid_in? ~flag: flag; + end + + always @(*) begin + if (~rst_n) + data_r = 0; + else + data_r = valid_in ? data_in : data_r; + end + + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else + data_out <= flag&&valid_in? {data_r, data_in}: data_out; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_out <= 0; + else + valid_out <= flag&&valid_in; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob010_integer_multiple_data_bit_width_conversion_8to16_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob010_integer_multiple_data_bit_width_conversion_8to16_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..871ba50d8fffed3bb0f14adbfb85fad0277d59a1 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob010_integer_multiple_data_bit_width_conversion_8to16_ref.sv @@ -0,0 +1,40 @@ +module RefModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [15:0] data_out +); + reg [7:0] data_r; + reg flag; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + flag <= 0; + else + flag <= valid_in? ~flag: flag; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_r <= 0; + else + data_r <= valid_in? data_in: data_r; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else + data_out <= flag&&valid_in? {data_r, data_in}: data_out; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_out <= 0; + else + valid_out <= flag&&valid_in; + end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob010_integer_multiple_data_bit_width_conversion_8to16_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob010_integer_multiple_data_bit_width_conversion_8to16_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..799c6e6a4757f8af250d91797e0be99a8d88a037 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob010_integer_multiple_data_bit_width_conversion_8to16_test.sv @@ -0,0 +1,181 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_in, + output logic [7:0] data_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_in = 0; + data_in = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test first pair: 0xAA, 0x55 (should form 0xAA55) + valid_in = 1; + data_in = 8'hAA; @(posedge clk); + data_in = 8'h55; @(posedge clk); + + // Test second pair: 0x12, 0x34 (should form 0x1234) + data_in = 8'h12; @(posedge clk); + data_in = 8'h34; @(posedge clk); + + // Test with valid_in low (should ignore data) + valid_in = 0; + data_in = 8'hFF; @(posedge clk); + data_in = 8'hEE; @(posedge clk); + + // Test third pair: 0x78, 0x9A (should form 0x789A) + valid_in = 1; + data_in = 8'h78; @(posedge clk); + data_in = 8'h9A; @(posedge clk); + + // Test fourth pair: 0xBC, 0xDE (should form 0xBCDE) + data_in = 8'hBC; @(posedge clk); + data_in = 8'hDE; @(posedge clk); + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + valid_in <= $random; + data_in <= $urandom % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_valid_out; + int errortime_valid_out; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_in; + logic [7:0] data_in; + logic valid_out_ref; + logic valid_out_dut; + logic [15:0] data_out_ref; + logic [15:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_in, data_in, valid_out_ref, valid_out_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_in, + .data_in ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_valid_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_out", stats1.errors_valid_out, stats1.errortime_valid_out); + else $display("Hint: Output '%s' has no mismatches.", "valid_out"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { valid_out_ref, data_out_ref } === ( { valid_out_ref, data_out_ref } ^ { valid_out_dut, data_out_dut } ^ { valid_out_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (valid_out_ref !== ( valid_out_ref ^ valid_out_dut ^ valid_out_ref )) + begin if (stats1.errors_valid_out == 0) stats1.errortime_valid_out = $time; + stats1.errors_valid_out = stats1.errors_valid_out+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob012_state_machine_overlapping_sequence_detection_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob012_state_machine_overlapping_sequence_detection_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..0e4127465ee36cb33903560a7dd01eafc949adb1 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob012_state_machine_overlapping_sequence_detection_prompt.txt @@ -0,0 +1,66 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst + - input data + - output reg flag + +The module should implement a state machine that detects the sequence "1011" +with overlapping detection capability. The state machine should detect +multiple occurrences of the sequence even when they overlap. reset is active low. + +The sequence detection should work as follows: +- Monitor the input data stream continuously +- Detect when the sequence "1011" appears in the input +- Support overlapping detection: "10110111" should be detected twice +- Output flag signal should be high for one clock cycle when the sequence + is detected, starting from the next clock cycle after detection, that is, + two cycles after the last bit of "1011" is on data + + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule( + input wire clk , + input wire rst , + input wire data , + output reg flag +); +//*************code***********// + parameter S0=0, S1=1, S2=2, S3=3, S4=4; + reg [2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= S0; + else + state <= nstate; + end + + always@(*) begin + if(~rst) + nstate <= S0; + else + case(state) + S0 : nstate <= data? S1: S0; + S1 : nstate <= data? S1: S2; + S2 : nstate <= data? S3: S0; + S3 : nstate <= data? S4: S2; + S4 : nstate <= data? S1: S2; + default: nstate <= S0; + endcase + end + + always @(*) begin + if (~rst) + flag = 1'b0; + else + flag = (state == S4); + end + + +//*************code***********// +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob012_state_machine_overlapping_sequence_detection_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob012_state_machine_overlapping_sequence_detection_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..9e6792cac0abf39247e8718dc9d59f9e638ff7b8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob012_state_machine_overlapping_sequence_detection_ref.sv @@ -0,0 +1,40 @@ +module RefModule( + input wire clk , + input wire rst , + input wire data , + output reg flag +); +//*************code***********// + parameter S0=0, S1=1, S2=2, S3=3, S4=4; + reg [2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= S0; + else + state <= nstate; + end + + always@(*) begin + if(~rst) + nstate <= S0; + else + case(state) + S0 : nstate <= data? S1: S0; + S1 : nstate <= data? S1: S2; + S2 : nstate <= data? S3: S0; + S3 : nstate <= data? S4: S2; + S4 : nstate <= data? S1: S2; + default: nstate <= S0; + endcase + end + + always@(posedge clk or negedge rst) begin + if(~rst) + flag <= 0; + else + flag <= state==S4; + end + +//*************code***********// +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob012_state_machine_overlapping_sequence_detection_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob012_state_machine_overlapping_sequence_detection_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..a837d96a98935a96d3a2d8d9b3e47d692a7e56ee --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob012_state_machine_overlapping_sequence_detection_test.sv @@ -0,0 +1,164 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst, + output logic data + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + data = 0; + repeat(10) @(posedge clk); + rst <= 1; + @(posedge clk); + + // Test sequence "1011" - should detect once + data <= 1; @(posedge clk); // First bit + data <= 0; @(posedge clk); // Second bit + data <= 1; @(posedge clk); // Third bit + data <= 1; @(posedge clk); // Fourth bit - should detect here + + // Test overlapping sequences "10110111" - should detect twice + data <= 0; @(posedge clk); // Fifth bit + data <= 1; @(posedge clk); // Sixth bit + data <= 1; @(posedge clk); // Seventh bit - should detect here + data <= 1; @(posedge clk); // Eighth bit + + // Test non-matching sequence + data <= 0; @(posedge clk); + data <= 0; @(posedge clk); + data <= 1; @(posedge clk); + data <= 0; @(posedge clk); + + // Test another "1011" sequence + data <= 1; @(posedge clk); + data <= 0; @(posedge clk); + data <= 1; @(posedge clk); + data <= 1; @(posedge clk); // Should detect here + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + data <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_flag; + int errortime_flag; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic data; + logic flag_ref; + logic flag_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, data, flag_ref, flag_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst, + .data ); + + RefModule good1 ( + .clk, + .rst, + .data, + .flag(flag_ref) ); + + TopModule top_module1 ( + .clk, + .rst, + .data, + .flag(flag_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_flag) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "flag", stats1.errors_flag, stats1.errortime_flag); + else $display("Hint: Output '%s' has no mismatches.", "flag"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { flag_ref } === ( { flag_ref } ^ { flag_dut } ^ { flag_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (flag_ref !== ( flag_ref ^ flag_dut ^ flag_ref )) + begin if (stats1.errors_flag == 0) stats1.errortime_flag = $time; + stats1.errors_flag = stats1.errors_flag+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob013_least_common_multiple_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob013_least_common_multiple_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..e32cba6d3af8df1cb4bbce46866d40a93ae43afd --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob013_least_common_multiple_prompt.txt @@ -0,0 +1,130 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input A (8 bits) + - input B (8 bits) + - input vld_in + - output lcm_out (16 bits) + - output mcd_out (8 bits) + - output vld_out + +Design a sequential circuit that takes two unsigned numbers as input and +outputs their least common multiple (LCM) and greatest common divisor (GCD). +The bit width can be determined by parameter DATA_W. For this implementation, +DATA_W is 8, so A, B, and mcd_out are 8 bits while lcm_out is 16 bits. + +The circuit should operate as follows: +- When vld_in is asserted, the input data A and B are valid and should be + processed +- The circuit computes both the LCM and GCD of the two input numbers +- When the computation is complete, vld_out should be asserted to indicate + that the output data is valid +- Before the output is valid, lcm_out should be driven to high impedance + ('hz). This is required in this design; otherwise there will be a problem +- The rst_n signal is an active-low reset signal + +Assume all sequential logic is triggered on the positive edge of the clock. + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule#( +parameter DATA_W = 8) +( +input [DATA_W-1:0] A, +input [DATA_W-1:0] B, +input vld_in, +input rst_n, +input clk, +output wire [DATA_W*2-1:0] lcm_out, +output wire [DATA_W-1:0] mcd_out, +output reg vld_out +); + reg [DATA_W*2-1:0] A_reg; + reg [DATA_W*2-1:0] B_reg; + reg [DATA_W*2-1:0] mcd_out_r1; + reg [DATA_W*2-1:0] mult_reg; + reg [1:0] c_state, n_state; + parameter IDLE = 'd0, lcm1 = 'd1, Finish = 'd2; + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) c_state <= IDLE; + else c_state <= n_state; + end + + always @(*) begin + case(c_state) + IDLE : if(vld_in) n_state = lcm1; + else n_state = IDLE; + lcm1 : if(A_reg == B_reg) n_state = Finish; + else n_state = lcm1; + Finish : n_state = IDLE; + default : n_state = IDLE; + endcase + end + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + else begin + case(c_state) + IDLE : + if(vld_in) begin + A_reg <= A; + B_reg <= B; + mult_reg <= A * B; + mcd_out_r1 <= 0; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + mult_reg <= mult_reg; + mcd_out_r1 <= 0; + end + lcm1 : + if(A_reg > B_reg) begin + A_reg <= A_reg - B_reg; + B_reg <= B_reg; + end + else if(A_reg < B_reg) begin + B_reg <= B_reg - A_reg; + A_reg <= A_reg; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + end + Finish : mcd_out_r1 <= A_reg; + + default : begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + endcase + end + end + always @(posedge clk or negedge rst_n) begin + if (!rst_n) + mcd_out <= 'd0; + else + mcd_out <= mcd_out_r1; + end + + assign lcm_out = (mcd_out_r1 == 'd0) ? 'hz : (mult_reg / mcd_out_r1); // If the GCD register is 0, output high impedance, otherwise the circuit will have problems + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) vld_out <= 1'b0; + else if(c_state == Finish) vld_out <= 1'b1; + else vld_out <= 1'b0; + end + +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob013_least_common_multiple_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob013_least_common_multiple_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..c6c378d54ca460b83b669849fc4f89e7a2d7b746 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob013_least_common_multiple_ref.sv @@ -0,0 +1,92 @@ + +module RefModule#( +parameter DATA_W = 8) +( +input [DATA_W-1:0] A, +input [DATA_W-1:0] B, +input vld_in, +input rst_n, +input clk, +output wire [DATA_W*2-1:0] lcm_out, +output wire [DATA_W-1:0] mcd_out, +output reg vld_out +); + reg [DATA_W*2-1:0] A_reg; + reg [DATA_W*2-1:0] B_reg; + reg [DATA_W*2-1:0] mcd_out_r1; + reg [DATA_W*2-1:0] mult_reg; + reg [1:0] c_state, n_state; + parameter IDLE = 'd0, lcm1 = 'd1, Finish = 'd2; + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) c_state <= IDLE; + else c_state <= n_state; + end + + always @(*) begin + case(c_state) + IDLE : if(vld_in) n_state = lcm1; + else n_state = IDLE; + lcm1 : if(A_reg == B_reg) n_state = Finish; + else n_state = lcm1; + Finish : n_state = IDLE; + default : n_state = IDLE; + endcase + end + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + else begin + case(c_state) + IDLE : + if(vld_in) begin + A_reg <= A; + B_reg <= B; + mult_reg <= A * B; + mcd_out_r1 <= 0; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + mult_reg <= mult_reg; + mcd_out_r1 <= 0; + end + lcm1 : + if(A_reg > B_reg) begin + A_reg <= A_reg - B_reg; + B_reg <= B_reg; + end + else if(A_reg < B_reg) begin + B_reg <= B_reg - A_reg; + A_reg <= A_reg; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + end + Finish : mcd_out_r1 <= A_reg; + + default : begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + endcase + end + end + assign mcd_out = mcd_out_r1; + assign lcm_out = (mcd_out_r1 == 'd0) ? 'hz : (mult_reg / mcd_out_r1); // If the GCD register is 0, output high impedance, otherwise the circuit will have problems + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) vld_out <= 1'b0; + else if(c_state == Finish) vld_out <= 1'b1; + else vld_out <= 1'b0; + end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob013_least_common_multiple_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob013_least_common_multiple_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..ca87e1e8e7186a4ecb91b6bfe6e51215fbae87f6 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob013_least_common_multiple_test.sv @@ -0,0 +1,237 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [7:0] A, + output logic [7:0] B, + output logic vld_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n <= 0; + A <= 0; + B <= 0; + vld_in <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic cases + A <= 8'd12; + B <= 8'd8; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); // Wait for computation + + A <= 8'd15; + B <= 8'd25; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd7; + B <= 8'd11; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd16; + B <= 8'd24; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + + // Test edge cases + A <= 8'd1; + B <= 8'd1; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd255; + B <= 8'd1; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd0; + B <= 8'd5; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + // Random test sequence + repeat(50) @(posedge clk) begin + A <= $random & 255; // 8-bit random + B <= $random & 255; // 8-bit random + vld_in <= $random & 1; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_lcm_out; + int errortime_lcm_out; + int errors_mcd_out; + int errortime_mcd_out; + int errors_vld_out; + int errortime_vld_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [7:0] A; + logic [7:0] B; + logic vld_in; + logic [15:0] lcm_out_ref; + logic [15:0] lcm_out_dut; + logic [7:0] mcd_out_ref; + logic [7:0] mcd_out_dut; + logic vld_out_ref; + logic vld_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, A, B, vld_in, lcm_out_ref, lcm_out_dut, mcd_out_ref, mcd_out_dut, vld_out_ref, vld_out_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .A, + .B, + .vld_in + ); + + RefModule good1 ( + .A, + .B, + .vld_in, + .rst_n, + .clk, + .lcm_out(lcm_out_ref), + .mcd_out(mcd_out_ref), + .vld_out(vld_out_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .A, + .B, + .vld_in, + .lcm_out(lcm_out_dut), + .mcd_out(mcd_out_dut), + .vld_out(vld_out_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_lcm_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "lcm_out", stats1.errors_lcm_out, stats1.errortime_lcm_out); + else $display("Hint: Output '%s' has no mismatches.", "lcm_out"); + if (stats1.errors_mcd_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "mcd_out", stats1.errors_mcd_out, stats1.errortime_mcd_out); + else $display("Hint: Output '%s' has no mismatches.", "mcd_out"); + if (stats1.errors_vld_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "vld_out", stats1.errors_vld_out, stats1.errortime_vld_out); + else $display("Hint: Output '%s' has no mismatches.", "vld_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + // Where the reference drives lcm_out='hz (the spec requires 'hz while the output is not valid) the DUT must drive + // 'hz too; the X-only tolerance below would turn a 'z reference into 'x and reject every design. + wire lcm_ok = (lcm_out_ref === 16'bz) ? (lcm_out_dut === 16'bz) : (lcm_out_ref === ( lcm_out_ref ^ lcm_out_dut ^ lcm_out_ref )); + assign tb_match = lcm_ok && ( { mcd_out_ref, vld_out_ref } === ( { mcd_out_ref, vld_out_ref } ^ { mcd_out_dut, vld_out_dut } ^ { mcd_out_ref, vld_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (!lcm_ok) + begin if (stats1.errors_lcm_out == 0) stats1.errortime_lcm_out = $time; + stats1.errors_lcm_out = stats1.errors_lcm_out+1'b1; end + if (mcd_out_ref !== ( mcd_out_ref ^ mcd_out_dut ^ mcd_out_ref )) + begin if (stats1.errors_mcd_out == 0) stats1.errortime_mcd_out = $time; + stats1.errors_mcd_out = stats1.errors_mcd_out+1'b1; end + if (vld_out_ref !== ( vld_out_ref ^ vld_out_dut ^ vld_out_ref )) + begin if (stats1.errors_vld_out == 0) stats1.errortime_vld_out = $time; + stats1.errors_vld_out = stats1.errors_vld_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob014_sequence_generator_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob014_sequence_generator_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..579aaaa07b9a6a80b576e48644dc0ce8f8f5b987 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob014_sequence_generator_prompt.txt @@ -0,0 +1,47 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output data + +Implement a module that continuously outputs the sequence 001011 in a +cyclic manner. + +The circuit should generate the pattern as follows: +- Output '0', then '0', then '1', then '0', then '1', then '1' +- After outputting the last '1', the sequence repeats from the beginning +- The sequence 001011 repeats indefinitely + +The rst_n signal is an active-low asynchronous reset signal; data is 0 while rst_n is low. When reset is asserted, +the sequence should restart from the beginning. Assume all sequential +logic is triggered on the positive edge of the clock. After rst_n is released, +the first sequence element appears on data at the first rising clock edge. + + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + output reg data + ); + + reg [5:0] q; + + always @(*) begin + if (!rst_n) + q = 6'b001011; + else + q = {q[4:0], q[5]}; + end + + always @(*) begin + if (!rst_n) + data = 1'b0; + else + data = q[5]; + end +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob014_sequence_generator_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob014_sequence_generator_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..139d5513975bbb32f359727bfb417bd3d7338b02 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob014_sequence_generator_ref.sv @@ -0,0 +1,21 @@ + +module RefModule( + input clk, + input rst_n, + output reg data + ); + + reg [5:0] q; + + always@(posedge clk or negedge rst_n) + if (!rst_n) + q <= 6'b001011; + else + q <= {q[4:0],q[5]}; + + always@(posedge clk or negedge rst_n) + if (!rst_n) + data <= 1'd0; + else + data <= q[5]; +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob014_sequence_generator_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob014_sequence_generator_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..d674f42565304d0f2bcf1950261451fbe1fe02a7 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob014_sequence_generator_test.sv @@ -0,0 +1,139 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Let it run for multiple cycles + repeat(200) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_data; + int errortime_data; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic data_ref; + logic data_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data_ref, data_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n + ); + + RefModule good1 ( + .clk, + .rst_n, + .data(data_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .data(data_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_data) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data", stats1.errors_data, stats1.errortime_data); + else $display("Hint: Output '%s' has no mismatches.", "data"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { data_ref } === ( { data_ref } ^ { data_dut } ^ { data_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (data_ref !== ( data_ref ^ data_dut ^ data_ref )) + begin if (stats1.errors_data == 0) stats1.errortime_data = $time; + stats1.errors_data = stats1.errors_data+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob015_vending_machine2_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob015_vending_machine2_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..09c16b9ef54e6009d7aedbe5915ef75b78fadbbf --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob015_vending_machine2_prompt.txt @@ -0,0 +1,103 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst + - input d1 + - input d2 + - input sel + - output reg out1 + - output reg out2 + - output reg out3 + +The module should implement a vending machine that accepts two types of +currency (0.5 yuan and 1 yuan) and dispenses two types of beverages +(1.5 yuan and 2.5 yuan) with change dispensing capability. +reset is active low + +The vending machine should work as follows: +- Input currency: d1 represents 0.5 yuan, d2 represents 1 yuan +- Beverage selection: sel chooses between two beverages +- Beverage prices: Beverage 1 costs 1.5 yuan, Beverage 2 costs 2.5 yuan +- Change dispensing: Only 0.5 yuan coins are dispensed as change + +Currency input behavior: +- Input currency signals (d1, d2) are automatically edge-detected +- Each currency input produces a pulse signal that goes high on clock + rising edge and low on clock falling edge +- The machine accumulates the total amount of money inserted + +Beverage dispensing: +- out1: Dispenses beverage 1 (1.5 yuan) +- out2: Dispenses beverage 2 (2.5 yuan) +- out3: Dispenses change (0.5 yuan coins only) +- Timing: out1, out2 and out3 respond to the amount and sel in the same cycle + + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule( + input wire clk , + input wire rst , + input wire d1 , + input wire d2 , + input wire sel , + + output reg out1, + output reg out2, + output reg out3 +); +//*************code***********// + + parameter S0=0, S0_5=1, S1=2, S1_5=3, S2=4, S2_5=5, S3=6; + reg[2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= 0; + else + state <= nstate; + end + + always@(*) begin + case(state) + S0 : nstate = d1? S0_5: + d2? S1: + nstate; + S0_5 : nstate = d1? S1: + d2? S1_5: + nstate; + S1 : nstate = d1? S1_5: + d2? S2: + nstate; + S1_5 : nstate = ~sel? S0: + d1? S2: + d2? S2_5: + nstate; + S2 : nstate = ~sel? S0: + d1? S2_5: + d2? S3: + nstate; + default: nstate = S0; + endcase + end + + always @(posedge clk or negedge rst) begin + if (~rst) begin + {out1, out2, out3} <= 3'b000; + end + else begin + case (state) + S0, S0_5, S1: {out1, out2, out3} <= 3'b000; + S1_5: {out1, out2, out3} <= (~sel) ? 3'b100 : 3'b000; + S2: {out1, out2, out3} <= (~sel) ? 3'b101 : 3'b000; + S2_5: {out1, out2, out3} <= (~sel) ? 3'b101 : 3'b010; + S3: {out1, out2, out3} <= (~sel) ? 3'b101 : 3'b011; + default: {out1, out2, out3} <= 3'b000; + endcase + end + end +//*************code***********// +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob015_vending_machine2_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob015_vending_machine2_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..a1ac1b88ccb70a8aeabfb77968ec0cc89b517377 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob015_vending_machine2_ref.sv @@ -0,0 +1,64 @@ + +module RefModule( + input wire clk , + input wire rst , + input wire d1 , + input wire d2 , + input wire sel , + + output reg out1, + output reg out2, + output reg out3 +); +//*************code***********// + + parameter S0=0, S0_5=1, S1=2, S1_5=3, S2=4, S2_5=5, S3=6; + reg[2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= 0; + else + state <= nstate; + end + + always@(*) begin + case(state) + S0 : nstate = d1? S0_5: + d2? S1: + nstate; + S0_5 : nstate = d1? S1: + d2? S1_5: + nstate; + S1 : nstate = d1? S1_5: + d2? S2: + nstate; + S1_5 : nstate = ~sel? S0: + d1? S2: + d2? S2_5: + nstate; + S2 : nstate = ~sel? S0: + d1? S2_5: + d2? S3: + nstate; + default: nstate = S0; + endcase + end + + always@(*) begin + if(~rst) begin + {out1, out2, out3} = 3'b000; + end + else begin + case(state) + S0, S0_5, S1: {out1, out2, out3} = 0; + S1_5 : {out1, out2, out3} = ~sel? 3'b100: 3'b000; + S2 : {out1, out2, out3} = ~sel? 3'b101: 3'b000; + S2_5 : {out1, out2, out3} = ~sel? 3'b101: 3'b010; + S3 : {out1, out2, out3} = ~sel? 3'b101: 3'b011; + default : {out1, out2, out3} = 3'b000; + endcase + end + end +//*************code***********// +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob015_vending_machine2_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob015_vending_machine2_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..17d6dee4540eb7c8bbb9f648823598a0a643812f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob015_vending_machine2_test.sv @@ -0,0 +1,214 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst, + output logic d1, + output logic d2, + output logic sel + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + d1 = 0; + d2 = 0; + sel = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test case 1: Insert 1.5 yuan and select beverage 1 (sel=0) + d1 = 1; @(posedge clk); // Insert 0.5 yuan + d1 = 0; @(posedge clk); + d1 = 1; @(posedge clk); // Insert another 0.5 yuan + d1 = 0; @(posedge clk); + d1 = 1; @(posedge clk); // Insert third 0.5 yuan + d1 = 0; @(posedge clk); + sel = 0; @(posedge clk); // Select beverage 1 + sel = 0; @(posedge clk); // Should dispense beverage 1 + + // Test case 2: Insert 2.5 yuan and select beverage 2 (sel=1) + d2 = 1; @(posedge clk); // Insert 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert another 1 yuan + d2 = 0; @(posedge clk); + d1 = 1; @(posedge clk); // Insert 0.5 yuan + d1 = 0; @(posedge clk); + sel = 1; @(posedge clk); // Select beverage 2 + sel = 1; @(posedge clk); // Should dispense beverage 2 + + // Test case 3: Insert 2 yuan and select beverage 1 (sel=0) - should get change + d2 = 1; @(posedge clk); // Insert 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert another 1 yuan + d2 = 0; @(posedge clk); + sel = 0; @(posedge clk); // Select beverage 1 + sel = 0; @(posedge clk); // Should dispense beverage 1 + 0.5 yuan change + + // Test case 4: Insert 3 yuan and select beverage 2 (sel=1) - should get change + d2 = 1; @(posedge clk); // Insert 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert another 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert third 1 yuan + d2 = 0; @(posedge clk); + sel = 1; @(posedge clk); // Select beverage 2 + sel = 1; @(posedge clk); // Should dispense beverage 2 + 0.5 yuan change + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + d1 <= $random; + d2 <= $random; + sel <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_out1; + int errortime_out1; + int errors_out2; + int errortime_out2; + int errors_out3; + int errortime_out3; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic d1; + logic d2; + logic sel; + logic out1_ref; + logic out2_ref; + logic out3_ref; + logic out1_dut; + logic out2_dut; + logic out3_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, d1, d2, sel, out1_ref, out2_ref, out3_ref, out1_dut, out2_dut, out3_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst, + .d1, + .d2, + .sel ); + + RefModule good1 ( + .clk, + .rst, + .d1, + .d2, + .sel, + .out1(out1_ref), + .out2(out2_ref), + .out3(out3_ref) ); + + TopModule top_module1 ( + .clk, + .rst, + .d1, + .d2, + .sel, + .out1(out1_dut), + .out2(out2_dut), + .out3(out3_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_out1) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out1", stats1.errors_out1, stats1.errortime_out1); + else $display("Hint: Output '%s' has no mismatches.", "out1"); + if (stats1.errors_out2) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out2", stats1.errors_out2, stats1.errortime_out2); + else $display("Hint: Output '%s' has no mismatches.", "out2"); + if (stats1.errors_out3) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out3", stats1.errors_out3, stats1.errortime_out3); + else $display("Hint: Output '%s' has no mismatches.", "out3"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { out1_ref, out2_ref, out3_ref } === ( { out1_ref, out2_ref, out3_ref } ^ { out1_dut, out2_dut, out3_dut } ^ { out1_ref, out2_ref, out3_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (out1_ref !== ( out1_ref ^ out1_dut ^ out1_ref )) + begin if (stats1.errors_out1 == 0) stats1.errortime_out1 = $time; + stats1.errors_out1 = stats1.errors_out1+1'b1; end + if (out2_ref !== ( out2_ref ^ out2_dut ^ out2_ref )) + begin if (stats1.errors_out2 == 0) stats1.errortime_out2 = $time; + stats1.errors_out2 = stats1.errors_out2+1'b1; end + if (out3_ref !== ( out3_ref ^ out3_dut ^ out3_ref )) + begin if (stats1.errors_out3 == 0) stats1.errortime_out3 = $time; + stats1.errors_out3 = stats1.errors_out3+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob016_odd-number_division_with_a_duty_cycle_of_half_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob016_odd-number_division_with_a_duty_cycle_of_half_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..5c90c56fb152bd8d93159d964ded283ce922b54a --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob016_odd-number_division_with_a_duty_cycle_of_half_prompt.txt @@ -0,0 +1,85 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk_in + - input rst + - output clk_out7 + +The module should implement a clock divider that produces a 7:1 frequency +division with a 50% duty cycle output. reset is active low. + +The clock divider should work as follows: +- Input clock: clk_in (reference clock signal) +- Output clock: clk_out7 (divided clock signal) +- Division ratio: 7:1 (output frequency is 1/7 of input frequency) +- Duty cycle: 50% (output is high for half the period, low for half the period) + +Clock division behavior: +- The output clk_out7 should toggle at 1/7 the frequency of clk_in +- The output should maintain a 50% duty cycle (equal high and low times) +- The output should be synchronized to the input clock clk_in +- The division should be continuous and stable + + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule + #(parameter N = 7) + ( + input wire rst , + input wire clk_in, + output wire clk_out7 + ); + + + + reg [3:0] cnt ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + cnt <= 'b0 ; + end + else if (cnt == N-1) begin + cnt <= 'b0 ; + end + else begin + cnt <= cnt + 1'b1 ; + end + end + + reg clkp ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + clkp <= 1'b0 ; + end + else if (cnt == (N>>1)) begin + clkp <= 1 ; + end + else if (cnt == N-1) begin + clkp <= 0 ; + end + end + + + reg clkn ; + always @(negedge clk_in or negedge rst) begin + if (!rst) begin + clkn <= 1'b0 ; + end + else if (cnt == (N>>1) ) begin + clkn <= 1 ; + end + else if (cnt == N-1) begin + clkn <= 0 ; + end + end + + + always @(posedge clk or negedge rst_n) begin + if (!rst_n) + clk_out7 <= 1'b0; + else + clk_out7 <= clkp | clkn; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob016_odd-number_division_with_a_duty_cycle_of_half_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob016_odd-number_division_with_a_duty_cycle_of_half_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..8c5863988ee32c234c730e1f807a42794cb70c74 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob016_odd-number_division_with_a_duty_cycle_of_half_ref.sv @@ -0,0 +1,56 @@ + +module RefModule + #(parameter N = 7) + ( + input wire rst , + input wire clk_in, + output wire clk_out7 + ); + + + + reg [3:0] cnt ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + cnt <= 'b0 ; + end + else if (cnt == N-1) begin + cnt <= 'b0 ; + end + else begin + cnt <= cnt + 1'b1 ; + end + end + + reg clkp ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + clkp <= 1'b0 ; + end + else if (cnt == (N>>1)) begin + clkp <= 1 ; + end + else if (cnt == N-1) begin + clkp <= 0 ; + end + end + + + reg clkn ; + always @(negedge clk_in or negedge rst) begin + if (!rst) begin + clkn <= 1'b0 ; + end + else if (cnt == (N>>1) ) begin + clkn <= 1 ; + end + else if (cnt == N-1) begin + clkn <= 0 ; + end + end + + + assign clk_out7 = clkp | clkn ; + + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob016_odd-number_division_with_a_duty_cycle_of_half_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob016_odd-number_division_with_a_duty_cycle_of_half_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..f9718e4354229f62c57df0869666fd678dadda81 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob016_odd-number_division_with_a_duty_cycle_of_half_test.sv @@ -0,0 +1,133 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test clock divider for multiple cycles + repeat(300) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_clk_out7; + int errortime_clk_out7; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic clk_out7_ref; + logic clk_out7_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, clk_out7_ref, clk_out7_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst ); + + RefModule good1 ( + .rst, + .clk_in(clk), + .clk_out7(clk_out7_ref) ); + + TopModule top_module1 ( + .clk_in(clk), + .rst, + .clk_out7(clk_out7_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_clk_out7) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "clk_out7", stats1.errors_clk_out7, stats1.errortime_clk_out7); + else $display("Hint: Output '%s' has no mismatches.", "clk_out7"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { clk_out7_ref } === ( { clk_out7_ref } ^ { clk_out7_dut } ^ { clk_out7_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (clk_out7_ref !== ( clk_out7_ref ^ clk_out7_dut ^ clk_out7_ref )) + begin if (stats1.errors_clk_out7 == 0) stats1.errortime_clk_out7 = $time; + stats1.errors_clk_out7 = stats1.errors_clk_out7+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob017_arbitrary_fractional_frequency_division_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob017_arbitrary_fractional_frequency_division_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..9de8ef2c2f91299427827d85f5e85dad7d35c5d5 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob017_arbitrary_fractional_frequency_division_prompt.txt @@ -0,0 +1,88 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk_in + - input rst + - output clk_out + +The module should implement an arbitrary fractional frequency divider that +can achieve non-integer division ratios, such as 8.7x frequency division. + +The fractional frequency divider should work as follows: +- Input clock: clk_in (reference clock signal) +- Output clock: clk_out (divided clock signal) +- Division ratio: 8.7x (output frequency is 1/8.7 of input frequency) +- Reset: rst is active low + +Fractional division principle: +Since hardware cannot perform fractional operations (e.g., 2.1 clock cycles +or 3.3 registers), fractional division cannot achieve exact fractional +periods for each output clock cycle. Instead, the implementation uses +the least common multiple approach to achieve the desired average frequency. + +For 8.7x division: +- Over 10 input clock cycles, the output should produce 53 output clock + cycles to achieve an average 8.7x division ratio +- This means: 53 output cycles / 10 input cycles = 5.3x, which when + inverted gives 1/5.3 ≈ 1/8.7x frequency division + +I give u some useful parameter: +parameter M_N = 8'd87; +parameter c89 = 8'd24; // 8/9 clock switching point +parameter div_e = 5'd8; //even period +parameter div_o = 5'd9; //odd period + +Based on the problem description above, the code below has bug, please fix that: +``` +module TopModule( + input wire clk_in, + input wire rst, + output wire clk_out +); + parameter M_N = 8'd87; + parameter c89 = 8'd24; // 8/9 clock switching point + parameter div_e = 5'd8; //Even period + parameter div_o = 5'd9; //Odd period +//*************code***********// + reg [3:0] clk_cnt; + reg [6:0] cyc_cnt; + reg div_flag; + reg clk_out_r; + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_cnt <= 0; + else if(~div_flag) + clk_cnt <= clk_cnt==(div_e-1)? 0: clk_cnt+1; + else + clk_cnt <= clk_cnt==(div_o-1)? 0: clk_cnt+1; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + cyc_cnt <= 0; + else + cyc_cnt <= cyc_cnt==(M_N-1)? 0: cyc_cnt+1; + end + + always @(*) begin + if (~rst) + div_flag = 1'b0; + else + div_flag = (cyc_cnt == (M_N - 1) || cyc_cnt == (c89 - 1)) ? ~div_flag : div_flag; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_out_r <= 0; + else if(~div_flag) + clk_out_r <= clk_cnt<=((div_e>>2)+1); + else + clk_out_r <= clk_cnt<=((div_o>>2)+1); + end + + assign clk_out = clk_out_r; +//*************code***********// +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob017_arbitrary_fractional_frequency_division_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob017_arbitrary_fractional_frequency_division_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..350db63a669018308988298e429b2b98dd23d2f0 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob017_arbitrary_fractional_frequency_division_ref.sv @@ -0,0 +1,51 @@ + +module RefModule( + input wire clk_in, + input wire rst, + output wire clk_out +); + parameter M_N = 8'd87; + parameter c89 = 8'd24; // 8/9 clock switching point + parameter div_e = 5'd8; //Even period + parameter div_o = 5'd9; //Odd period +//*************code***********// + reg [3:0] clk_cnt; + reg [6:0] cyc_cnt; + reg div_flag; + reg clk_out_r; + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_cnt <= 0; + else if(~div_flag) + clk_cnt <= clk_cnt==(div_e-1)? 0: clk_cnt+1; + else + clk_cnt <= clk_cnt==(div_o-1)? 0: clk_cnt+1; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + cyc_cnt <= 0; + else + cyc_cnt <= cyc_cnt==(M_N-1)? 0: cyc_cnt+1; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + div_flag <= 0; + else + div_flag <= cyc_cnt==(M_N-1)||cyc_cnt==(c89-1)? ~div_flag: div_flag; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_out_r <= 0; + else if(~div_flag) + clk_out_r <= clk_cnt<=((div_e>>2)+1); + else + clk_out_r <= clk_cnt<=((div_o>>2)+1); + end + + assign clk_out = clk_out_r; +//*************code***********// +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob017_arbitrary_fractional_frequency_division_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob017_arbitrary_fractional_frequency_division_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..999415b04af6fa5676ee8d641f6deb0976eb13e9 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob017_arbitrary_fractional_frequency_division_test.sv @@ -0,0 +1,134 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test fractional frequency divider for multiple cycles + // The divider should achieve 8.7x division ratio + repeat(1000) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_clk_out; + int errortime_clk_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic clk_out_ref; + logic clk_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, clk_out_ref, clk_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst ); + + RefModule good1 ( + .clk_in(clk), + .rst, + .clk_out(clk_out_ref) ); + + TopModule top_module1 ( + .clk_in(clk), + .rst, + .clk_out(clk_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_clk_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "clk_out", stats1.errors_clk_out, stats1.errortime_clk_out); + else $display("Hint: Output '%s' has no mismatches.", "clk_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { clk_out_ref } === ( { clk_out_ref } ^ { clk_out_dut } ^ { clk_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (clk_out_ref !== ( clk_out_ref ^ clk_out_dut ^ clk_out_ref )) + begin if (stats1.errors_clk_out == 0) stats1.errortime_clk_out = $time; + stats1.errors_clk_out = stats1.errors_clk_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob018_submodules_to_implement_comparison_of_three_input_numbers_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob018_submodules_to_implement_comparison_of_three_input_numbers_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..9b49218182cc3c45d910bab926143e73f28909df --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob018_submodules_to_implement_comparison_of_three_input_numbers_prompt.txt @@ -0,0 +1,87 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input a (8 bits) + - input b (8 bits) + - input c (8 bits) + - output d (8 bits) + +In digital chip design, code that performs a specific function and is +relatively independent is usually written as a submodule. This submodule +is then instantiated in the main module when needed, to improve code +reusability and design hierarchy, and to facilitate subsequent modifications. + +Design a hierarchical module structure where you first create a submodule +that compares two 8-bit unsigned numbers and outputs the smaller one. +Then, in the main module (TopModule), instantiate this submodule to find +the minimum among three 8-bit unsigned inputs. + +The submodule should take two 8-bit inputs (a and b) and output +the smaller of the two values. Keep it as `slave_mod` with the ports of the code below. In the main module, use this submodule to +determine the minimum value among a, b, and c. + +The rst_n signal is an active-low asynchronous reset signal. Assume all +sequential logic is triggered on the positive edge of the clock. + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input [7:0]a, + input [7:0]b, + input [7:0]c, + + output [7:0]d +); + + wire [7:0] c_out1; + slave_mod s1( + .clk(clk), + .rst_n(rst_n), + .a(a), + .b(b), + .c_out(c_out1) + ); + + reg [7:0] c1; + always @(*) begin + if (!rst_n) + c1 = 8'b0; + else + c1 = c; + end + + slave_mod s2( + .clk(clk), + .rst_n(rst_n), + .a(c_out1), + .b(c1), + .c_out(d) + ); + +endmodule + +module slave_mod( + input clk, + input rst_n, + input [7:0]a, + input [7:0]b, + output reg [7:0] c_out +); + always @(posedge clk,negedge rst_n) begin + if(!rst_n) + c_out <= 8'b0; + else + if(a>b) + c_out <= b; + else + c_out <= a; + end + +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob018_submodules_to_implement_comparison_of_three_input_numbers_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob018_submodules_to_implement_comparison_of_three_input_numbers_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..527ec9ea6299cf85372e355b7ea4ba2be258d3ae --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob018_submodules_to_implement_comparison_of_three_input_numbers_ref.sv @@ -0,0 +1,38 @@ + +module RefModule( + input clk, + input rst_n, + input [7:0]a, + input [7:0]b, + input [7:0]c, + + output [7:0]d +); + + wire [7:0] c_out1; + slave_mod s1( + .clk(clk), + .rst_n(rst_n), + .a(a), + .b(b), + .c_out(c_out1) + ); + + reg [7:0] c1; + always @(posedge clk, negedge rst_n) begin + if(!rst_n) + c1 <= 8'b0; + else + c1 <= c; + end + + slave_mod s2( + .clk(clk), + .rst_n(rst_n), + .a(c_out1), + .b(c1), + .c_out(d) + ); + +endmodule + diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob018_submodules_to_implement_comparison_of_three_input_numbers_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob018_submodules_to_implement_comparison_of_three_input_numbers_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..04bb280682925145ffe137654a052a6184268426 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob018_submodules_to_implement_comparison_of_three_input_numbers_test.sv @@ -0,0 +1,238 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [7:0] a, + output logic [7:0] b, + output logic [7:0] c, + output logic chk + // === End your code here === +); + // === Start your code here === + initial begin + chk = 0; + rst_n <= 0; + a <= 0; + b <= 0; + c <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test case 1: a is minimum + a <= 8'd10; + b <= 8'd20; + c <= 8'd15; + @(posedge clk); + + // Test case 2: b is minimum + a <= 8'd25; + b <= 8'd5; + c <= 8'd30; + @(posedge clk); + + // Test case 3: c is minimum + a <= 8'd50; + b <= 8'd40; + c <= 8'd15; + @(posedge clk); + + // Test case 4: all equal + a <= 8'd100; + b <= 8'd100; + c <= 8'd100; + @(posedge clk); + + // Test case 5: two values equal and minimum + a <= 8'd5; + b <= 8'd5; + c <= 8'd10; + @(posedge clk); + + // Test case 6: large values + a <= 8'd255; + b <= 8'd200; + c <= 8'd150; + @(posedge clk); + + // Test case 7: edge cases + a <= 8'd0; + b <= 8'd1; + c <= 8'd2; + @(posedge clk); + + a <= 8'd254; + b <= 8'd255; + c <= 8'd253; + @(posedge clk); + + // Random test sequence + repeat(200) @(posedge clk) begin + a <= $random & 255; + b <= $random & 255; + c <= $random & 255; + end + + // Additional checks: the reference instantiates the candidate's own slave_mod, so the + // compare itself is only checked against itself. Hold a, b, c steady for 4 cycles and + // check d == the minimum of the three unsigned inputs (latency-agnostic, see tb). + repeat(60) begin + case ($urandom % 4) + 0: begin a <= $urandom; b <= $urandom; c <= $urandom; end + 1: begin a <= 8'h80 | $urandom; b <= $urandom & 8'h7F; c <= 8'h80 | $urandom; end + 2: begin a <= $urandom & 8'h7F; b <= 8'h80 | $urandom; c <= 8'h80 | $urandom; end + 3: begin a <= 8'h80 | $urandom; b <= 8'h80 | $urandom; c <= $urandom & 8'h7F; end + endcase + repeat(3) @(posedge clk); + chk <= 1; + @(posedge clk); + chk <= 0; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_d; + int errortime_d; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [7:0] a; + logic [7:0] b; + logic [7:0] c; + logic [7:0] d_ref; + logic [7:0] d_dut; + logic chk; + int errors_min; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, a, b, c, d_ref, d_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .a, + .b, + .c, + .chk + ); + + RefModule good1 ( + .clk, + .rst_n, + .a, + .b, + .c, + .d(d_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .a, + .b, + .c, + .d(d_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + // additional check check: with a, b, c held for 3+ cycles, d must equal min(a, b, c) (unsigned). + wire [7:0] min_abc = (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c); + always @(negedge clk) if (chk) begin + if (d_dut !== min_abc) begin + if (errors_min == 0) $display("Hint: d is not min(a,b,c) at time %0d: a=%0d b=%0d c=%0d d=%0d", $time, a, b, c, d_dut); + errors_min++; + stats1.errors++; + end + end + + final begin + // === Start your code here === + if (stats1.errors_d) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "d", stats1.errors_d, stats1.errortime_d); + else $display("Hint: Output '%s' has no mismatches.", "d"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { d_ref } === ( { d_ref } ^ { d_dut } ^ { d_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (d_ref !== ( d_ref ^ d_dut ^ d_ref )) + begin if (stats1.errors_d == 0) stats1.errortime_d = $time; + stats1.errors_d = stats1.errors_d+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob021_asynchronous_FIFO_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob021_asynchronous_FIFO_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..72f6ff323e47d3fd7254ee4e7512272ee4446655 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob021_asynchronous_FIFO_prompt.txt @@ -0,0 +1,185 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input wclk + - input rclk + - input wrstn + - input rrstn + - input winc + - input rinc + - input wdata (WIDTH bits) + - output wfull + - output rempty + - output rdata (WIDTH bits) + +The module should implement an asynchronous FIFO (First-In-First-Out) buffer +with parameterizable width and depth. The FIFO uses a dual-port RAM for +data storage and implements proper read/write pointer management with +clock domain crossing. + +The asynchronous FIFO should work as follows: +- Write interface: Uses wclk domain with winc (write enable) and wdata +- Read interface: Uses rclk domain with rinc (read enable) and rdata +- Reset: wrstn (write domain reset) and rrstn (read domain reset) +- Status signals: wfull (write full) and rempty (read empty) + +FIFO operation: +- Data is written when winc is high and wfull is low +- Data is read when rinc is high and rempty is low +- wfull indicates the FIFO is full and cannot accept more writes +- rempty indicates the FIFO is empty and has no data to read +- The FIFO should handle clock domain crossing properly + + +Dual-port RAM module (provided) you need to directly use it without any modify: +``` +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk +,input wenc +,input [$clog2(DEPTH)-1:0] waddr +,input [WIDTH-1:0] wdata +,input rclk +,input renc +,input [$clog2(DEPTH)-1:0] raddr +,output reg [WIDTH-1:0] rdata +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin +if(wenc) +RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin +if(renc) +rdata <= RAM_MEM[raddr]; +end + +endmodule +``` + + +`dual_port_RAM` is provided; do not define it in `TopModule.sv`. The +self-test reports it as an unknown module; that is expected. + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input wclk , + input rclk , + input wrstn , + input rrstn , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wire wfull , + output wire rempty , + output wire [WIDTH-1:0] rdata +); + +parameter ADDR_WIDTH = $clog2(DEPTH); + +/**********************addr bin gen*************************/ +reg [ADDR_WIDTH:0] waddr_bin; +reg [ADDR_WIDTH:0] raddr_bin; + +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + waddr_bin <= 'd0; + end + else if(!wfull && winc)begin + waddr_bin <= waddr_bin + 1'd1; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + raddr_bin <= 'd0; + end + else if(!rempty && rinc)begin + raddr_bin <= raddr_bin + 1'd1; + end +end + +/**********************addr gray gen*************************/ +wire [ADDR_WIDTH:0] waddr_gray; +wire [ADDR_WIDTH:0] raddr_gray; +reg [ADDR_WIDTH:0] wptr; +reg [ADDR_WIDTH:0] rptr; +assign waddr_gray = waddr_bin ^ (waddr_bin>>1); +assign raddr_gray = raddr_bin ^ (raddr_bin>>1); +always @(*) begin + if (~wrstn) + wptr = 'd0; + else + wptr = waddr_gray; +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + rptr <= 'd0; + end + else begin + rptr <= raddr_gray; + end +end +/**********************syn addr gray*************************/ +reg [ADDR_WIDTH:0] wptr_buff; +reg [ADDR_WIDTH:0] wptr_syn; +reg [ADDR_WIDTH:0] rptr_buff; +reg [ADDR_WIDTH:0] rptr_syn; +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + rptr_buff <= 'd0; + rptr_syn <= 'd0; + end + else begin + rptr_buff <= rptr; + rptr_syn <= rptr_buff; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + wptr_buff <= 'd0; + wptr_syn <= 'd0; + end + else begin + wptr_buff <= wptr; + wptr_syn <= wptr_buff; + end +end +/**********************full empty gen*************************/ +assign wfull = (wptr == {~rptr_syn[ADDR_WIDTH:ADDR_WIDTH-1],rptr_syn[ADDR_WIDTH-2:0]}); +assign rempty = (rptr == wptr_syn); + +/**********************RAM*************************/ +wire wen ; +wire ren ; +wire wren;//high write +wire [ADDR_WIDTH-1:0] waddr; +wire [ADDR_WIDTH-1:0] raddr; +assign wen = winc & !wfull; +assign ren = rinc & !rempty; +assign waddr = waddr_bin[ADDR_WIDTH-1:0]; +assign raddr = raddr_bin[ADDR_WIDTH-1:0]; + +dual_port_RAM #(.DEPTH(DEPTH), + .WIDTH(WIDTH) +)dual_port_RAM( + .wclk (wclk), + .wenc (wen), + .waddr(waddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .wdata(wdata), //Data write + .rclk (rclk), + .renc (ren), + .raddr(raddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .rdata(rdata) //Data output +); + +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob021_asynchronous_FIFO_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob021_asynchronous_FIFO_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..795d58f089d5e69688191a77ecbf071073eb49bd --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob021_asynchronous_FIFO_ref.sv @@ -0,0 +1,144 @@ + +module RefModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input wclk , + input rclk , + input wrstn , + input rrstn , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wire wfull , + output wire rempty , + output wire [WIDTH-1:0] rdata +); + +parameter ADDR_WIDTH = $clog2(DEPTH); + +/**********************addr bin gen*************************/ +reg [ADDR_WIDTH:0] waddr_bin; +reg [ADDR_WIDTH:0] raddr_bin; + +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + waddr_bin <= 'd0; + end + else if(!wfull && winc)begin + waddr_bin <= waddr_bin + 1'd1; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + raddr_bin <= 'd0; + end + else if(!rempty && rinc)begin + raddr_bin <= raddr_bin + 1'd1; + end +end + +/**********************addr gray gen*************************/ +wire [ADDR_WIDTH:0] waddr_gray; +wire [ADDR_WIDTH:0] raddr_gray; +reg [ADDR_WIDTH:0] wptr; +reg [ADDR_WIDTH:0] rptr; +assign waddr_gray = waddr_bin ^ (waddr_bin>>1); +assign raddr_gray = raddr_bin ^ (raddr_bin>>1); +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + wptr <= 'd0; + end + else begin + wptr <= waddr_gray; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + rptr <= 'd0; + end + else begin + rptr <= raddr_gray; + end +end +/**********************syn addr gray*************************/ +reg [ADDR_WIDTH:0] wptr_buff; +reg [ADDR_WIDTH:0] wptr_syn; +reg [ADDR_WIDTH:0] rptr_buff; +reg [ADDR_WIDTH:0] rptr_syn; +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + rptr_buff <= 'd0; + rptr_syn <= 'd0; + end + else begin + rptr_buff <= rptr; + rptr_syn <= rptr_buff; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + wptr_buff <= 'd0; + wptr_syn <= 'd0; + end + else begin + wptr_buff <= wptr; + wptr_syn <= wptr_buff; + end +end +/**********************full empty gen*************************/ +assign wfull = (wptr == {~rptr_syn[ADDR_WIDTH:ADDR_WIDTH-1],rptr_syn[ADDR_WIDTH-2:0]}); +assign rempty = (rptr == wptr_syn); + +/**********************RAM*************************/ +wire wen ; +wire ren ; +wire wren;//high write +wire [ADDR_WIDTH-1:0] waddr; +wire [ADDR_WIDTH-1:0] raddr; +assign wen = winc & !wfull; +assign ren = rinc & !rempty; +assign waddr = waddr_bin[ADDR_WIDTH-1:0]; +assign raddr = raddr_bin[ADDR_WIDTH-1:0]; + +dual_port_RAM #(.DEPTH(DEPTH), + .WIDTH(WIDTH) +)dual_port_RAM( + .wclk (wclk), + .wenc (wen), + .waddr(waddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .wdata(wdata), //Data write + .rclk (rclk), + .renc (ren), + .raddr(raddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .rdata(rdata) //Data output +); + +endmodule + +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk + ,input wenc + ,input [$clog2(DEPTH)-1:0] waddr //Take log2 of depth to get address bit width. + ,input [WIDTH-1:0] wdata //Data write + ,input rclk + ,input renc + ,input [$clog2(DEPTH)-1:0] raddr //Take log2 of depth to get address bit width. + ,output reg [WIDTH-1:0] rdata //Data output +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin + if(wenc) + RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin + if(renc) + rdata <= RAM_MEM[raddr]; +end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob021_asynchronous_FIFO_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob021_asynchronous_FIFO_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..cfac6dd9030ae54dece5c33b93b201bf6a0e1743 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob021_asynchronous_FIFO_test.sv @@ -0,0 +1,292 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic wclk, + output logic rclk, + output logic wrstn, + output logic rrstn, + output logic winc, + output logic rinc, + output logic [7:0] wdata + // === End your code here === +); + // === Start your code here === + reg wclk_reg = 0; + reg rclk_reg = 0; + assign wclk = wclk_reg; + assign rclk = rclk_reg; + + initial forever #10 wclk_reg = ~wclk_reg; // 50MHz write clock + initial forever #15 rclk_reg = ~rclk_reg; // 33.3MHz read clock + + initial begin + wrstn = 0; + rrstn = 0; + winc = 0; + rinc = 0; + wdata = 0; + repeat(20) @(posedge wclk); @(posedge rclk); + wrstn <= 1; + rrstn <= 1; + @(posedge wclk); @(posedge rclk); + + // Test basic write and read operations + winc <= 1; + wdata <= 8'hAA; @(posedge wclk); + wdata <= 8'h55; @(posedge wclk); + wdata <= 8'h33; @(posedge wclk); + wdata <= 8'hCC; @(posedge wclk); + winc <= 0; + @(posedge wclk); + + // Test read operations + rinc <= 1; + repeat(4) @(posedge rclk); + rinc <= 0; + @(posedge rclk); + + // Test simultaneous write and read + winc <= 1; + rinc <= 1; + wdata <= 8'h11; @(posedge wclk); @(posedge rclk); + wdata <= 8'h22; @(posedge wclk); @(posedge rclk); + wdata <= 8'h44; @(posedge wclk); @(posedge rclk); + wdata <= 8'h88; @(posedge wclk); @(posedge rclk); + winc <= 0; + rinc <= 0; + @(posedge wclk); + + // Test FIFO full condition + winc <= 1; + wdata <= 8'hF0; @(posedge wclk); + wdata <= 8'hF1; @(posedge wclk); + wdata <= 8'hF2; @(posedge wclk); + wdata <= 8'hF3; @(posedge wclk); + wdata <= 8'hF4; @(posedge wclk); + wdata <= 8'hF5; @(posedge wclk); + wdata <= 8'hF6; @(posedge wclk); + wdata <= 8'hF7; @(posedge wclk); + wdata <= 8'hF8; @(posedge wclk); + wdata <= 8'hF9; @(posedge wclk); + wdata <= 8'hFA; @(posedge wclk); + wdata <= 8'hFB; @(posedge wclk); + wdata <= 8'hFC; @(posedge wclk); + wdata <= 8'hFD; @(posedge wclk); + wdata <= 8'hFE; @(posedge wclk); + wdata <= 8'hFF; @(posedge wclk); + winc <= 0; + @(posedge wclk); + + // Test FIFO empty condition + rinc <= 1; + repeat(20) @(posedge rclk); // Should trigger rempty + rinc <= 0; + @(posedge rclk); + + // Random testing + repeat(200) @(posedge wclk, posedge rclk) begin + winc <= $random; + rinc <= $random; + wdata <= $random % 256; + end + + // === Additional checks: both domain resets asserted mid-operation === + // Spec: "Reset: wrstn (write domain reset) and rrstn (read domain reset)" with the quoted asynchronous + // resets of both pointer domains. Both resets are asserted together; the FIFO never gets near full here. + // All new stimulus changes 2 time units after a clock edge (edges fall on multiples of 5): no edge race. + @(posedge wclk); #2; + winc <= 0; rinc <= 0; + wrstn <= 0; rrstn <= 0; // reset from whatever state the random section left + repeat(3) @(posedge rclk); + #2 wrstn <= 1; rrstn <= 1; + repeat(2) @(posedge rclk); + #2 winc <= 1; + for (int k = 0; k < 6; k++) begin // FIFO holds 6 words + wdata <= 8'h90 + k; + @(posedge wclk); #2; + end + winc <= 0; + repeat(4) @(posedge rclk); + #2 rinc <= 1; // read 2 of them + repeat(2) @(posedge rclk); + #2 rinc <= 0; + repeat(2) @(posedge rclk); + #2 wrstn <= 0; rrstn <= 0; // reset while 4 words are still stored + repeat(3) @(posedge rclk); + #2 wrstn <= 1; rrstn <= 1; + repeat(4) @(posedge rclk); + #2 rinc <= 1; // FIFO must be empty: these reads are ignored + repeat(3) @(posedge rclk); + #2 rinc <= 0; + @(posedge wclk); #2 winc <= 1; + for (int k = 0; k < 5; k++) begin + wdata <= 8'hA0 + k; + @(posedge wclk); #2; + end + winc <= 0; + repeat(4) @(posedge rclk); + #2 rinc <= 1; + repeat(10) @(posedge rclk); // read the 5 new words, then reads while empty + #2 rinc <= 0; + repeat(4) @(posedge rclk); + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_wfull; + int errortime_wfull; + int errors_rempty; + int errortime_rempty; + int errors_rdata; + int errortime_rdata; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic wclk; + logic rclk; + logic wrstn; + logic rrstn; + logic winc; + logic rinc; + logic [7:0] wdata; + logic wfull_ref; + logic rempty_ref; + logic [7:0] rdata_ref; + logic wfull_dut; + logic rempty_dut; + logic [7:0] rdata_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, wclk, rclk, wrstn, rrstn, winc, rinc, wdata, wfull_ref, rempty_ref, rdata_ref, wfull_dut, rempty_dut, rdata_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .wclk, + .rclk, + .wrstn, + .rrstn, + .winc, + .rinc, + .wdata ); + + RefModule good1 ( + .wclk, + .rclk, + .wrstn, + .rrstn, + .winc, + .rinc, + .wdata, + .wfull(wfull_ref), + .rempty(rempty_ref), + .rdata(rdata_ref) ); + + TopModule top_module1 ( + .wclk, + .rclk, + .wrstn, + .rrstn, + .winc, + .rinc, + .wdata, + .wfull(wfull_dut), + .rempty(rempty_dut), + .rdata(rdata_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_wfull) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "wfull", stats1.errors_wfull, stats1.errortime_wfull); + else $display("Hint: Output '%s' has no mismatches.", "wfull"); + if (stats1.errors_rempty) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rempty", stats1.errors_rempty, stats1.errortime_rempty); + else $display("Hint: Output '%s' has no mismatches.", "rempty"); + if (stats1.errors_rdata) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rdata", stats1.errors_rdata, stats1.errortime_rdata); + else $display("Hint: Output '%s' has no mismatches.", "rdata"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { wfull_ref, rempty_ref, rdata_ref } === ( { wfull_ref, rempty_ref, rdata_ref } ^ { wfull_dut, rempty_dut, rdata_dut } ^ { wfull_ref, rempty_ref, rdata_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (wfull_ref !== ( wfull_ref ^ wfull_dut ^ wfull_ref )) + begin if (stats1.errors_wfull == 0) stats1.errortime_wfull = $time; + stats1.errors_wfull = stats1.errors_wfull+1'b1; end + if (rempty_ref !== ( rempty_ref ^ rempty_dut ^ rempty_ref )) + begin if (stats1.errors_rempty == 0) stats1.errortime_rempty = $time; + stats1.errors_rempty = stats1.errors_rempty+1'b1; end + if (rdata_ref !== ( rdata_ref ^ rdata_dut ^ rdata_ref )) + begin if (stats1.errors_rdata == 0) stats1.errortime_rdata = $time; + stats1.errors_rdata = stats1.errors_rdata+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob022_synchronous_FIFO_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob022_synchronous_FIFO_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..d84924b284338a6e94da2c959048725ba2853d09 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob022_synchronous_FIFO_prompt.txt @@ -0,0 +1,138 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input winc + - input rinc + - input wdata (WIDTH bits) + - output wfull + - output rempty + - output rdata (WIDTH bits) + +The module should implement a synchronous FIFO (First In, First Out) buffer +that uses a dual-port RAM for data storage. The FIFO should be parameterizable +in both WIDTH and DEPTH, with configurable bit WIDTH and DEPTH parameters. + +The synchronous FIFO should work as follows: +- Input interface: + - clk: Single clock signal for both read and write operations + - rst_n: Active low reset signal + - winc: Write enable signal (pulse to write data) + - rinc: Read enable signal (pulse to read data) + - wdata: 8-bit input data to be written +- Output interface: + - wfull: FIFO full flag (high when FIFO is full) + - rempty: FIFO empty flag (high when FIFO is empty) + - rdata: 8-bit output data read from FIFO + +FIFO operation: +- Write operations occur on the positive edge of clk when winc is high +- Read operations occur on the positive edge of clk when rinc is high +- The FIFO uses a dual-port RAM for data storage +- Write and read pointers are managed to track FIFO status +- Full and empty conditions are detected based on pointer comparison +- The FIFO should handle simultaneous read and write operations correctly + +Dual-port RAM module (provided): +``` +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk +,input wenc +,input [$clog2(DEPTH)-1:0] waddr +,input [WIDTH-1:0] wdata +,input rclk +,input renc +,input [$clog2(DEPTH)-1:0] raddr +,output reg [WIDTH-1:0] rdata +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin + if(wenc) + RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin + if(renc) + rdata <= RAM_MEM[raddr]; +end + +endmodule +``` + +`dual_port_RAM` is provided separately, but not to the self-test; do not +define it in `/project/TopModule.sv`. + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input clk , + input rst_n , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wfull , + output rempty , + output wire [WIDTH-1:0] rdata +); + +localparam DP_WD = $clog2(DEPTH); + +reg [DP_WD :0]waddr; +wire wenc; +wire waddr_d_h; +wire [DP_WD -1:0]waddr_d_l; +assign wenc = winc & (!wfull); +assign waddr_d_h = (waddr[DP_WD-1:0] == DEPTH-1) ? ~waddr[DP_WD] : waddr[DP_WD]; +assign waddr_d_l = (waddr[DP_WD-1:0] == DEPTH-1) ? 0 : waddr[DP_WD-1:0] + 1; + +always @(*) begin + if (~rst_n) + waddr = 0; + else if (wenc) + waddr = {waddr_d_h, waddr_d_l}; + else + waddr = waddr; +end + +reg [DP_WD :0]raddr; +wire renc; +wire raddr_d_h; +wire [DP_WD -1:0]raddr_d_l; +assign renc = rinc & (!rempty); +assign raddr_d_h = (raddr[DP_WD-1:0] == DEPTH-1) ? ~raddr[DP_WD] : raddr[DP_WD]; +assign raddr_d_l = (raddr[DP_WD-1:0] == DEPTH-1) ? 0 : raddr[DP_WD-1:0] + 1; + +always @(posedge clk or negedge rst_n)begin + if(~rst_n) raddr <= 0; + else if(renc) raddr <= {raddr_d_h, raddr_d_l}; +end + +wire [DP_WD :0]fifo_cnt = (waddr[DP_WD] == raddr[DP_WD]) ? waddr[DP_WD-1:0] - raddr[DP_WD-1:0]: + (waddr[DP_WD-1:0] + DEPTH - raddr[DP_WD-1:0]); + +assign rempty = (fifo_cnt == 0); +assign wfull = (fifo_cnt == DEPTH); + +dual_port_RAM #(.DEPTH(DEPTH), .WIDTH(WIDTH)) +u_ram ( + .wclk (clk), + .wenc (wenc), + .waddr (waddr[$clog2(DEPTH)-1:0]), + .wdata (wdata), + .rclk (clk), + .renc (renc), + .raddr (raddr[$clog2(DEPTH)-1:0]), + .rdata (rdata) +); + +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob022_synchronous_FIFO_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob022_synchronous_FIFO_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..56d8eef402a8c831f04631a322a0d7a520809e67 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob022_synchronous_FIFO_ref.sv @@ -0,0 +1,88 @@ + +module RefModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input clk , + input rst_n , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wfull , + output rempty , + output wire [WIDTH-1:0] rdata +); + +localparam DP_WD = $clog2(DEPTH); + +reg [DP_WD :0]waddr; +wire wenc; +wire waddr_d_h; +wire [DP_WD -1:0]waddr_d_l; +assign wenc = winc & (!wfull); +assign waddr_d_h = (waddr[DP_WD-1:0] == DEPTH-1) ? ~waddr[DP_WD] : waddr[DP_WD]; +assign waddr_d_l = (waddr[DP_WD-1:0] == DEPTH-1) ? 0 : waddr[DP_WD-1:0] + 1; +always @(posedge clk or negedge rst_n)begin + if(~rst_n) waddr <= 0; + else if(wenc) waddr <= {waddr_d_h, waddr_d_l}; +end + +reg [DP_WD :0]raddr; +wire renc; +wire raddr_d_h; +wire [DP_WD -1:0]raddr_d_l; +assign renc = rinc & (!rempty); +assign raddr_d_h = (raddr[DP_WD-1:0] == DEPTH-1) ? ~raddr[DP_WD] : raddr[DP_WD]; +assign raddr_d_l = (raddr[DP_WD-1:0] == DEPTH-1) ? 0 : raddr[DP_WD-1:0] + 1; +always @(posedge clk or negedge rst_n)begin + if(~rst_n) raddr <= 0; + else if(renc) raddr <= {raddr_d_h, raddr_d_l}; +end + +wire [DP_WD :0]fifo_cnt = (waddr[DP_WD] == raddr[DP_WD]) ? waddr[DP_WD-1:0] - raddr[DP_WD-1:0]: + (waddr[DP_WD-1:0] + DEPTH - raddr[DP_WD-1:0]); + +assign rempty = (fifo_cnt == 0); +assign wfull = (fifo_cnt == DEPTH); + +dual_port_RAM #(.DEPTH(DEPTH), .WIDTH(WIDTH)) +u_ram ( + .wclk (clk), + .wenc (wenc), + .waddr (waddr[$clog2(DEPTH)-1:0]), + .wdata (wdata), + .rclk (clk), + .renc (renc), + .raddr (raddr[$clog2(DEPTH)-1:0]), + .rdata (rdata) +); + +endmodule + +/**********************************RAM************************************/ +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk + ,input wenc + ,input [$clog2(DEPTH)-1:0] waddr //Take log2 of depth to get address bit width. + ,input [WIDTH-1:0] wdata //Data write + ,input rclk + ,input renc + ,input [$clog2(DEPTH)-1:0] raddr //Take log2 of depth to get address bit width. + ,output reg [WIDTH-1:0] rdata //Data output +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin + if(wenc) + RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin + if(renc) + rdata <= RAM_MEM[raddr]; +end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob022_synchronous_FIFO_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob022_synchronous_FIFO_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..2f390c79de5e0bdb52fcffe96fad3a7009e4f2b1 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob022_synchronous_FIFO_test.sv @@ -0,0 +1,296 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic winc, + output logic rinc, + output logic [7:0] wdata, + output logic rst_n2, + output logic winc2, + output logic rinc2, + output logic [7:0] wdata2 + // === End your code here === +); + // === Start your code here === + initial begin + rst_n2 = 0; + winc2 = 0; + rinc2 = 0; + wdata2 = 0; + rst_n = 0; + winc = 0; + rinc = 0; + wdata = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic write operations + winc <= 1; + wdata <= 8'hAA; @(posedge clk); + wdata <= 8'h55; @(posedge clk); + wdata <= 8'h33; @(posedge clk); + wdata <= 8'hCC; @(posedge clk); + winc <= 0; + @(posedge clk); + + // Test basic read operations + rinc <= 1; + repeat(4) @(posedge clk); + rinc <= 0; + @(posedge clk); + + // Test simultaneous write and read + winc <= 1; + rinc <= 1; + wdata <= 8'h11; @(posedge clk); + wdata <= 8'h22; @(posedge clk); + wdata <= 8'h44; @(posedge clk); + wdata <= 8'h88; @(posedge clk); + winc <= 0; + rinc <= 0; + @(posedge clk); + + // Test FIFO full condition + winc <= 1; + wdata <= 8'hF0; @(posedge clk); + wdata <= 8'hF1; @(posedge clk); + wdata <= 8'hF2; @(posedge clk); + wdata <= 8'hF3; @(posedge clk); + wdata <= 8'hF4; @(posedge clk); + wdata <= 8'hF5; @(posedge clk); + wdata <= 8'hF6; @(posedge clk); + wdata <= 8'hF7; @(posedge clk); + wdata <= 8'hF8; @(posedge clk); + wdata <= 8'hF9; @(posedge clk); + wdata <= 8'hFA; @(posedge clk); + wdata <= 8'hFB; @(posedge clk); + wdata <= 8'hFC; @(posedge clk); + wdata <= 8'hFD; @(posedge clk); + wdata <= 8'hFE; @(posedge clk); + wdata <= 8'hFF; @(posedge clk); // Should trigger wfull + winc <= 0; + @(posedge clk); + + // Test FIFO empty condition + rinc <= 1; + repeat(20) @(posedge clk); // Should trigger rempty + rinc <= 0; + @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + winc <= $random; + rinc <= $random; + wdata <= $random % 256; + end + + // Additional checks: "The FIFO should be parameterizable in both WIDTH and DEPTH". + // A second pair of instances with DEPTH = 4 and DEPTH = 32 (WIDTH = 8) is compared with + // RefModule at the same depths (see tb); until now it was held in reset. + @(negedge clk) rst_n2 = 1; + @(posedge clk); + winc2 <= 1; // fill past full for DEPTH 4 and 32 + repeat(36) begin wdata2 <= $urandom; @(posedge clk); end + winc2 <= 0; + rinc2 <= 1; // drain past empty + repeat(36) @(posedge clk); + rinc2 <= 0; + @(posedge clk); + winc2 <= 1; rinc2 <= 1; // simultaneous read/write + repeat(10) begin wdata2 <= $urandom; @(posedge clk); end + winc2 <= 0; rinc2 <= 0; + repeat(300) begin // random, write-biased then read-biased + winc2 <= ($urandom % 4) != 0; + rinc2 <= ($urandom % 2); + wdata2 <= $urandom; + @(posedge clk); + end + repeat(300) begin + winc2 <= ($urandom % 2); + rinc2 <= ($urandom % 4) != 0; + wdata2 <= $urandom; + @(posedge clk); + end + winc2 <= 0; rinc2 <= 0; + repeat(2) @(posedge clk); + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_wfull; + int errortime_wfull; + int errors_rempty; + int errortime_rempty; + int errors_rdata; + int errortime_rdata; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic winc; + logic rinc; + logic [7:0] wdata; + logic wfull_ref; + logic rempty_ref; + logic [7:0] rdata_ref; + logic wfull_dut; + logic rempty_dut; + logic [7:0] rdata_dut; + // additional check: DEPTH = 4 and DEPTH = 32 instances + logic rst_n2, winc2, rinc2; + logic [7:0] wdata2; + logic wfull_ref4, rempty_ref4, wfull_dut4, rempty_dut4, wfull_ref32, rempty_ref32, wfull_dut32, rempty_dut32; + logic [7:0] rdata_ref4, rdata_dut4, rdata_ref32, rdata_dut32; + int errors_depth; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, winc, rinc, wdata, wfull_ref, rempty_ref, rdata_ref, wfull_dut, rempty_dut, rdata_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .winc, + .rinc, + .wdata, + .rst_n2, + .winc2, + .rinc2, + .wdata2 ); + + RefModule good1 ( + .clk, + .rst_n, + .winc, + .rinc, + .wdata, + .wfull(wfull_ref), + .rempty(rempty_ref), + .rdata(rdata_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .winc, + .rinc, + .wdata, + .wfull(wfull_dut), + .rempty(rempty_dut), + .rdata(rdata_dut) ); + RefModule #(.WIDTH(8), .DEPTH(4)) good4 (.clk, .rst_n(rst_n2), .winc(winc2), .rinc(rinc2), .wdata(wdata2), + .wfull(wfull_ref4), .rempty(rempty_ref4), .rdata(rdata_ref4) ); + TopModule #(.WIDTH(8), .DEPTH(4)) top_module4 (.clk, .rst_n(rst_n2), .winc(winc2), .rinc(rinc2), .wdata(wdata2), + .wfull(wfull_dut4), .rempty(rempty_dut4), .rdata(rdata_dut4) ); + RefModule #(.WIDTH(8), .DEPTH(32)) good32 (.clk, .rst_n(rst_n2), .winc(winc2), .rinc(rinc2), .wdata(wdata2), + .wfull(wfull_ref32), .rempty(rempty_ref32), .rdata(rdata_ref32) ); + TopModule #(.WIDTH(8), .DEPTH(32)) top_module32 (.clk, .rst_n(rst_n2), .winc(winc2), .rinc(rinc2), .wdata(wdata2), + .wfull(wfull_dut32), .rempty(rempty_dut32), .rdata(rdata_dut32) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + // additional check check: the DEPTH = 4 / 32 instances match RefModule at the same depth (X in ref = don't care). + wire [9:0] v_ref4 = {wfull_ref4, rempty_ref4, rdata_ref4}, v_dut4 = {wfull_dut4, rempty_dut4, rdata_dut4}; + wire [9:0] v_ref32 = {wfull_ref32, rempty_ref32, rdata_ref32}, v_dut32 = {wfull_dut32, rempty_dut32, rdata_dut32}; + always @(posedge clk, negedge clk) begin + if (v_ref4 !== (v_ref4 ^ v_dut4 ^ v_ref4) || v_ref32 !== (v_ref32 ^ v_dut32 ^ v_ref32)) begin + if (errors_depth == 0) $display("Hint: DEPTH=4/32 instance mismatch first at time %0d", $time); + errors_depth++; + stats1.errors++; + end + end + + final begin + // === Start your code here === + if (stats1.errors_wfull) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "wfull", stats1.errors_wfull, stats1.errortime_wfull); + else $display("Hint: Output '%s' has no mismatches.", "wfull"); + if (stats1.errors_rempty) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rempty", stats1.errors_rempty, stats1.errortime_rempty); + else $display("Hint: Output '%s' has no mismatches.", "rempty"); + if (stats1.errors_rdata) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rdata", stats1.errors_rdata, stats1.errortime_rdata); + else $display("Hint: Output '%s' has no mismatches.", "rdata"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { wfull_ref, rempty_ref, rdata_ref } === ( { wfull_ref, rempty_ref, rdata_ref } ^ { wfull_dut, rempty_dut, rdata_dut } ^ { wfull_ref, rempty_ref, rdata_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (wfull_ref !== ( wfull_ref ^ wfull_dut ^ wfull_ref )) + begin if (stats1.errors_wfull == 0) stats1.errortime_wfull = $time; + stats1.errors_wfull = stats1.errors_wfull+1'b1; end + if (rempty_ref !== ( rempty_ref ^ rempty_dut ^ rempty_ref )) + begin if (stats1.errors_rempty == 0) stats1.errortime_rempty = $time; + stats1.errors_rempty = stats1.errors_rempty+1'b1; end + if (rdata_ref !== ( rdata_ref ^ rdata_dut ^ rdata_ref )) + begin if (stats1.errors_rdata == 0) stats1.errortime_rdata = $time; + stats1.errors_rdata = stats1.errors_rdata+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob023_gray_code_counter_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob023_gray_code_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..6b6384467a22ad5b4cb6d4f0786a1f75cd46101c --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob023_gray_code_counter_prompt.txt @@ -0,0 +1,69 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output reg [3:0] gray_out + +The module should implement a 4-bit Gray code counter. Assume all sequential +logic is triggered on the positive edge of the clock. The reset input is active +low asynchronous, and should reset the counter to 0. + +The Gray code counter should work as follows: +- The counter should generate a 4-bit Gray code sequence +- The counter increments every two clock cycles +- The implementation should follow this process: + 1. Input a Gray code value (use an internal wire variable initialized to 0) + 2. Convert the Gray code to binary representation + 3. Increment the binary value by 1 + 4. Convert the incremented binary back to Gray code + 5. Output the resulting Gray code on gray_out at the next positive clock + edge (0000 while rst_n is low) +- Gray code is a binary numeral system where two successive values differ + in only one bit position +- The counter should cycle through all 16 possible 4-bit Gray code values + and then repeat +- When rst_n is low, the counter should reset to 0000 + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + + output reg [3:0] gray_out +); +reg [3:0] bin_out; +wire [3:0] gray_wire; + +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_out <= 4'b0; + end + else begin + bin_out[3] = gray_wire[3]; + bin_out[2] = gray_wire[2]^bin_out[3]; + bin_out[1] = gray_wire[1]^bin_out[2]; + bin_out[0] = gray_wire[0]^bin_out[1]; + end +end +reg [3:0] bin_add_wire; +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_add_wire <= 4'b0; + end + else begin + bin_add_wire <= bin_out + 1'b1; + end +end +assign gray_wire = (bin_add_wire >> 1) ^ bin_add_wire; + +always @(*) begin + if (rst_n == 1'b0) + gray_out = 4'b0; + else + gray_out = gray_wire; +end +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob023_gray_code_counter_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob023_gray_code_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..894e7d68646d0b1562e7cdf67d0a96597dac0e4f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob023_gray_code_counter_ref.sv @@ -0,0 +1,44 @@ + +module RefModule( + input clk, + input rst_n, + + output reg [3:0] gray_out +); +//Gray code to binary conversion +reg [3:0] bin_out; +wire [3:0] gray_wire; + +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_out <= 4'b0; + end + else begin + bin_out[3] = gray_wire[3]; + bin_out[2] = gray_wire[2]^bin_out[3]; + bin_out[1] = gray_wire[1]^bin_out[2]; + bin_out[0] = gray_wire[0]^bin_out[1]; + end +end +//Binary increment +reg [3:0] bin_add_wire; +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_add_wire <= 4'b0; + end + else begin + bin_add_wire <= bin_out + 1'b1; + end +end +//Binary to gray code conversion +assign gray_wire = (bin_add_wire >> 1) ^ bin_add_wire; + +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + gray_out <= 4'b0; + end + else begin + gray_out <= gray_wire; + end +end +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob023_gray_code_counter_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob023_gray_code_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..71832152abeadfdf1876bddadd268eb2ce375283 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob023_gray_code_counter_test.sv @@ -0,0 +1,132 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test Gray code counter for multiple cycles + repeat(200) @(posedge clk); + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_gray_out; + int errortime_gray_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [3:0] gray_out_ref; + logic [3:0] gray_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, gray_out_ref, gray_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .gray_out(gray_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .gray_out(gray_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_gray_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "gray_out", stats1.errors_gray_out, stats1.errortime_gray_out); + else $display("Hint: Output '%s' has no mismatches.", "gray_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { gray_out_ref } === ( { gray_out_ref } ^ { gray_out_dut } ^ { gray_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (gray_out_ref !== ( gray_out_ref ^ gray_out_dut ^ gray_out_ref )) + begin if (stats1.errors_gray_out == 0) stats1.errortime_gray_out = $time; + stats1.errors_gray_out = stats1.errors_gray_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob025_pulse_synchronization_circuit_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob025_pulse_synchronization_circuit_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..7929868447a6a57d8b10b3bec75c9ebb612a1399 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob025_pulse_synchronization_circuit_prompt.txt @@ -0,0 +1,75 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk_fast + - input clk_slow + - input rst_n + - input data_in + - output data_out + +Extract a single clock cycle width pulse from clock domain A and establish +another single clock width pulse in a new clock domain B. + +The frequency of clock domain A is 10 times that of clock domain B. The +intervals between pulses in clock domain A are large, so there is no need +to consider the problem of pulses being too close together. + +When the fast clock domain detects an input pulse, it will toggle a flag +signal and synchronize it to the slow clock domain. After the slow clock +domain detects the change in this flag signal, it outputs a corresponding +single cycle pulse. + +The rst_n signal is an active-low asynchronous reset signal. Assume all +sequential logic is triggered on the positive edge of the clock. + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk_fast , + input clk_slow , + input rst_n , + input data_in , + + output data_out +); +reg Q_fast; +always @(posedge clk_fast or negedge rst_n) begin + if(~rst_n) begin + Q_fast <= 'd0; + end + else if(data_in)begin + Q_fast <= ~Q_fast; + end + else if(~data_in)begin + Q_fast <= Q_fast; + end +end +reg Q_buff0; +reg Q_buff1; + +always @(*) begin + if (~rst_n) begin + Q_buff0 = 'd0; + Q_buff1 = 'd0; + end + else begin + Q_buff0 = Q_fast; + Q_buff1 = Q_buff0; + end +end + +reg Q_slow; +always @(posedge clk_slow or negedge rst_n) begin + if(~rst_n) begin + Q_slow <= 'd0; + end + else begin + Q_slow <= Q_buff1; + end +end + +assign data_out = Q_buff1 ^ Q_slow; +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob025_pulse_synchronization_circuit_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob025_pulse_synchronization_circuit_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..370add92fcdb39b2b3bfc5db449ea3df65945962 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob025_pulse_synchronization_circuit_ref.sv @@ -0,0 +1,45 @@ + +module RefModule( + input clk_fast , + input clk_slow , + input rst_n , + input data_in , + + output data_out +); +reg Q_fast; +always @(posedge clk_fast or negedge rst_n) begin + if(~rst_n) begin + Q_fast <= 'd0; + end + else if(data_in)begin + Q_fast <= ~Q_fast; + end + else if(~data_in)begin + Q_fast <= Q_fast; + end +end +reg Q_buff0; +reg Q_buff1; +always @(posedge clk_slow or negedge rst_n) begin + if(~rst_n) begin + Q_buff0 <= 'd0; + Q_buff1 <= 'd0; + end + else begin + Q_buff0 <= Q_fast; + Q_buff1 <= Q_buff0; + end +end +reg Q_slow; +always @(posedge clk_slow or negedge rst_n) begin + if(~rst_n) begin + Q_slow <= 'd0; + end + else begin + Q_slow <= Q_buff1; + end +end + +assign data_out = Q_buff1 ^ Q_slow; +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob025_pulse_synchronization_circuit_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob025_pulse_synchronization_circuit_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..16b80ac0342a138eada73c60b6497597f4374a04 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob025_pulse_synchronization_circuit_test.sv @@ -0,0 +1,194 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic clk_fast, + output logic clk_slow, + output logic rst_n, + output logic data_in + // === End your code here === +); + // === Start your code here === + initial begin + clk_fast <= 0; + clk_slow <= 0; + rst_n <= 0; + data_in <= 0; + + // Generate fast clock (10x faster than slow clock) + forever begin + #5 clk_fast <= ~clk_fast; + end + end + + initial begin + // Generate slow clock + forever begin + #50 clk_slow <= ~clk_slow; + end + end + + initial begin + // Reset sequence + @(posedge clk_fast); + @(posedge clk_fast); + rst_n <= 1; + @(posedge clk_fast); + + // Test basic pulse detection + data_in <= 1; + @(posedge clk_fast); + data_in <= 0; + @(posedge clk_fast); + @(posedge clk_fast); + @(posedge clk_fast); + + // Wait for slow clock to process + repeat(20) @(posedge clk_slow); + + // Test multiple pulses + data_in <= 1; + @(posedge clk_fast); + @(posedge clk_fast); + @(posedge clk_fast); + @(posedge clk_fast); + data_in <= 0; + @(posedge clk_fast); + @(posedge clk_fast); + + // Wait for slow clock to process + repeat(20) @(posedge clk_slow); + + // Random test sequence + repeat(100) @(posedge clk_fast) begin + data_in <= $random & 1; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic clk_fast; + logic clk_slow; + logic rst_n; + logic data_in; + logic data_out_ref; + logic data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, clk_fast, clk_slow, rst_n, data_in, data_out_ref, data_out_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .clk_fast, + .clk_slow, + .rst_n, + .data_in + ); + + RefModule good1 ( + .clk_fast, + .clk_slow, + .rst_n, + .data_in, + .data_out(data_out_ref) + ); + + TopModule top_module1 ( + .clk_fast, + .clk_slow, + .rst_n, + .data_in, + .data_out(data_out_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { data_out_ref } === ( { data_out_ref } ^ { data_out_dut } ^ { data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob026_simple_stopwatch_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob026_simple_stopwatch_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..27956d88a4b3aea764a55aec79e96786ad97c4a9 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob026_simple_stopwatch_prompt.txt @@ -0,0 +1,58 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output reg [5:0] second + - output reg [5:0] minute + +The module should implement a simple stopwatch functionality. Assume all +sequential logic is triggered on the positive edge of the clock. The reset +input is active low asynchronous, and should reset both counters to 0. + +The simple stopwatch should work as follows: +- The second counter counts from 1 to 60 in a cycle +- When the second counter reaches 60, it resets to 1 and the minute counter + increments by 1 +- The minute counter counts from 0 to 60 +- When the minute counter reaches 60, the stopwatch pauses counting +- Both counters should reset to 0 when rst_n is low + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + + output reg [5:0]second, + output reg [5:0]minute + ); + + + always @(*) begin + if (!rst_n) + minute = 6'd0; + else if (second == 6'd60) + minute = minute + 1; + else + minute = minute; + end + + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + second <= 6'd0; + end + else if(second == 6'd60) + begin + second <= 6'd1; + end + else if (minute == 60) + second <= second; + else + second <= second+1'd1; +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob026_simple_stopwatch_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob026_simple_stopwatch_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..bd35c5aaeb9b14ce97cf12d1d4138ffc5c7beb7f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob026_simple_stopwatch_ref.sv @@ -0,0 +1,38 @@ + +module RefModule( + input clk, + input rst_n, + + output reg [5:0]second, + output reg [5:0]minute + ); + + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + minute <= 6'd0; + end + else if (second == 6'd60) + begin + minute <= minute+1; + end + else + begin + minute <= minute; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + second <= 6'd0; + end + else if(second == 6'd60) + begin + second <= 6'd1; + end + else if (minute == 60) + second <= second; + else + second <= second+1'd1; +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob026_simple_stopwatch_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob026_simple_stopwatch_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..2e9249e83fe18d332a6bc787b4165e264d25c592 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob026_simple_stopwatch_test.sv @@ -0,0 +1,146 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test stopwatch for multiple cycles + // The stopwatch counts seconds from 1-60, then increments minutes + // When minute reaches 60, the stopwatch pauses + repeat(5000) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_second; + int errortime_second; + int errors_minute; + int errortime_minute; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [5:0] second_ref; + logic [5:0] minute_ref; + logic [5:0] second_dut; + logic [5:0] minute_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, second_ref, minute_ref, second_dut, minute_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .second(second_ref), + .minute(minute_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .second(second_dut), + .minute(minute_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_second) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "second", stats1.errors_second, stats1.errortime_second); + else $display("Hint: Output '%s' has no mismatches.", "second"); + if (stats1.errors_minute) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "minute", stats1.errors_minute, stats1.errortime_minute); + else $display("Hint: Output '%s' has no mismatches.", "minute"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { second_ref, minute_ref } === ( { second_ref, minute_ref } ^ { second_dut, minute_dut } ^ { second_ref, minute_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (second_ref !== ( second_ref ^ second_dut ^ second_ref )) + begin if (stats1.errors_second == 0) stats1.errortime_second = $time; + stats1.errors_second = stats1.errors_second+1'b1; end + if (minute_ref !== ( minute_ref ^ minute_dut ^ minute_ref )) + begin if (stats1.errors_minute == 0) stats1.errortime_minute = $time; + stats1.errors_minute = stats1.errors_minute+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob027_settable_counter_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob027_settable_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..c18a495f8ab9cc3d884d0d738b743284779b0f74 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob027_settable_counter_prompt.txt @@ -0,0 +1,84 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input set + - input [3:0] set_num + - output reg zero + - output reg [3:0] number + +The module should implement a hexadecimal settable counter. Assume all +sequential logic is triggered on the positive edge of the clock. The reset +input is active low asynchronous, and should reset the counter to 0. + +The settable counter should work as follows: +- The counter increments on each clock cycle and cycles through hexadecimal + values (0 to F, then back to 0) +- When the counter reaches 0, the zero signal is asserted (goes high) +- When the set signal is active, the counter output is forced to the value + of set_num instead of incrementing +- The zero signal is high only when the number equals 0, and low at all + other times +- When rst_n is low, the counter should reset to 0, and zero is 0 +- the output zero and number will be update on the next rising edge of the clock: + number lags the counter by one clock cycle and zero is high when number + is 0, so set_num reaches number on the second rising edge after set + +Counter operation: +- Normal counting: Counter increments each clock cycle (0, 1, 2, ..., F, 0, 1, ...). +- Set operation: When set is active, number is set to set_num value +- Zero detection: The zero signal is high only when number equals 0 +- Reset behavior: Active low reset to 0 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input set, + input [3:0] set_num, + output reg [3:0]number, + output reg zero + ); + reg [3:0]num; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + zero <= 1'd0; + end + else if (num == 4'd0) + begin + zero <= 1'b1; + end + else + begin + zero <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + num <= 4'b0; + end + else if(set) + begin + num <= set_num; + end + else + begin + num <= num + 1'd1; + end + + always @(*) begin + if (!rst_n) + number = 1'b0; + else + number = num; + end + +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob027_settable_counter_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob027_settable_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..f9cf9eb7c182e1264308d1d243de4012ff9a028c --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob027_settable_counter_ref.sv @@ -0,0 +1,50 @@ + +module RefModule( + input clk, + input rst_n, + input set, + input [3:0] set_num, + output reg [3:0]number, + output reg zero + ); + reg [3:0]num; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + zero <= 1'd0; + end + else if (num == 4'd0) + begin + zero <= 1'b1; + end + else + begin + zero <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + num <= 4'b0; + end + else if(set) + begin + num <= set_num; + end + else + begin + num <= num + 1'd1; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + number <= 1'd0; + end + else + begin + number <= num; + end + +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob027_settable_counter_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob027_settable_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..c6249e9b80c00ef43e9b590baab344971408bed8 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob027_settable_counter_test.sv @@ -0,0 +1,175 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic set, + output logic [3:0] set_num + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + set = 0; + set_num = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test normal counting + set = 0; + repeat(20) @(posedge clk); // Count: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,0,1,2,3 + + // Test set operation + set = 1; + set_num = 4'hA; @(posedge clk); // Set to A + set_num = 4'h5; @(posedge clk); // Set to 5 + set_num = 4'hF; @(posedge clk); // Set to F + set = 0; + @(posedge clk); + // Test set to 0 (should trigger zero signal) + set = 1; + set_num = 4'h0; @(posedge clk); // Set to 0 + set = 0; + @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + set <= $random; + set_num <= $random % 16; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_number; + int errortime_number; + int errors_zero; + int errortime_zero; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic set; + logic [3:0] set_num; + logic [3:0] number_ref; + logic zero_ref; + logic [3:0] number_dut; + logic zero_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, set, set_num, number_ref, zero_ref, number_dut, zero_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .set, + .set_num ); + + RefModule good1 ( + .clk, + .rst_n, + .set, + .set_num, + .number(number_ref), + .zero(zero_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .set, + .set_num, + .number(number_dut), + .zero(zero_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_number) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "number", stats1.errors_number, stats1.errortime_number); + else $display("Hint: Output '%s' has no mismatches.", "number"); + if (stats1.errors_zero) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "zero", stats1.errors_zero, stats1.errortime_zero); + else $display("Hint: Output '%s' has no mismatches.", "zero"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { number_ref, zero_ref } === ( { number_ref, zero_ref } ^ { number_dut, zero_dut } ^ { number_ref, zero_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (number_ref !== ( number_ref ^ number_dut ^ number_ref )) + begin if (stats1.errors_number == 0) stats1.errortime_number = $time; + stats1.errors_number = stats1.errors_number+1'b1; end + if (zero_ref !== ( zero_ref ^ zero_dut ^ zero_ref )) + begin if (stats1.errors_zero == 0) stats1.errortime_zero = $time; + stats1.errors_zero = stats1.errors_zero+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob028_up_and_down_counter_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob028_up_and_down_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..09d4627f7864a3f57a6c985c22360380694428e1 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob028_up_and_down_counter_prompt.txt @@ -0,0 +1,70 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input mode + - output reg [3:0] number + - output reg zero + +The module should implement a decimal up/down counter. Assume all sequential +logic is triggered on the positive edge of the clock. The reset input is active +low asynchronous, and should reset the counter to 0, and zero is also be 0. + +The up/down counter should work as follows: +- When mode signal is 1, the counter increments (counts up) each clock cycle +- When mode signal is 0, the counter decrements (counts down) each clock cycle +- The counter output (number) is a 4-bit value representing the current count +- The zero signal is asserted (goes high) for the next cycle when the number reaches 0, and is + low at all other times +- When rst_n is low, the counter should reset to 0 + +Counter operation: +- Mode = 1: Counter increments each clock cycle (0, 1, 2, 3, ..., 9, 0, 1, ...), only up to 9 +- Mode = 0: Counter decrements each clock cycle (9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, ...) +- Zero detection: The zero signal is high for next cycle only when number equals 0 +- Reset behavior: Active low reset to 0 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + input mode, + output reg [3:0]number, + output reg zero + ); + + always @(*) begin + if (!rst_n) + zero = 1'b0; + else if (number == 4'd0) + zero = 1'b1; + else + zero = 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + number <= 4'b0; + end + else if(mode) + begin + if(number == 9) + number <= 0; + else + number <= number + 1'd1; + end + else if(!mode) + begin + if(number == 0) + number <= 9; + else + number <= number - 1'd1; + end + else number <= number; +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob028_up_and_down_counter_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob028_up_and_down_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..3f4a2006e216d481bf02975a552c29d203eb8309 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob028_up_and_down_counter_ref.sv @@ -0,0 +1,44 @@ + +module RefModule( + input clk, + input rst_n, + input mode, + output reg [3:0]number, + output reg zero + ); + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + zero <= 1'd0; + end + else if (number == 4'd0) + begin + zero <= 1'b1; + end + else + begin + zero <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + number <= 4'b0; + end + else if(mode) + begin + if(number == 9) + number <= 0; + else + number <= number + 1'd1; + end + else if(!mode) + begin + if(number == 0) + number <= 9; + else + number <= number - 1'd1; + end + else number <= number; +endmodule \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob028_up_and_down_counter_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob028_up_and_down_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..9c8d87a866b931fe81a5cb15a4f3ba531c1391a7 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob028_up_and_down_counter_test.sv @@ -0,0 +1,165 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic mode + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + mode = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test up counting (mode = 1) + mode <= 1; + repeat(15) @(posedge clk); // Count up: 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4 + + // Test down counting (mode = 0) + mode <= 0; + repeat(15) @(posedge clk); // Count down: 5,4,3,2,1,0,9,8,7,6,5,4,3,2,1 + + // Test mode switching + mode <= 1; @(posedge clk); // Count up + mode <= 0; @(posedge clk); // Count down + mode <= 1; @(posedge clk); // Count up + mode <= 0; @(posedge clk); // Count down + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + mode <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_number; + int errortime_number; + int errors_zero; + int errortime_zero; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic mode; + logic [3:0] number_ref; + logic zero_ref; + logic [3:0] number_dut; + logic zero_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, mode, number_ref, zero_ref, number_dut, zero_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .mode ); + + RefModule good1 ( + .clk, + .rst_n, + .mode, + .number(number_ref), + .zero(zero_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .mode, + .number(number_dut), + .zero(zero_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_number) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "number", stats1.errors_number, stats1.errortime_number); + else $display("Hint: Output '%s' has no mismatches.", "number"); + if (stats1.errors_zero) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "zero", stats1.errors_zero, stats1.errortime_zero); + else $display("Hint: Output '%s' has no mismatches.", "zero"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { number_ref, zero_ref } === ( { number_ref, zero_ref } ^ { number_dut, zero_dut } ^ { number_ref, zero_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (number_ref !== ( number_ref ^ number_dut ^ number_ref )) + begin if (stats1.errors_number == 0) stats1.errortime_number = $time; + stats1.errors_number = stats1.errors_number+1'b1; end + if (zero_ref !== ( zero_ref ^ zero_dut ^ zero_ref )) + begin if (stats1.errors_zero == 0) stats1.errortime_zero = $time; + stats1.errors_zero = stats1.errors_zero+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob030_simple_implementation_RAM_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob030_simple_implementation_RAM_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..da450f90fd4400646ce2cc40642857c6da81350f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob030_simple_implementation_RAM_prompt.txt @@ -0,0 +1,60 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input read_en + - input write_en + - input read_addr (8 bits) + - input write_addr (8 bits) + - input write_data (4 bits) + - output read_data (4 bits) + +Implement a dual-port RAM with depth of 8 and data width of 4 bits. All +data should be initialized to 0000. The RAM has two separate ports: one +for reading data and one for writing data. Read and write operations can +be performed simultaneously. + +When the read enable signal read_en is asserted, the module should read +data from the location specified by read_addr and output it on read_data. +When the write enable signal write_en is asserted, the module should write +write_data to the location specified by write_addr. + +The rst_n signal is an active-low asynchronous reset signal. Assume all +sequential logic is triggered on the positive edge of the clock. + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk, + input rst_n, + + input write_en, + input [7:0]write_addr, + input [3:0]write_data, + + input read_en, + input [7:0]read_addr, + output reg [3:0]read_data +); + reg [3:0] myRAM [7:0]; + + reg [8:0] i; + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + for(i=0;i<256;i=i+1) + myRAM[i] <= 0; + else + myRAM[write_addr] <= write_en? write_data: myRAM[write_addr]; + end + + always @(*) begin + if (~rst_n) + read_data = 0; + else + read_data = read_en ? myRAM[read_addr] : read_data; + end + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob030_simple_implementation_RAM_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob030_simple_implementation_RAM_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..c979fc6d858b9b6871182cfcc04ae6454d16a591 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob030_simple_implementation_RAM_ref.sv @@ -0,0 +1,31 @@ + +module RefModule( + input clk, + input rst_n, + + input write_en, + input [7:0]write_addr, + input [3:0]write_data, + + input read_en, + input [7:0]read_addr, + output reg [3:0]read_data +); + reg [3:0] myRAM [7:0]; + + reg [8:0] i; + // integer i; // Not synthesizable + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + for(i=0;i<256;i=i+1) + myRAM[i] <= 0; + else + myRAM[write_addr] <= write_en? write_data: myRAM[write_addr]; + end + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + read_data <= 0; + else + read_data <= read_en? myRAM[read_addr]: read_data; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob030_simple_implementation_RAM_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob030_simple_implementation_RAM_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..21619b4ac32a1fe2b5e064e4455edcc01e90d402 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob030_simple_implementation_RAM_test.sv @@ -0,0 +1,254 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic read_en, + output logic write_en, + output logic [7:0] read_addr, + output logic [7:0] write_addr, + output logic [3:0] write_data + // === End your code here === +); + // === Start your code here === + initial begin + rst_n <= 0; + read_en <= 0; + write_en <= 0; + read_addr <= 0; + write_addr <= 0; + write_data <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic write operations + write_en <= 1; + write_addr <= 0; + write_data <= 4'b1010; + @(posedge clk); + + write_addr <= 1; + write_data <= 4'b0101; + @(posedge clk); + + write_addr <= 2; + write_data <= 4'b1111; + @(posedge clk); + + write_en <= 0; + @(posedge clk); + + // Test basic read operations + read_en <= 1; + read_addr <= 0; + @(posedge clk); // 1010 + + read_addr <= 1; + @(posedge clk); // 0101 + + read_addr <= 2; + @(posedge clk); // 1111 + + read_en <= 0; + @(posedge clk); + + // Test simultaneous read and write + read_en <= 1; + write_en <= 1; + read_addr <= 0; + write_addr <= 3; + write_data <= 4'b1100; + @(posedge clk); + + read_addr <= 1; + write_addr <= 4; + write_data <= 4'b0011; + @(posedge clk); + + read_en <= 0; + write_en <= 0; + @(posedge clk); + + // Random test sequence + repeat(200) @(posedge clk) begin + read_en <= $random & 1; + write_en <= $random & 1; + read_addr <= $random & 7; // Only use addresses 0-7 for depth 8 + write_addr <= $random & 7; + write_data <= $random & 15; // 4-bit data + end + + // Additional checks: rst_n is the active-low asynchronous reset and the data is + // initialized to 0000 (the given code clears every word on reset): after a mid-run reset + // every location reads 0000. Fill all 8 words with non-zero data, pulse rst_n low between + // clock edges, then read every address. + @(posedge clk); + read_en <= 0; + write_en <= 1; + for (int k = 0; k < 8; k++) begin + write_addr <= k; + write_data <= 4'hF - k; + @(posedge clk); + end + write_en <= 0; + read_en <= 1; + for (int k = 0; k < 8; k++) begin // read back (non-zero) before the reset + read_addr <= k; + @(posedge clk); + end + read_en <= 0; + @(posedge clk); + #2 rst_n <= 0; // assert between edges + repeat(2) @(posedge clk); + #2 rst_n <= 1; // release between edges + @(posedge clk); + read_en <= 1; + for (int k = 0; k < 8; k++) begin // every word must read 0000 + read_addr <= k; + @(posedge clk); + end + read_en <= 0; + repeat(2) @(posedge clk); + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_read_data; + int errortime_read_data; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic read_en; + logic write_en; + logic [7:0] read_addr; + logic [7:0] write_addr; + logic [3:0] write_data; + logic [3:0] read_data_ref; + logic [3:0] read_data_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, read_en, write_en, read_addr, write_addr, write_data, read_data_ref, read_data_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .read_en, + .write_en, + .read_addr, + .write_addr, + .write_data + ); + + RefModule good1 ( + .clk, + .rst_n, + .write_en, + .write_addr, + .write_data, + .read_en, + .read_addr, + .read_data(read_data_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .read_en, + .write_en, + .read_addr, + .write_addr, + .write_data, + .read_data(read_data_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_read_data) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "read_data", stats1.errors_read_data, stats1.errortime_read_data); + else $display("Hint: Output '%s' has no mismatches.", "read_data"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { read_data_ref } === ( { read_data_ref } ^ { read_data_dut } ^ { read_data_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (read_data_ref !== ( read_data_ref ^ read_data_dut ^ read_data_ref )) + begin if (stats1.errors_read_data == 0) stats1.errortime_read_data = $time; + stats1.errors_read_data = stats1.errors_read_data+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob031_johnson_counter_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob031_johnson_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..785d86374b621bc2a1c5cd351323ca46d59c8729 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob031_johnson_counter_prompt.txt @@ -0,0 +1,37 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output reg [3:0] Q + +The module should implement a 4-bit Johnson counter (also known as a twisted +ring counter). Assume all sequential logic is triggered on the positive edge +of the clock. The reset input is active low asynchronous, and should reset +the counter to 0000. + +The Johnson counter should work as follows: +- The counter follows a specific sequence of states in a circular pattern +- The state sequence is: 0000 -> 1000 -> 1100 -> 1110 -> 1111 -> 0111 -> 0011 -> 0001 -> 0000 +- This creates a cycle of 8 states before repeating +- On each positive clock edge, the counter advances to the next state in the sequence +- When rst_n is low, the counter should reset to 0000 + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule( + input clk , + input rst_n, + + output reg [3:0] Q +); + always @(*) begin + if (!rst_n) + Q = 'd0; + else + Q = {~Q[0], Q[3:1]}; + end + +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob031_johnson_counter_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob031_johnson_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..1f68a1ac386fe58d6d32038dd39d05c6c255c978 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob031_johnson_counter_ref.sv @@ -0,0 +1,12 @@ + +module RefModule( + input clk , + input rst_n, + + output reg [3:0] Q +); + always@(posedge clk or negedge rst_n)begin + if(!rst_n) Q <= 'd0; + else Q <= {~Q[0], Q[3 : 1]}; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob031_johnson_counter_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob031_johnson_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..dc33439851b599a6017ec24dc89ff6eab20774ae --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob031_johnson_counter_test.sv @@ -0,0 +1,133 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test Johnson counter for multiple cycles + // The counter cycles through 8 states: 0000->1000->1100->1110->1111->0111->0011->0001->0000 + repeat(200) @(posedge clk); + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_Q; + int errortime_Q; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [3:0] Q_ref; + logic [3:0] Q_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, Q_ref, Q_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .Q(Q_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .Q(Q_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_Q) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "Q", stats1.errors_Q, stats1.errortime_Q); + else $display("Hint: Output '%s' has no mismatches.", "Q"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { Q_ref } === ( { Q_ref } ^ { Q_dut } ^ { Q_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (Q_ref !== ( Q_ref ^ Q_dut ^ Q_ref )) + begin if (stats1.errors_Q == 0) stats1.errortime_Q = $time; + stats1.errors_Q = stats1.errors_Q+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob032_pipeline_multiplier_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob032_pipeline_multiplier_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..56b0e4a41b75369dcbef666c8aacd141258bd67a --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob032_pipeline_multiplier_prompt.txt @@ -0,0 +1,70 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input [3:0] mul_a + - input [3:0] mul_b + - output reg [7:0] mul_out + +The module should implement a 4-bit unsigned pipelined multiplier. Assume all +sequential logic is triggered on the positive edge of the clock. The reset +input is active low asynchronous, and should reset the multiplier to 0. + +The pipelined multiplier should work as follows: +- The multiplier performs unsigned multiplication of two 4-bit inputs (mul_a and mul_b) +- The result is an 8-bit output (mul_out) representing the product +- The multiplication is implemented using a pipelined architecture for improved + performance and throughput +- The pipeline allows new inputs to be processed every clock cycle while + previous multiplications are still being completed +- When rst_n is low, the multiplier should reset to 0 + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule#( + parameter size = 4 +)( + input clk , + input rst_n , + input [size-1:0] mul_a , + input [size-1:0] mul_b , + + output reg [size*2-1:0] mul_out +); + //parameter + parameter N = size * 2; + //defination + wire [N - 1 : 0] temp [0 : 3]; + + reg [N - 1 : 0] adder_0; + reg [N - 1 : 0] adder_1; + + //output + genvar i; + generate + for(i = 0; i < 4; i = i + 1)begin : loop + assign temp[i] = mul_b[i] ? mul_a << i : 'd0; + end + endgenerate + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_0 <= 'd0; + else adder_0 <= temp[0] + temp[1]; + end + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_1 <= 'd0; + else adder_1 <= temp[2] + temp[3]; + end + + always @(*) begin + if (!rst_n) + mul_out = 'd0; + else + mul_out = adder_0 + adder_1; + end +endmodule +``` \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob032_pipeline_multiplier_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob032_pipeline_multiplier_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..4686788d7af68d2fe694cf911d4d8452976defa0 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob032_pipeline_multiplier_ref.sv @@ -0,0 +1,42 @@ + +module RefModule#( + parameter size = 4 +)( + input clk , + input rst_n , + input [size-1:0] mul_a , + input [size-1:0] mul_b , + + output reg [size*2-1:0] mul_out +); + //parameter + parameter N = size * 2; + //defination + wire [N - 1 : 0] temp [0 : 3]; + + reg [N - 1 : 0] adder_0; + reg [N - 1 : 0] adder_1; + + //output + genvar i; + generate + for(i = 0; i < 4; i = i + 1)begin : loop + assign temp[i] = mul_b[i] ? mul_a << i : 'd0; + end + endgenerate + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_0 <= 'd0; + else adder_0 <= temp[0] + temp[1]; + end + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_1 <= 'd0; + else adder_1 <= temp[2] + temp[3]; + end + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) mul_out <= 'd0; + else mul_out <= adder_0 + adder_1; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob032_pipeline_multiplier_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob032_pipeline_multiplier_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..1dce2b7fad20e13c6d4e6b7019b17061acb1401f --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob032_pipeline_multiplier_test.sv @@ -0,0 +1,156 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [3:0] mul_a, + output logic [3:0] mul_b + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + mul_a = 0; + mul_b = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic multiplication cases + mul_a <= 4'd2; mul_b <= 4'd3; @(posedge clk); // 2 * 3 = 6 + mul_a <= 4'd4; mul_b <= 4'd5; @(posedge clk); // 4 * 5 = 20 + mul_a <= 4'd7; mul_b <= 4'd8; @(posedge clk); // 7 * 8 = 56 + mul_a <= 4'd9; mul_b <= 4'd10; @(posedge clk); // 9 * 10 = 90 + mul_a <= 4'd12; mul_b <= 4'd13; @(posedge clk); // 12 * 13 = 156 + mul_a <= 4'd15; mul_b <= 4'd15; @(posedge clk); // 15 * 15 = 225 + + // Test edge cases + mul_a <= 4'd0; mul_b <= 4'd5; @(posedge clk); // 0 * 5 = 0 + mul_a <= 4'd3; mul_b <= 4'd0; @(posedge clk); // 3 * 0 = 0 + mul_a <= 4'd1; mul_b <= 4'd1; @(posedge clk); // 1 * 1 = 1 + mul_a <= 4'd15; mul_b <= 4'd1; @(posedge clk); // 15 * 1 = 15 + mul_a <= 4'd1; mul_b <= 4'd15; @(posedge clk); // 1 * 15 = 15 + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_mul_out; + int errortime_mul_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [3:0] mul_a; + logic [3:0] mul_b; + logic [7:0] mul_out_ref; + logic [7:0] mul_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, mul_a, mul_b, mul_out_ref, mul_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .mul_a, + .mul_b ); + + RefModule good1 ( + .clk, + .rst_n, + .mul_a, + .mul_b, + .mul_out(mul_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .mul_a, + .mul_b, + .mul_out(mul_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_mul_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "mul_out", stats1.errors_mul_out, stats1.errortime_mul_out); + else $display("Hint: Output '%s' has no mismatches.", "mul_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { mul_out_ref } === ( { mul_out_ref } ^ { mul_out_dut } ^ { mul_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (mul_out_ref !== ( mul_out_ref ^ mul_out_dut ^ mul_out_ref )) + begin if (stats1.errors_mul_out == 0) stats1.errortime_mul_out = $time; + stats1.errors_mul_out = stats1.errors_mul_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob033_traffic_lights_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob033_traffic_lights_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..14545c417d9c370a60987db7d8142d173285e406 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob033_traffic_lights_prompt.txt @@ -0,0 +1,147 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input pass_request + - output reg [7:0] clock + - output reg red + - output reg yellow + - output reg green + +The module should implement a traffic light controller with pedestrian +request functionality. Assume all sequential logic is triggered on the +positive edge of the clock. The reset input is active low asynchronous, +and should reset the traffic light to red state. + +The traffic light controller should work as follows: +- Normal operation: Traffic light cycles through red (10 clock cycles), + green (60 clock cycles), and yellow (5 clock cycles) in sequence +- Pedestrian request: When pass_request is active, if remaining green time + is greater than 10 clock cycles, it should be shortened to 10 clock + cycles; if remaining green time is 10 clock cycles or less, it remains + unchanged +- Light sequence: Red → Green → Yellow → Red (repeating) +- Clock counter: The clock output shows the countdown timer for the current + light state +- Light outputs: Only one light should be active at a time (red, yellow, + or green) + +Traffic light operation: +- Red light: 10 clock cycles, clock counts down from 10 to 1 +- Green light: 60 clock cycles normally, or 10 clock cycles if pedestrian + request is active and remaining time > 10 +- Yellow light: 5 clock cycles, clock counts down from 5 to 1 +- Pedestrian request: Shortens green time to 10 cycles if remaining time > 10 +- Reset behavior: Active low reset to red state with clock = 10 + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule + ( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input pass_request, + output wire[7:0]clock, + output reg red, + output reg yellow, + output reg green + ); + + parameter idle = 2'd0, + s1_red = 2'd1, + s2_green = 2'd2, + s3_yellow = 2'd3; + reg [7:0] cnt; + reg [1:0] state; + reg p_red,p_yellow,p_green; //Buffer previous signal light values for rising edge detection + + +always @(posedge clk or negedge rst_n) + begin + if(!rst_n) + begin + state <= idle; + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + end + else case(state) + idle: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + state <= s1_red; + end + s1_red: + begin + p_red <= 1'b1; + p_green <= 1'b0; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s2_green; + else + state <= s1_red; + end + s2_green: + begin + p_red <= 1'b0; + p_green <= 1'b1; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s3_yellow; + else + state <= s2_green; + end + s3_yellow: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b1; + if (cnt == 3) + state <= s1_red; + else + state <= s3_yellow; + end + endcase + end + +always @(posedge clk or negedge rst_n) + if(!rst_n) + cnt <= 7'd10; + else if (pass_request&&green&&(cnt>10)) + cnt <= 7'd10; + else if (!green&&p_green) + cnt <= 7'd60; + else if (!yellow&&p_yellow) + cnt <= 7'd5; + else if (!red&&p_red) + cnt <= 7'd10; + else cnt <= cnt -1; + assign clock = cnt; + +always @(*) begin + if (!rst_n) begin + yellow = 1'b0; + red = 1'b0; + green = 1'b0; + end + else begin + yellow = p_yellow; + red = p_red; + green = p_green; + end +end + + +endmodule + + +// notice: the why we set if (cnt == 3) for state transition, because at cnt 3, the next cycle +// cnt 2 the state will be updated +// cnt 1 the p_yellow, p_red, p_green will be updated +// cnt 0 the yellow, red, green will be updated +// next cycle, the traffic light will change +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob033_traffic_lights_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob033_traffic_lights_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..3f0649950bde0cffb62e5e6de99d06676e0caa8e --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob033_traffic_lights_ref.sv @@ -0,0 +1,107 @@ + +module RefModule + ( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input pass_request, + output wire[7:0]clock, + output reg red, + output reg yellow, + output reg green + ); + + parameter idle = 2'd0, + s1_red = 2'd1, + s2_green = 2'd2, + s3_yellow = 2'd3; + reg [7:0] cnt; + reg [1:0] state; + reg p_red,p_yellow,p_green; //Buffer previous signal light values for rising edge detection + + +always @(posedge clk or negedge rst_n) + begin + if(!rst_n) + begin + state <= idle; + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + end + else case(state) + idle: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + state <= s1_red; + end + s1_red: + begin + p_red <= 1'b1; + p_green <= 1'b0; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s2_green; + else + state <= s1_red; + end + s2_green: + begin + p_red <= 1'b0; + p_green <= 1'b1; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s3_yellow; + else + state <= s2_green; + end + s3_yellow: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b1; + if (cnt == 3) + state <= s1_red; + else + state <= s3_yellow; + end + endcase + end + +always @(posedge clk or negedge rst_n) + if(!rst_n) + cnt <= 7'd10; + else if (pass_request&&green&&(cnt>10)) + cnt <= 7'd10; + else if (!green&&p_green) + cnt <= 7'd60; + else if (!yellow&&p_yellow) + cnt <= 7'd5; + else if (!red&&p_red) + cnt <= 7'd10; + else cnt <= cnt -1; + assign clock = cnt; + +always @(posedge clk or negedge rst_n) + if(!rst_n) + begin + yellow <= 1'd0; + red <= 1'd0; + green <= 1'd0; + end + else + begin + yellow <= p_yellow; + red <= p_red; + green <= p_green; + end + +endmodule + + +// notice: the why we set if (cnt == 3) for state transition, because at cnt 3, the next cycle +// cnt 2 the state will be updated +// cnt 1 the p_yellow, p_red, p_green will be updated +// cnt 0 the yellow, red, green will be updated +// next cycle, the traffic light will change \ No newline at end of file diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob033_traffic_lights_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob033_traffic_lights_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..b3b7eda5538208585526f69ada1c07a7459e47e2 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob033_traffic_lights_test.sv @@ -0,0 +1,195 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic pass_request + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + pass_request = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test normal traffic light cycle + pass_request = 0; + repeat(100) @(posedge clk); // Let it cycle through red->green->yellow->red + + // Test pedestrian request during green phase + pass_request = 1; + repeat(20) @(posedge clk); // Request during green + pass_request = 0; + @(posedge clk); + + // Test pedestrian request during red phase (should not affect) + pass_request = 1; + repeat(15) @(posedge clk); // Request during red + pass_request = 0; + @(posedge clk); + + // Test pedestrian request during yellow phase (should not affect) + pass_request = 1; + repeat(10) @(posedge clk); // Request during yellow + pass_request = 0; + @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + pass_request <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_clock; + int errortime_clock; + int errors_red; + int errortime_red; + int errors_yellow; + int errortime_yellow; + int errors_green; + int errortime_green; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic pass_request; + logic [7:0] clock_ref; + logic red_ref; + logic yellow_ref; + logic green_ref; + logic [7:0] clock_dut; + logic red_dut; + logic yellow_dut; + logic green_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, pass_request, clock_ref, red_ref, yellow_ref, green_ref, clock_dut, red_dut, yellow_dut, green_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .pass_request ); + + RefModule good1 ( + .clk, + .rst_n, + .pass_request, + .clock(clock_ref), + .red(red_ref), + .yellow(yellow_ref), + .green(green_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .pass_request, + .clock(clock_dut), + .red(red_dut), + .yellow(yellow_dut), + .green(green_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_clock) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "clock", stats1.errors_clock, stats1.errortime_clock); + else $display("Hint: Output '%s' has no mismatches.", "clock"); + if (stats1.errors_red) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "red", stats1.errors_red, stats1.errortime_red); + else $display("Hint: Output '%s' has no mismatches.", "red"); + if (stats1.errors_yellow) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "yellow", stats1.errors_yellow, stats1.errortime_yellow); + else $display("Hint: Output '%s' has no mismatches.", "yellow"); + if (stats1.errors_green) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "green", stats1.errors_green, stats1.errortime_green); + else $display("Hint: Output '%s' has no mismatches.", "green"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { clock_ref, red_ref, yellow_ref, green_ref } === ( { clock_ref, red_ref, yellow_ref, green_ref } ^ { clock_dut, red_dut, yellow_dut, green_dut } ^ { clock_ref, red_ref, yellow_ref, green_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (clock_ref !== ( clock_ref ^ clock_dut ^ clock_ref )) + begin if (stats1.errors_clock == 0) stats1.errortime_clock = $time; + stats1.errors_clock = stats1.errors_clock+1'b1; end + if (red_ref !== ( red_ref ^ red_dut ^ red_ref )) + begin if (stats1.errors_red == 0) stats1.errortime_red = $time; + stats1.errors_red = stats1.errors_red+1'b1; end + if (yellow_ref !== ( yellow_ref ^ yellow_dut ^ yellow_ref )) + begin if (stats1.errors_yellow == 0) stats1.errortime_yellow = $time; + stats1.errors_yellow = stats1.errors_yellow+1'b1; end + if (green_ref !== ( green_ref ^ green_dut ^ green_ref )) + begin if (stats1.errors_green == 0) stats1.errortime_green = $time; + stats1.errors_green = stats1.errors_green+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob034_gaming_machine_billing_program_prompt.txt b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob034_gaming_machine_billing_program_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..961fad148a54c902ec06822a14630ad93f2d50a9 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob034_gaming_machine_billing_program_prompt.txt @@ -0,0 +1,82 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input [9:0] money + - input set + - input boost + - output reg [9:0] remain + - output reg yellow + - output reg red + +The module should implement a gaming machine billing system with +prepaid charging functionality. Assume all sequential logic is triggered +on the positive edge of the clock. The reset input is active low +asynchronous, and should reset the system to initial state. + +The gaming machine billing system should work as follows: +- Prepaid charging: When set signal is active, the money value is added + to the remaining balance (remain) +- Game modes: Normal mode (1 yuan per minute) and boost mode (2 yuan per + minute) +- Default mode: Normal mode (boost = 0) +- Boost mode: Activated when boost = 1, charges 2 yuan per clock cycle +- Balance deduction: Each clock cycle deducts 1 yuan (normal) or 2 yuan + (boost) from remain +- Warning indicators: Yellow light when remain > 0 and < 10, red light + when remain < 1 in normal mode, when remain < 2 in boost mode. +- Continuous charging: Users can recharge during gameplay via set signal + +Gaming machine operation: +- Normal mode: remain decreases by 1 each clock cycle +- Boost mode: remain decreases by 2 each clock cycle +- Charging: When set = 1, money value is added to remain +- Yellow warning: Active when 0 < remain < 10 +- Red warning: Active when remain < 1 for normal mode, remaon < 2 for boost mode + (insufficient funds) +- Warning timing: yellow and red follow their conditions one clock cycle + later; red uses the boost value of that earlier cycle +- Reset behavior: Active low reset to initial state + + +Based on the problem description above, the code below in TopModule has bug, please fix that: +``` +module TopModule +( + input rst_n, + input clk, + input [9:0]money, + input set, + input boost, + output reg[9:0]remain, + output reg yellow, + output reg red +); + always @(*) begin + if (~rst_n) begin + yellow = 1'b0; + red = 1'b0; + end + else begin + yellow = (remain < 10) && remain; + red = boost ? (remain < 2) : (remain < 1); + end + end + + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + remain <= 0; + else if(boost) + remain <= set ? remain+money: + remain<2? remain: + remain-2; + else + remain <= set ? remain+money: + remain<1? remain: + remain-1; + end +endmodule +``` diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob034_gaming_machine_billing_program_ref.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob034_gaming_machine_billing_program_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..84d31ece4f078c42fce9b0073d9ff77540935471 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob034_gaming_machine_billing_program_ref.sv @@ -0,0 +1,36 @@ + +module RefModule +( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input [9:0]money, + input set, + input boost, + output reg[9:0]remain, + output reg yellow, + output reg red +); + always@(posedge clk or negedge rst_n) begin + if(~rst_n) begin + yellow <= 0; + red <= 0; + end + else begin + yellow <= remain<10&&remain; + red <= boost? remain<2: remain<1; + end + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + remain <= 0; + else if(boost) + remain <= set ? remain+money: + remain<2? remain: + remain-2; + else + remain <= set ? remain+money: + remain<1? remain: + remain-1; + end +endmodule diff --git a/Verilog Debugging/dataset_debug_zero_shot_timing/Prob034_gaming_machine_billing_program_test.sv b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob034_gaming_machine_billing_program_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..e447ea3b14ac0f37fb8bc116b201da5c61489f87 --- /dev/null +++ b/Verilog Debugging/dataset_debug_zero_shot_timing/Prob034_gaming_machine_billing_program_test.sv @@ -0,0 +1,208 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [9:0] money, + output logic set, + output logic boost + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + money = 0; + set = 0; + boost = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test normal mode charging + set = 1; + money = 10'd50; @(posedge clk); // Charge 50 yuan + set = 0; + @(posedge clk); + + // Test normal mode consumption (1 yuan per cycle) + boost = 0; + repeat(20) @(posedge clk); // Consume 20 yuan + + // Test charging during gameplay + boost = 0; + set = 1; + money = 10'd20; @(posedge clk); // Charge while playing + set = 0; + @(posedge clk); + + boost = 1; + repeat(25) @(posedge clk); // Consume 50 yuan + + // Test insufficient funds in normal mode + boost = 0; + repeat(10) @(posedge clk); // Try to consume more than available + + // Test insufficient funds in boost mode + set = 1; + money = 10'd3; @(posedge clk); // Charge small amount + set = 0; + @(posedge clk); + boost = 1; + repeat(5) @(posedge clk); // Try to consume in boost mode + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + set <= $random; + boost <= $random; + money <= $random % 1024; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_remain; + int errortime_remain; + int errors_yellow; + int errortime_yellow; + int errors_red; + int errortime_red; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [9:0] money; + logic set; + logic boost; + logic [9:0] remain_ref; + logic yellow_ref; + logic red_ref; + logic [9:0] remain_dut; + logic yellow_dut; + logic red_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, money, set, boost, remain_ref, yellow_ref, red_ref, remain_dut, yellow_dut, red_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .money, + .set, + .boost ); + + RefModule good1 ( + .clk, + .rst_n, + .money, + .set, + .boost, + .remain(remain_ref), + .yellow(yellow_ref), + .red(red_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .money, + .set, + .boost, + .remain(remain_dut), + .yellow(yellow_dut), + .red(red_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_remain) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "remain", stats1.errors_remain, stats1.errortime_remain); + else $display("Hint: Output '%s' has no mismatches.", "remain"); + if (stats1.errors_yellow) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "yellow", stats1.errors_yellow, stats1.errortime_yellow); + else $display("Hint: Output '%s' has no mismatches.", "yellow"); + if (stats1.errors_red) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "red", stats1.errors_red, stats1.errortime_red); + else $display("Hint: Output '%s' has no mismatches.", "red"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { remain_ref, yellow_ref, red_ref } === ( { remain_ref, yellow_ref, red_ref } ^ { remain_dut, yellow_dut, red_dut } ^ { remain_ref, yellow_ref, red_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (remain_ref !== ( remain_ref ^ remain_dut ^ remain_ref )) + begin if (stats1.errors_remain == 0) stats1.errortime_remain = $time; + stats1.errors_remain = stats1.errors_remain+1'b1; end + if (yellow_ref !== ( yellow_ref ^ yellow_dut ^ yellow_ref )) + begin if (stats1.errors_yellow == 0) stats1.errortime_yellow = $time; + stats1.errors_yellow = stats1.errors_yellow+1'b1; end + if (red_ref !== ( red_ref ^ red_dut ^ red_ref )) + begin if (stats1.errors_red == 0) stats1.errortime_red = $time; + stats1.errors_red = stats1.errors_red+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_cpu_ip/Prob001_controller_prompt.txt b/Verilog Gen/dataset_cpu_ip/Prob001_controller_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..093c894ffbbf46bbe060f569970504aad24ea9c8 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob001_controller_prompt.txt @@ -0,0 +1,50 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input Opcode (7 bits) + - output ALUSrc + - output MemtoReg + - output RegWrite + - output MemRead + - output MemWrite + - output ALUOp (2 bits) + - output Branch + - output JalrSel + - output RWSel (2 bits) + +The module should implement the controller part for RISC-V. This controller +generates control signals based on the 7-bit opcode field from the instruction. + +The output signals should work as follows: + +- ALUSrc: 0 means the second ALU operand comes from the second register file + (Read data 2); 1 means the second ALU operand is the sign-extended lower + 16 bits of the instruction. Notice the first ALU operand is already the first register file. + ALUSrc is 1 for LW, SW, I-type and JALR, and 0 for every other opcode. + +- MemtoReg: 0 means the value fed to the register Write data input comes from + the ALU; 1 means the value fed to the register Write data input comes from + the data memory. + +- RegWrite: When set, the register on the Write register input is written + with the value on the Write data input. + +- MemRead: Data memory contents designated by the address input are put on + the Read data output. + +- MemWrite: Data memory contents designated by the address input are replaced + by the value on the Write data input. + +- ALUOp: The 2-bit signal encodes the operation type - 00 for LW/SW/AUIPC/JALR, + 01 for Branch, 10 for R-type/I-type, 11 for JAL/LUI. + +- Branch: A control signal indicating that the current instruction may change the program counter (PC). + It is set to 1 for both conditional branch instructions (BR opcodes like beq, bne, etc.) + and for the jump instruction (JAL). + +- JalrSel: 0 means JALR is not taken; 1 means JALR is taken. + +- RWSel: The 2-bit signal selects the write back source - 00 for Register + Write Back, 01 for PC+4 write back (JAL/JALR), 10 for imm-gen write back + (LUI), 11 for PC+imm-gen write back (AUIPC). diff --git a/Verilog Gen/dataset_cpu_ip/Prob001_controller_ref.sv b/Verilog Gen/dataset_cpu_ip/Prob001_controller_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..465431e23a196a40a3949ceddaeaa8364cf24ec5 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob001_controller_ref.sv @@ -0,0 +1,52 @@ + +module RefModule( + + //Input + input logic [6:0] Opcode, //7-bit opcode field from the instruction + + //Outputs + output logic ALUSrc,//0: The second ALU operand comes from the second register file output (Read data 2); + //1: The second ALU operand is the sign-extended, lower 16 bits of the instruction. + output logic MemtoReg, //0: The value fed to the register Write data input comes from the ALU. + //1: The value fed to the register Write data input comes from the data memory. + output logic RegWrite, //The register on the Write register input is written with the value on the Write data input + output logic MemRead, //Data memory contents designated by the address input are put on the Read data output + output logic MemWrite, //Data memory contents designated by the address input are replaced by the value on the Write data input. + output logic [1:0] ALUOp, //00: LW/SW/AUIPC; 01:Branch; 10: Rtype/Itype; 11:JAL/LUI + output logic Branch, //0: branch is not taken; 1: branch is taken + output logic JalrSel, //0: Jalr is not taken; 1: jalr is taken + output logic [1:0] RWSel //00:Register Write Back; 01: PC+4 write back(JAL/JALR); 10: imm-gen write back(LUI); 11: pc+imm-gen write back(AUIPC) +); + + logic [6:0] R_TYPE, LW, SW, RTypeI, BR, JAL, JALR, LUI, AUIPC; + + assign R_TYPE = 7'b0110011; //add,sub,sll,slt,sltu,xor,srl,sra,or,and + assign LW = 7'b0000011; //lb,lh,lw + assign SW = 7'b0100011; //sb,sh,sw + assign RTypeI = 7'b0010011; //addi,ori,andi + + assign BR = 7'b1100011; //beq,bne,blt,bge,bltu,bgeu + + assign JAL = 7'b1101111; + assign JALR = 7'b1100111; + + assign LUI = 7'b0110111; + assign AUIPC = 7'b0010111; + + + + + assign ALUSrc = (Opcode==LW || Opcode==SW || Opcode == RTypeI || Opcode == JALR); + assign MemtoReg = (Opcode==LW); + assign RegWrite = (Opcode==R_TYPE || Opcode==LW || Opcode == RTypeI || Opcode==JAL || Opcode==JALR || Opcode==LUI || Opcode==AUIPC); + assign MemRead = (Opcode==LW); + assign MemWrite = (Opcode==SW); + assign ALUOp[0] = (Opcode==BR || Opcode==JAL || Opcode==LUI); + assign ALUOp[1] = (Opcode==R_TYPE || Opcode==JAL || Opcode==LUI || Opcode==RTypeI); + assign Branch = (Opcode==BR || Opcode==JAL); + assign JalrSel = (Opcode==JALR); + assign RWSel[0] = (Opcode==JALR || Opcode==JAL || Opcode==AUIPC); + assign RWSel[1] = (Opcode==LUI || Opcode==AUIPC); + + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_cpu_ip/Prob001_controller_test.sv b/Verilog Gen/dataset_cpu_ip/Prob001_controller_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..c2694eb6c72e74c4b18ac9cf5b190c90647c7713 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob001_controller_test.sv @@ -0,0 +1,236 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + +module stimulus_gen ( + input clk, + output logic [6:0] Opcode +); + + initial begin + // Test each RISC-V instruction type + // R-Type instruction (add, sub, etc.) + Opcode = 7'b0110011; + repeat(5) @(posedge clk, negedge clk); + + // Load Word (LW) + Opcode = 7'b0000011; + repeat(5) @(posedge clk, negedge clk); + + // Store Word (SW) + Opcode = 7'b0100011; + repeat(5) @(posedge clk, negedge clk); + + // I-Type ALU (addi, ori, etc.) + Opcode = 7'b0010011; + repeat(5) @(posedge clk, negedge clk); + + // Branch (beq, bne, etc.) + Opcode = 7'b1100011; + repeat(5) @(posedge clk, negedge clk); + + // JAL + Opcode = 7'b1101111; + repeat(5) @(posedge clk, negedge clk); + + // JALR + Opcode = 7'b1100111; + repeat(5) @(posedge clk, negedge clk); + + // LUI + Opcode = 7'b0110111; + repeat(5) @(posedge clk, negedge clk); + + // AUIPC + Opcode = 7'b0010111; + repeat(5) @(posedge clk, negedge clk); + + // Test some invalid/random opcodes + repeat(500) @(negedge clk) begin + Opcode <= $random; + end + + #1 $finish; + end + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_ALUSrc; + int errortime_ALUSrc; + int errors_MemtoReg; + int errortime_MemtoReg; + int errors_RegWrite; + int errortime_RegWrite; + int errors_MemRead; + int errortime_MemRead; + int errors_MemWrite; + int errortime_MemWrite; + int errors_ALUOp; + int errortime_ALUOp; + int errors_Branch; + int errortime_Branch; + int errors_JalrSel; + int errortime_JalrSel; + int errors_RWSel; + int errortime_RWSel; + + int clocks; + } stats; + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + logic [6:0] Opcode; + logic ALUSrc_ref; + logic ALUSrc_dut; + logic MemtoReg_ref; + logic MemtoReg_dut; + logic RegWrite_ref; + logic RegWrite_dut; + logic MemRead_ref; + logic MemRead_dut; + logic MemWrite_ref; + logic MemWrite_dut; + logic [1:0] ALUOp_ref; + logic [1:0] ALUOp_dut; + logic Branch_ref; + logic Branch_dut; + logic JalrSel_ref; + logic JalrSel_dut; + logic [1:0] RWSel_ref; + logic [1:0] RWSel_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, Opcode, ALUSrc_ref, ALUSrc_dut, MemtoReg_ref, MemtoReg_dut, RegWrite_ref, RegWrite_dut, MemRead_ref, MemRead_dut, MemWrite_ref, MemWrite_dut, ALUOp_ref, ALUOp_dut, Branch_ref, Branch_dut, JalrSel_ref, JalrSel_dut, RWSel_ref, RWSel_dut); + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + stimulus_gen stim1 ( + .clk, + .Opcode ); + RefModule good1 ( + .Opcode, + .ALUSrc(ALUSrc_ref), + .MemtoReg(MemtoReg_ref), + .RegWrite(RegWrite_ref), + .MemRead(MemRead_ref), + .MemWrite(MemWrite_ref), + .ALUOp(ALUOp_ref), + .Branch(Branch_ref), + .JalrSel(JalrSel_ref), + .RWSel(RWSel_ref) ); + + TopModule top_module1 ( + .Opcode, + .ALUSrc(ALUSrc_dut), + .MemtoReg(MemtoReg_dut), + .RegWrite(RegWrite_dut), + .MemRead(MemRead_dut), + .MemWrite(MemWrite_dut), + .ALUOp(ALUOp_dut), + .Branch(Branch_dut), + .JalrSel(JalrSel_dut), + .RWSel(RWSel_dut) ); + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + if (stats1.errors_ALUSrc) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "ALUSrc", stats1.errors_ALUSrc, stats1.errortime_ALUSrc); + else $display("Hint: Output '%s' has no mismatches.", "ALUSrc"); + if (stats1.errors_MemtoReg) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "MemtoReg", stats1.errors_MemtoReg, stats1.errortime_MemtoReg); + else $display("Hint: Output '%s' has no mismatches.", "MemtoReg"); + if (stats1.errors_RegWrite) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "RegWrite", stats1.errors_RegWrite, stats1.errortime_RegWrite); + else $display("Hint: Output '%s' has no mismatches.", "RegWrite"); + if (stats1.errors_MemRead) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "MemRead", stats1.errors_MemRead, stats1.errortime_MemRead); + else $display("Hint: Output '%s' has no mismatches.", "MemRead"); + if (stats1.errors_MemWrite) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "MemWrite", stats1.errors_MemWrite, stats1.errortime_MemWrite); + else $display("Hint: Output '%s' has no mismatches.", "MemWrite"); + if (stats1.errors_ALUOp) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "ALUOp", stats1.errors_ALUOp, stats1.errortime_ALUOp); + else $display("Hint: Output '%s' has no mismatches.", "ALUOp"); + if (stats1.errors_Branch) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "Branch", stats1.errors_Branch, stats1.errortime_Branch); + else $display("Hint: Output '%s' has no mismatches.", "Branch"); + if (stats1.errors_JalrSel) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "JalrSel", stats1.errors_JalrSel, stats1.errortime_JalrSel); + else $display("Hint: Output '%s' has no mismatches.", "JalrSel"); + if (stats1.errors_RWSel) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "RWSel", stats1.errors_RWSel, stats1.errortime_RWSel); + else $display("Hint: Output '%s' has no mismatches.", "RWSel"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { ALUSrc_ref, MemtoReg_ref, RegWrite_ref, MemRead_ref, MemWrite_ref, ALUOp_ref, Branch_ref, JalrSel_ref, RWSel_ref } === ( { ALUSrc_ref, MemtoReg_ref, RegWrite_ref, MemRead_ref, MemWrite_ref, ALUOp_ref, Branch_ref, JalrSel_ref, RWSel_ref } ^ { ALUSrc_dut, MemtoReg_dut, RegWrite_dut, MemRead_dut, MemWrite_dut, ALUOp_dut, Branch_dut, JalrSel_dut, RWSel_dut } ^ { ALUSrc_ref, MemtoReg_ref, RegWrite_ref, MemRead_ref, MemWrite_ref, ALUOp_ref, Branch_ref, JalrSel_ref, RWSel_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + wait_for_end_of_timestep(); + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (ALUSrc_ref !== ( ALUSrc_ref ^ ALUSrc_dut ^ ALUSrc_ref )) + begin if (stats1.errors_ALUSrc == 0) stats1.errortime_ALUSrc = $time; + stats1.errors_ALUSrc = stats1.errors_ALUSrc+1'b1; end + if (MemtoReg_ref !== ( MemtoReg_ref ^ MemtoReg_dut ^ MemtoReg_ref )) + begin if (stats1.errors_MemtoReg == 0) stats1.errortime_MemtoReg = $time; + stats1.errors_MemtoReg = stats1.errors_MemtoReg+1'b1; end + if (RegWrite_ref !== ( RegWrite_ref ^ RegWrite_dut ^ RegWrite_ref )) + begin if (stats1.errors_RegWrite == 0) stats1.errortime_RegWrite = $time; + stats1.errors_RegWrite = stats1.errors_RegWrite+1'b1; end + if (MemRead_ref !== ( MemRead_ref ^ MemRead_dut ^ MemRead_ref )) + begin if (stats1.errors_MemRead == 0) stats1.errortime_MemRead = $time; + stats1.errors_MemRead = stats1.errors_MemRead+1'b1; end + if (MemWrite_ref !== ( MemWrite_ref ^ MemWrite_dut ^ MemWrite_ref )) + begin if (stats1.errors_MemWrite == 0) stats1.errortime_MemWrite = $time; + stats1.errors_MemWrite = stats1.errors_MemWrite+1'b1; end + if (ALUOp_ref !== ( ALUOp_ref ^ ALUOp_dut ^ ALUOp_ref )) + begin if (stats1.errors_ALUOp == 0) stats1.errortime_ALUOp = $time; + stats1.errors_ALUOp = stats1.errors_ALUOp+1'b1; end + if (Branch_ref !== ( Branch_ref ^ Branch_dut ^ Branch_ref )) + begin if (stats1.errors_Branch == 0) stats1.errortime_Branch = $time; + stats1.errors_Branch = stats1.errors_Branch+1'b1; end + if (JalrSel_ref !== ( JalrSel_ref ^ JalrSel_dut ^ JalrSel_ref )) + begin if (stats1.errors_JalrSel == 0) stats1.errortime_JalrSel = $time; + stats1.errors_JalrSel = stats1.errors_JalrSel+1'b1; end + if (RWSel_ref !== ( RWSel_ref ^ RWSel_dut ^ RWSel_ref )) + begin if (stats1.errors_RWSel == 0) stats1.errortime_RWSel = $time; + stats1.errors_RWSel = stats1.errors_RWSel+1'b1; end + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_cpu_ip/Prob002_alu_prompt.txt b/Verilog Gen/dataset_cpu_ip/Prob002_alu_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..b67808fe3972ad5ed2627100ae3fdf7cf0a9a778 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob002_alu_prompt.txt @@ -0,0 +1,64 @@ + +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input SrcA (32 bits) + - input SrcB (32 bits) + - input Operation (4 bits) + - output ALUResult (32 bits) + +The module should implement a combinational ALU. The 4-bit Operation input +selects exactly one of the following operations. Unless stated otherwise, +shift amounts are taken from the lower 5 bits of SrcB (SrcB[4:0]). For +comparison operations, the result must be 32'd1 when the condition is true +and 32'd0 when false. + + - 4'b0000: AND + ALUResult = SrcA & SrcB + + - 4'b0001: OR + ALUResult = SrcA | SrcB + + - 4'b0010: ADD + ALUResult = SrcA + SrcB (32-bit addition) + + - 4'b0011: XOR + ALUResult = SrcA ^ SrcB + + - 4'b0100: LEFT SHIFT (logical) + ALUResult = SrcA << SrcB[4:0] + + - 4'b0101: RIGHT SHIFT (logical) + ALUResult = SrcA >> SrcB[4:0] (zero-fill) + + - 4'b0110: SUBTRACT + ALUResult = SrcA - SrcB (32-bit subtraction) + + - 4'b0111: RIGHT SHIFT ARITHMETIC + ALUResult = $signed(SrcA) >>> SrcB[4:0] + + - 4'b1000: EQUAL + ALUResult = (SrcA == SrcB) ? 32'd1 : 32'd0 + + - 4'b1001: NOT EQUAL + ALUResult = (SrcA != SrcB) ? 32'd1 : 32'd0 + + - 4'b1010: ALWAYS TRUE + ALUResult = 32'd1 + + - 4'b1100: LESS THAN (signed) + ALUResult = ($signed(SrcA) < $signed(SrcB)) ? 32'd1 : 32'd0 + + - 4'b1101: GREATER OR EQUAL (signed) + ALUResult = ($signed(SrcA) >= $signed(SrcB)) ? 32'd1 : 32'd0 + + - 4'b1110: UNSIGNED LESS THAN + ALUResult = (SrcA < SrcB) ? 32'd1 : 32'd0 + + - 4'b1111: UNSIGNED GREATER OR EQUAL + ALUResult = (SrcA >= SrcB) ? 32'd1 : 32'd0 + +Inputs and outputs are purely combinational; no sequential logic is +required. + diff --git a/Verilog Gen/dataset_cpu_ip/Prob002_alu_ref.sv b/Verilog Gen/dataset_cpu_ip/Prob002_alu_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..9b266f2f6425b1dcd7d465add61f8290df2324a1 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob002_alu_ref.sv @@ -0,0 +1,51 @@ +module RefModule#( + parameter DATA_WIDTH = 32, + parameter OPCODE_LENGTH = 4 + ) + ( + input logic [DATA_WIDTH-1:0] SrcA, + input logic [DATA_WIDTH-1:0] SrcB, + + input logic [OPCODE_LENGTH-1:0] Operation, + output logic[DATA_WIDTH-1:0] ALUResult + ); + + always_comb + begin + case(Operation) + 4'b0000: // AND + ALUResult = SrcA & SrcB; + 4'b0001: // OR + ALUResult = SrcA | SrcB; + 4'b0010: // ADD + ALUResult = $signed(SrcA) + $signed(SrcB); + 4'b0011: // XOR + ALUResult = SrcA ^ SrcB; + 4'b0100: // Left Shift + ALUResult = SrcA << SrcB[4:0]; + 4'b0101: // Right Shift + ALUResult = SrcA >> SrcB[4:0]; + 4'b0110: // Subtract + ALUResult = $signed(SrcA) - $signed(SrcB); + 4'b0111: // Right Shift Arithm + ALUResult = $signed(SrcA) >>> SrcB[4:0]; + + 4'b1000: // Equal + ALUResult = (SrcA == SrcB) ? 1 : 0; + 4'b1001: // Not Equal + ALUResult = (SrcA != SrcB) ? 1 : 0; + 4'b1100: // Less Than + ALUResult = ($signed(SrcA) < $signed(SrcB)) ? 1 : 0; + 4'b1101: // Greater/Equal Than + ALUResult = ($signed(SrcA) >= $signed(SrcB)) ? 1 : 0; + 4'b1110: // Unsigned Less Than + ALUResult = (SrcA < SrcB) ? 1 : 0; + 4'b1111: // Unsigned Greater/Equal Than + ALUResult = (SrcA >= SrcB) ? 1 : 0; + 4'b1010: // Always True, for jal + ALUResult = 1; + default: + ALUResult = 0; + endcase + end +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_cpu_ip/Prob002_alu_test.sv b/Verilog Gen/dataset_cpu_ip/Prob002_alu_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..613347f2d41be4cfd1cd474a8d3fc76822b57084 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob002_alu_test.sv @@ -0,0 +1,229 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + output logic [31:0] SrcA, + output logic [31:0] SrcB, + output logic [3:0] Operation +); + + +// Add two ports to module stimulus_gen: +// output [511:0] wavedrom_title +// output reg wavedrom_enable + + + + function automatic logic [31:0] eqv(input int i); + case (i) + 0: eqv = 32'h0000_0000; + 1: eqv = 32'h0000_0001; + 2: eqv = 32'hffff_ffff; + 3: eqv = 32'h8000_0000; + default: eqv = 32'h7fff_ffff; + endcase + endfunction + + initial begin + // Directed sweep of opcodes with representative operands + SrcA <= 32'h8000_0001; // negative with LSB 1 + SrcB <= 32'h0000_001f; // cover max logical shift amount 31 + Operation <= 4'h0; + @(posedge clk) Operation <= 4'h0; // AND + @(posedge clk) Operation <= 4'h1; // OR + @(posedge clk) Operation <= 4'h2; // ADD + @(posedge clk) Operation <= 4'h3; // XOR (exists in RefModule) + @(posedge clk) Operation <= 4'h4; // SLL + @(posedge clk) Operation <= 4'h5; // SRL + @(posedge clk) Operation <= 4'h6; // SUB + @(posedge clk) Operation <= 4'h7; // SRA + @(posedge clk) Operation <= 4'h8; // EQ + @(posedge clk) Operation <= 4'h9; // NE + @(posedge clk) Operation <= 4'hA; // ALWAYS TRUE (jal) + @(posedge clk) Operation <= 4'hC; // SLT + @(posedge clk) Operation <= 4'hD; // SGE + @(posedge clk) Operation <= 4'hE; // SLTU + @(posedge clk) Operation <= 4'hF; // SGEU + + // Additional directed tests for shifts and signedness + @(posedge clk) begin + SrcA <= 32'hffff_fffe; // -2 + SrcB <= 32'h0000_0001; // shift by 1 + Operation <= 4'h7; // SRA should replicate sign + end + @(posedge clk) begin + SrcA <= 32'h7fff_ffff; // largest positive + SrcB <= 32'h0000_0001; + Operation <= 4'h5; // SRL should zero-fill + end + @(posedge clk) begin + SrcA <= 32'hffff_ffff; // -1 + SrcB <= 32'h0000_001f; // shift by 31 + Operation <= 4'h4; // SLL + end + + // Randomized testing + repeat(1000) @(posedge clk) begin + SrcA <= $urandom; + SrcB <= $urandom; + // Bias ops toward all supported encodings seen in RefModule + unique case ($urandom_range(0,14)) + 0: Operation <= 4'h0; // AND + 1: Operation <= 4'h1; // OR + 2: Operation <= 4'h2; // ADD + 3: Operation <= 4'h3; // XOR + 4: Operation <= 4'h4; // SLL + 5: Operation <= 4'h5; // SRL + 6: Operation <= 4'h6; // SUB + 7: Operation <= 4'h7; // SRA + 8: Operation <= 4'h8; // EQ + 9: Operation <= 4'h9; // NE + 10: Operation <= 4'hA; // ALWAYS TRUE + 11: Operation <= 4'hC; // SLT + 12: Operation <= 4'hD; // SGE + 13: Operation <= 4'hE; // SLTU + 14: Operation <= 4'hF; // SGEU + endcase + end + + // Additional checks: equal operands. The spec defines EQ/NE/SLT/SGE/SLTU/SGEU results for every + // operand pair, but random 32-bit operands are never equal, so the SrcA == SrcB cases were never applied. + // Every listed opcode with SrcA == SrcB, on a few representative values. + for (int i = 0; i < 5; i++) begin + for (int op = 0; op < 16; op++) begin + if (op != 11) @(posedge clk) begin + SrcA <= eqv(i); + SrcB <= eqv(i); + Operation <= op[3:0]; + end + end + end + // Random equal operands and operands differing by +/-1, on the compare opcodes. + repeat(200) @(posedge clk) begin + logic [31:0] r; + r = $urandom; + SrcA <= r; + unique case ($urandom_range(0,2)) + 0: SrcB <= r; + 1: SrcB <= r + 32'd1; + 2: SrcB <= r - 32'd1; + endcase + unique case ($urandom_range(0,5)) + 0: Operation <= 4'h8; + 1: Operation <= 4'h9; + 2: Operation <= 4'hC; + 3: Operation <= 4'hD; + 4: Operation <= 4'hE; + 5: Operation <= 4'hF; + endcase + end + + #1 $finish; + end + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_ALUResult; + int errortime_ALUResult; + + int clocks; + } stats; + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + logic [31:0] SrcA; + logic [31:0] SrcB; + logic [3:0] Operation; + logic [31:0] ALUResult_ref; + logic [31:0] ALUResult_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch ,clk,SrcA,SrcB,Operation,ALUResult_ref,ALUResult_dut ); + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + stimulus_gen stim1 ( + .clk, + .*, + .SrcA, + .SrcB, + .Operation ); + RefModule good1 ( + .SrcA, + .SrcB, + .Operation, + .ALUResult(ALUResult_ref) ); + + TopModule top_module1 ( + .SrcA, + .SrcB, + .Operation, + .ALUResult(ALUResult_dut) ); + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + if (stats1.errors_ALUResult) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "ALUResult", stats1.errors_ALUResult, stats1.errortime_ALUResult); + else $display("Hint: Output '%s' has no mismatches.", "ALUResult"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { ALUResult_ref } === ( { ALUResult_ref } ^ { ALUResult_dut } ^ { ALUResult_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (ALUResult_ref !== ( ALUResult_ref ^ ALUResult_dut ^ ALUResult_ref )) + begin if (stats1.errors_ALUResult == 0) stats1.errortime_ALUResult = $time; + stats1.errors_ALUResult = stats1.errors_ALUResult+1'b1; end + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule + + diff --git a/Verilog Gen/dataset_cpu_ip/Prob003_RegFile_prompt.txt b/Verilog Gen/dataset_cpu_ip/Prob003_RegFile_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..9d3030fd2c1d8f4e7a2ed9b58ddb1a3bd61c727b --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob003_RegFile_prompt.txt @@ -0,0 +1,32 @@ + +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst + - input rg_wrt_en + - input rg_wrt_dest (5 bits) + - input rg_rd_addr1 (5 bits) + - input rg_rd_addr2 (5 bits) + - input rg_wrt_data (32 bits) + - output rg_rd_data1 (32 bits) + - output rg_rd_data2 (32 bits) + +The module should implement a 32-register file with 32-bit wide registers +and two read ports, one write port. The number of registers is 2^5 = 32. + +Behavioral requirements: + - Reset is active-high synchronous. When rst=1 at a rising clock edge, all + registers must be set to 32'd0. + - A write occurs on the rising edge of clk when rg_wrt_en=1. The register + at index rg_wrt_dest is updated with rg_wrt_data. + - The two read outputs are purely combinational and continuously reflect the + current contents of the addressed registers: rg_rd_data1 corresponds to + register at rg_rd_addr1, and rg_rd_data2 corresponds to register at + rg_rd_addr2. + - If a write and read target the same register index in the same cycle, the + read outputs reflect the register contents prior to the clock edge; the + written value is visible after the rising edge that performs the write. + + diff --git a/Verilog Gen/dataset_cpu_ip/Prob003_RegFile_ref.sv b/Verilog Gen/dataset_cpu_ip/Prob003_RegFile_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..652628cf3acdf93f37646240e2438fbec50cf520 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob003_RegFile_ref.sv @@ -0,0 +1,39 @@ +module RefModule#( + // Parameters + parameter DATA_WIDTH = 32, // number of bits in each register + parameter ADDRESS_WIDTH = 5, //number of registers = 2^ADDRESS_WIDTH + parameter NUM_REGS = 32 + ) + ( + // Inputs + input clk, //clock + input rst,//synchronous reset; if it is asserted (rst=1), all registers are reseted to 0 + input rg_wrt_en, //write signal + input [ADDRESS_WIDTH-1:0] rg_wrt_dest, //address of the register that supposed to written into + input [ADDRESS_WIDTH-1:0] rg_rd_addr1, //first address to be read from + input [ADDRESS_WIDTH-1:0] rg_rd_addr2, //second address to be read from + input [DATA_WIDTH-1:0] rg_wrt_data, // data that supposed to be written into the register file + + // Outputs + output logic [DATA_WIDTH-1:0] rg_rd_data1, //content of reg_file[rg_rd_addr1] is loaded into + output logic [DATA_WIDTH-1:0] rg_rd_data2 //content of reg_file[rg_rd_addr2] is loaded into + ); + +integer i; + +logic [DATA_WIDTH-1:0] register_file [NUM_REGS-1:0]; + +always @( posedge clk ) +begin + if( rst == 1'b1 ) + for (i = 0; i < NUM_REGS ; i = i + 1) + register_file [i] <= 0; + else if( rst ==1'b0 && rg_wrt_en ==1'b1 ) begin + register_file [ rg_wrt_dest ] <= rg_wrt_data; + end +end + +assign rg_rd_data1 = register_file[rg_rd_addr1]; +assign rg_rd_data2 = register_file[rg_rd_addr2]; + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_cpu_ip/Prob003_RegFile_test.sv b/Verilog Gen/dataset_cpu_ip/Prob003_RegFile_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..a13de3b0446128d32ac84b74010b6f7b10537495 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob003_RegFile_test.sv @@ -0,0 +1,172 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + +module stimulus_gen ( + input clk, + output logic rst, + output logic rg_wrt_en, + output logic [4:0] rg_wrt_dest, + output logic [4:0] rg_rd_addr1, + output logic [4:0] rg_rd_addr2, + output logic [31:0] rg_wrt_data +); + + initial begin + // Initialize + rst <= 1'b1; rg_wrt_en <= 1'b0; rg_wrt_dest <= '0; rg_rd_addr1 <= '0; rg_rd_addr2 <= '0; rg_wrt_data <= '0; + + @(posedge clk); rst <= 1'b0; + + // After reset, all registers should be zero; probe a few + @(posedge clk); rg_rd_addr1 <= 5'd0; rg_rd_addr2 <= 5'd31; + @(posedge clk); rg_rd_addr1 <= 5'd1; rg_rd_addr2 <= 5'd2; + + // Write to a register and read it on subsequent cycles + @(posedge clk); rg_wrt_en <= 1'b1; rg_wrt_dest <= 5'd3; rg_wrt_data <= 32'hDEADBEEF; + // Same-cycle read-during-write should return old value (pre-write) + rg_rd_addr1 <= 5'd3; rg_rd_addr2 <= 5'd0; + @(posedge clk); rg_wrt_en <= 1'b0; // now value becomes visible + rg_rd_addr1 <= 5'd3; rg_rd_addr2 <= 5'd31; + + // Multiple writes + @(posedge clk); rg_wrt_en <= 1'b1; rg_wrt_dest <= 5'd10; rg_wrt_data <= 32'h12345678; rg_rd_addr1 <= 5'd10; rg_rd_addr2 <= 5'd3; + @(posedge clk); rg_wrt_en <= 1'b1; rg_wrt_dest <= 5'd31; rg_wrt_data <= 32'hCAFEBABE; rg_rd_addr1 <= 5'd31; rg_rd_addr2 <= 5'd10; + @(posedge clk); rg_wrt_en <= 1'b0; rg_rd_addr1 <= 5'd31; rg_rd_addr2 <= 5'd10; + + // Read-after-write same cycle to different ports + @(posedge clk); rg_wrt_en <= 1'b1; rg_wrt_dest <= 5'd15; rg_wrt_data <= 32'h0; rg_rd_addr1 <= 5'd15; rg_rd_addr2 <= 5'd15; + @(posedge clk); rg_wrt_en <= 1'b0; rg_rd_addr1 <= 5'd15; rg_rd_addr2 <= 5'd15; + + // Randomized stress + repeat (500) @(posedge clk) begin + // randomize write enable with ~25% probability + rg_wrt_en <= ($urandom & 8'h3) == 0; + rg_wrt_dest <= $urandom_range(0,31); + rg_wrt_data <= $urandom; + rg_rd_addr1 <= $urandom_range(0,31); + rg_rd_addr2 <= $urandom_range(0,31); + end + + #1 $finish; + end + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_rd1; + int errortime_rd1; + int errors_rd2; + int errortime_rd2; + + int clocks; + } stats; + + stats stats1; + + reg clk=0; + initial forever + #5 clk = ~clk; + + logic rst; + logic rg_wrt_en; + logic [4:0] rg_wrt_dest; + logic [4:0] rg_rd_addr1; + logic [4:0] rg_rd_addr2; + logic [31:0] rg_wrt_data; + + logic [31:0] rg_rd_data1_ref; + logic [31:0] rg_rd_data2_ref; + logic [31:0] rg_rd_data1_dut; + logic [31:0] rg_rd_data2_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch ,clk,rst,rg_wrt_en,rg_wrt_dest,rg_rd_addr1,rg_rd_addr2,rg_wrt_data,rg_rd_data1_ref,rg_rd_data2_ref,rg_rd_data1_dut,rg_rd_data2_dut ); + end + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + assign tb_match = ( { rg_rd_data1_ref, rg_rd_data2_ref } === + ( { rg_rd_data1_ref, rg_rd_data2_ref } ^ { rg_rd_data1_dut, rg_rd_data2_dut } ^ { rg_rd_data1_ref, rg_rd_data2_ref } ) ); + + stimulus_gen stim1 ( + .clk, + .rst, + .rg_wrt_en, + .rg_wrt_dest, + .rg_rd_addr1, + .rg_rd_addr2, + .rg_wrt_data ); + + RefModule good1 ( + .clk, + .rst, + .rg_wrt_en, + .rg_wrt_dest, + .rg_rd_addr1, + .rg_rd_addr2, + .rg_wrt_data, + .rg_rd_data1(rg_rd_data1_ref), + .rg_rd_data2(rg_rd_data2_ref) ); + + TopModule top_module1 ( + .clk, + .rst, + .rg_wrt_en, + .rg_wrt_dest, + .rg_rd_addr1, + .rg_rd_addr2, + .rg_wrt_data, + .rg_rd_data1(rg_rd_data1_dut), + .rg_rd_data2(rg_rd_data2_dut) ); + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (rg_rd_data1_ref !== ( rg_rd_data1_ref ^ rg_rd_data1_dut ^ rg_rd_data1_ref )) begin + if (stats1.errors_rd1 == 0) stats1.errortime_rd1 = $time; + stats1.errors_rd1 = stats1.errors_rd1 + 1'b1; + end + if (rg_rd_data2_ref !== ( rg_rd_data2_ref ^ rg_rd_data2_dut ^ rg_rd_data2_ref )) begin + if (stats1.errors_rd2 == 0) stats1.errortime_rd2 = $time; + stats1.errors_rd2 = stats1.errors_rd2 + 1'b1; + end + end + + final begin + if (stats1.errors_rd1) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rg_rd_data1", stats1.errors_rd1, stats1.errortime_rd1); + else $display("Hint: Output '%s' has no mismatches.", "rg_rd_data1"); + if (stats1.errors_rd2) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rg_rd_data2", stats1.errors_rd2, stats1.errortime_rd2); + else $display("Hint: Output '%s' has no mismatches.", "rg_rd_data2"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule + + diff --git a/Verilog Gen/dataset_cpu_ip/Prob004_Branch_Unit_prompt.txt b/Verilog Gen/dataset_cpu_ip/Prob004_Branch_Unit_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..d274e40ee5432542d51e77dc84faf51f1ca05e8c --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob004_Branch_Unit_prompt.txt @@ -0,0 +1,36 @@ + +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input Cur_PC (9 bits) + - input Imm (32 bits) + - input JalrSel + - input Branch + - input AluResult (32 bits) + - output PC_Imm (32 bits) + - output PC_Four (32 bits) + - output BrPC (32 bits) + - output PcSel + +The module should be purely combinational and implement the basic branch +unit functions: + + - PC_Imm: The PC-relative target computed as Cur_PC + Imm. + Treat Cur_PC as an unsigned PC value zero-extended to 32 bits before + addition. Imm is a 32-bit signed immediate; perform a 32-bit addition. + + - PC_Four: The sequential PC computed as Cur_PC + 32'd4 (with Cur_PC + zero-extended to 32 bits prior to the addition). + + - BrPC: The selected branch/jump target address. First compute + Branch_Sel = Branch && AluResult[0]. If JalrSel=1, choose AluResult + (e.g., JALR target). Otherwise, if Branch_Sel=1, choose PC_Imm + (e.g., branch or JAL target). If neither JalrSel nor Branch_Sel is + asserted, set BrPC to 32'd0. + + - PcSel: Set to 1 when a control-flow change is taken; otherwise 0. + For this problem, a branch is taken only when Branch=1 and AluResult[0]=1. + JALR is taken whenever JalrSel=1. Therefore, PcSel = JalrSel || + (Branch && AluResult[0]). + diff --git a/Verilog Gen/dataset_cpu_ip/Prob004_Branch_Unit_ref.sv b/Verilog Gen/dataset_cpu_ip/Prob004_Branch_Unit_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..dee1936f3970ba80dd4f8321db5d29f2dabc5cca --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob004_Branch_Unit_ref.sv @@ -0,0 +1,26 @@ +module RefModule + #(parameter PC_W = 9) + (input logic [PC_W-1:0] Cur_PC, + input logic [31:0] Imm, + input logic JalrSel, + input logic Branch, + input logic [31:0] AluResult, + output logic [31:0] PC_Imm, + output logic [31:0] PC_Four, + output logic [31:0] BrPC, + output logic PcSel); + + logic Branch_Sel; + logic [31:0] PC_Full; + + assign PC_Full = {23'b0, Cur_PC}; + assign PC_Imm = PC_Full + Imm; + assign PC_Four = PC_Full + 32'b100; + + assign Branch_Sel = Branch && AluResult[0]; // 0:Branch is taken; 1:Branch is not taken + + assign BrPC = (JalrSel) ? AluResult : // Jalr -> AluResult + (Branch_Sel) ? PC_Imm : 32'b0; // Branch/Jal -> PC+Imm // Otherwise, BrPC value is not important + assign PcSel = JalrSel || Branch_Sel; // 1:branch (b/jal/jalr) is taken; 0:branch is not taken(choose pc+4) + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_cpu_ip/Prob004_Branch_Unit_test.sv b/Verilog Gen/dataset_cpu_ip/Prob004_Branch_Unit_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..9df3d9b154d9d270a53c18baf046dc4c686a4b82 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob004_Branch_Unit_test.sv @@ -0,0 +1,167 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + output logic [8:0] Cur_PC, + output logic [31:0] Imm, + output logic JalrSel, + output logic Branch, + output logic [31:0] AluResult +); + + initial begin + // Init + Cur_PC <= '0; Imm <= '0; JalrSel <= 1'b0; Branch <= 1'b0; AluResult <= '0; + + // Directed: basic PC increments + @(posedge clk); Cur_PC <= 9'd0; Imm <= 32'sd16; Branch <= 1'b0; JalrSel <= 1'b0; AluResult <= 32'hAAAA_0001; // AluResult[0]=1 for branch select tests later + @(posedge clk); Cur_PC <= 9'd4; Imm <= -32'sd8; Branch <= 1'b0; JalrSel <= 1'b0; + @(posedge clk); Cur_PC <= 9'd255; Imm <= 32'sd4; Branch <= 1'b0; JalrSel <= 1'b0; + + // Directed: Branch behavior depends on Branch && AluResult[0] + // Case: Branch=1 but AluResult[0]=0 -> not taken + @(posedge clk); Cur_PC <= 9'd100; Imm <= 32'sd20; Branch <= 1'b1; JalrSel <= 1'b0; AluResult <= 32'h1234_5678; // LSB 0 + // Case: Branch=1 and AluResult[0]=1 -> taken to PC_Imm + @(posedge clk); Cur_PC <= 9'd100; Imm <= -32'sd12; Branch <= 1'b1; JalrSel <= 1'b0; AluResult <= 32'h1234_5679; // LSB 1 + + // Directed: JALR selection overrides branch path; BrPC = AluResult + @(posedge clk); Cur_PC <= 9'd5; Imm <= 32'sd100; Branch <= 1'b1; JalrSel <= 1'b1; AluResult <= 32'h8000_0100; + + // Randomized stress + repeat (500) @(posedge clk) begin + Cur_PC <= $urandom_range(0, (1<<9)-1); + Imm <= $urandom; + JalrSel <= $urandom_range(0,1); + Branch <= $urandom_range(0,1); + AluResult<= $urandom; + end + + #1 $finish; + end + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_PC_Imm; + int errortime_PC_Imm; + int errors_PC_Four; + int errortime_PC_Four; + int errors_BrPC; + int errortime_BrPC; + int errors_PcSel; + int errortime_PcSel; + + int clocks; + } stats; + + stats stats1; + + reg clk=0; + initial forever + #5 clk = ~clk; + + logic [8:0] Cur_PC; + logic [31:0] Imm; + logic JalrSel; + logic Branch; + logic [31:0] AluResult; + + logic [31:0] PC_Imm_ref, PC_Four_ref, BrPC_ref; logic PcSel_ref; + logic [31:0] PC_Imm_dut, PC_Four_dut, BrPC_dut; logic PcSel_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch ,clk,Cur_PC,Imm,JalrSel,Branch,AluResult,PC_Imm_ref,PC_Imm_dut,PC_Four_ref,PC_Four_dut,BrPC_ref,BrPC_dut,PcSel_ref,PcSel_dut ); + end + + wire tb_match; // Verification (vector compare) + wire tb_mismatch = ~tb_match; + assign tb_match = ( { PC_Imm_ref, PC_Four_ref, BrPC_ref, PcSel_ref } === + ( { PC_Imm_ref, PC_Four_ref, BrPC_ref, PcSel_ref } ^ { PC_Imm_dut, PC_Four_dut, BrPC_dut, PcSel_dut } ^ { PC_Imm_ref, PC_Four_ref, BrPC_ref, PcSel_ref } ) ); + + stimulus_gen stim1 ( + .clk, + .Cur_PC, + .Imm, + .JalrSel, + .Branch, + .AluResult ); + + RefModule good1 ( + .Cur_PC, + .Imm, + .JalrSel, + .Branch, + .AluResult, + .PC_Imm(PC_Imm_ref), + .PC_Four(PC_Four_ref), + .BrPC(BrPC_ref), + .PcSel(PcSel_ref) ); + + TopModule top_module1 ( + .Cur_PC, + .Imm, + .JalrSel, + .Branch, + .AluResult, + .PC_Imm(PC_Imm_dut), + .PC_Four(PC_Four_dut), + .BrPC(BrPC_dut), + .PcSel(PcSel_dut) ); + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (PC_Imm_ref !== ( PC_Imm_ref ^ PC_Imm_dut ^ PC_Imm_ref )) begin + if (stats1.errors_PC_Imm == 0) stats1.errortime_PC_Imm = $time; stats1.errors_PC_Imm++; + end + if (PC_Four_ref !== ( PC_Four_ref ^ PC_Four_dut ^ PC_Four_ref )) begin + if (stats1.errors_PC_Four == 0) stats1.errortime_PC_Four = $time; stats1.errors_PC_Four++; + end + if (BrPC_ref !== ( BrPC_ref ^ BrPC_dut ^ BrPC_ref )) begin + if (stats1.errors_BrPC == 0) stats1.errortime_BrPC = $time; stats1.errors_BrPC++; + end + if (PcSel_ref !== ( PcSel_ref ^ PcSel_dut ^ PcSel_ref )) begin + if (stats1.errors_PcSel == 0) stats1.errortime_PcSel = $time; stats1.errors_PcSel++; + end + end + + final begin + if (stats1.errors_PC_Imm) $display("Hint: Output '%s' has %0d mismatches. First mismatch at %0d.", "PC_Imm", stats1.errors_PC_Imm, stats1.errortime_PC_Imm); else $display("Hint: Output '%s' has no mismatches.", "PC_Imm"); + if (stats1.errors_PC_Four) $display("Hint: Output '%s' has %0d mismatches. First mismatch at %0d.", "PC_Four", stats1.errors_PC_Four, stats1.errortime_PC_Four); else $display("Hint: Output '%s' has no mismatches.", "PC_Four"); + if (stats1.errors_BrPC) $display("Hint: Output '%s' has %0d mismatches. First mismatch at %0d.", "BrPC", stats1.errors_BrPC, stats1.errortime_BrPC); else $display("Hint: Output '%s' has no mismatches.", "BrPC"); + if (stats1.errors_PcSel) $display("Hint: Output '%s' has %0d mismatches. First mismatch at %0d.", "PcSel", stats1.errors_PcSel, stats1.errortime_PcSel); else $display("Hint: Output '%s' has no mismatches.", "PcSel"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule + + diff --git a/Verilog Gen/dataset_cpu_ip/Prob005_ALU_Controller_prompt.txt b/Verilog Gen/dataset_cpu_ip/Prob005_ALU_Controller_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..0a10a1a84d5fda6df9f4f2d8894771987764959b --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob005_ALU_Controller_prompt.txt @@ -0,0 +1,36 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input ALUOp (2 bits) + - input Funct7 (7 bits) + - input Funct3 (3 bits) + - output Operation (4 bits) + +The module should implement an RISC-V ALU control decoder. It is purely +combinational and generates a 4-bit Operation code from ALUOp, Funct3, +and Funct7 to select the arithmetic/logic/shift/compare behavior in the ALU. + +Operation is decoded as follows: + +| ALUOp | Funct3 | Funct7 | Operation | +|-------|--------|--------|-----------| +| 00 | any | any | 0010 | +| 01 | 000, 010 or 011 | any | 1000 | +| 01 | 001 | any | 1001 | +| 01 | 100 | any | 1100 | +| 01 | 101 | any | 1101 | +| 01 | 110 | any | 1110 | +| 01 | 111 | any | 1111 | +| 10 | 000 | 0100000 | 0110 | +| 10 | 000 | any other value | 0010 | +| 10 | 001 | any | 0100 | +| 10 | 010 | any | 1100 | +| 10 | 011 | any | 1110 | +| 10 | 100 | any | 0011 | +| 10 | 101 | 0000000 | 0101 | +| 10 | 101 | 0100000 | 0111 | +| 10 | 101 | any other value | 0000 | +| 10 | 110 | any | 0001 | +| 10 | 111 | any | 0000 | +| 11 | any | any | 1010 | diff --git a/Verilog Gen/dataset_cpu_ip/Prob005_ALU_Controller_ref.sv b/Verilog Gen/dataset_cpu_ip/Prob005_ALU_Controller_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..11c7bd08e2002b75bee271d03ccdf38db65fab82 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob005_ALU_Controller_ref.sv @@ -0,0 +1,45 @@ +module RefModule( + + //Inputs + input logic [1:0] ALUOp, // 2-bit opcode field from the Controller--00: LW/SW/AUIPC; 01:Branch; 10: Rtype/Itype; 11:JAL/LUI + input logic [6:0] Funct7, // bits 25 to 31 of the instruction + input logic [2:0] Funct3, // bits 12 to 14 of the instruction + + //Output + output logic [3:0] Operation //operation selection for ALU +); + + assign Operation[0] = ((ALUOp==2'b10) && (Funct3==3'b110)) || // R\I-or + ((ALUOp==2'b10) && (Funct3==3'b100)) || // R\I-xor + ((ALUOp==2'b10) && (Funct3==3'b101) && (Funct7==7'b0000000)) || // R\I->> + ((ALUOp==2'b10) && (Funct3==3'b101) && (Funct7==7'b0100000)) || // R\I->>> + ((ALUOp==2'b01) && (Funct3==3'b001)) || // BNE + ((ALUOp==2'b01) && (Funct3==3'B101)) || // BGE + ((ALUOp==2'b01) && (Funct3==3'b111)); // BGEU + + assign Operation[1] = (ALUOp==2'b00) || // SW\SW\AUIPC\JALR-add + ((ALUOp==2'b10) && (Funct3==3'b000)) || // R\I-add + ((ALUOp==2'b10) && (Funct3==3'b100)) || // R\I-xor + ((ALUOp==2'b10) && (Funct7==7'b0100000) && (Funct3==3'b000)) || // R-sub + ((ALUOp==2'b10) && (Funct3==3'b101) && (Funct7==7'b0100000)) || // R\I->>> + (ALUOp==2'b11) || // JAL\LUI: return1 + ((ALUOp==2'b10) && (Funct3==3'b011)) || // SLTIU, SLTU: > + ((ALUOp==2'b10) && (Funct3==3'b101) && (Funct7==7'b0100000)) || // R\I->>> + ((ALUOp==2'b10) && (Funct3==3'b001)) || // R\I-<< + ((ALUOp==2'b10) && (Funct7==7'b0100000) && (Funct3==3'b000)) || // R-sub + ((ALUOp==2'b10) && (Funct3==3'b010)) || // R\I-< + ((ALUOp==2'b10) && (Funct3==3'b011)) || // SLTIU, SLTU: ADD regardless of functs + drive(2'b00, 3'b000, 7'b0000000); + drive(2'b00, 3'b101, 7'b0100000); + + // Branch class ALUOp=01 (BEQ/BNE/BLT/BGE/BLTU/BGEU) + drive(2'b01, 3'b000, 7'b0000000); // BEQ + drive(2'b01, 3'b001, 7'b0000000); // BNE + drive(2'b01, 3'b100, 7'b0000000); // BLT + drive(2'b01, 3'b101, 7'b0000000); // BGE + drive(2'b01, 3'b110, 7'b0000000); // BLTU + drive(2'b01, 3'b111, 7'b0000000); // BGEU + + // R/I class ALUOp=10 covering all Funct3/Funct7 combos + // ADD/SUB + drive(2'b10, 3'b000, 7'b0000000); // ADD/ADDI + drive(2'b10, 3'b000, 7'b0100000); // SUB + // AND/OR/XOR + drive(2'b10, 3'b111, 7'b0000000); // AND/ANDI + drive(2'b10, 3'b110, 7'b0000000); // OR/ORI + drive(2'b10, 3'b100, 7'b0000000); // XOR/XORI + // Shifts + drive(2'b10, 3'b001, 7'b0000000); // SLL/SLLI + drive(2'b10, 3'b101, 7'b0000000); // SRL/SRLI + drive(2'b10, 3'b101, 7'b0100000); // SRA/SRAI + // Set-less-than + drive(2'b10, 3'b010, 7'b0000000); // SLT/SLTI + drive(2'b10, 3'b011, 7'b0000000); // SLTU/SLTIU + + // Jumps/LUI group ALUOp=11 -> ALWAYS_TRUE in our encoding + drive(2'b11, 3'b000, 7'b0000000); + drive(2'b11, 3'b101, 7'b0100000); + + // Random sweep + repeat (500) begin + ALUOp <= $urandom_range(0,3); + Funct3 <= $urandom_range(0,7); + // Only two legal values of Funct7 for the ambiguous fields (0000000 or 0100000); + // pick randomly between them, otherwise use random for variety. + if (Funct3 == 3'b101 || Funct3 == 3'b000) + Funct7 <= ($urandom & 1) ? 7'b0100000 : 7'b0000000; + else + Funct7 <= $urandom; + @(posedge clk); + end + + #1 $finish; + end + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_Operation; + int errortime_Operation; + + int clocks; + } stats; + + stats stats1; + + reg clk=0; + initial forever + #5 clk = ~clk; + + logic [1:0] ALUOp; + logic [6:0] Funct7; + logic [2:0] Funct3; + logic [3:0] Operation_ref; + logic [3:0] Operation_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch ,clk,ALUOp,Funct7,Funct3,Operation_ref,Operation_dut ); + end + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + assign tb_match = ( { Operation_ref } === ( { Operation_ref } ^ { Operation_dut } ^ { Operation_ref } ) ); + + stimulus_gen stim1 ( + .clk, + .ALUOp, + .Funct7, + .Funct3 ); + + RefModule good1 ( + .ALUOp, + .Funct7, + .Funct3, + .Operation(Operation_ref) ); + + TopModule top_module1 ( + .ALUOp, + .Funct7, + .Funct3, + .Operation(Operation_dut) ); + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (Operation_ref !== ( Operation_ref ^ Operation_dut ^ Operation_ref )) begin + if (stats1.errors_Operation == 0) stats1.errortime_Operation = $time; + stats1.errors_Operation = stats1.errors_Operation + 1'b1; + end + end + + final begin + if (stats1.errors_Operation) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "Operation", stats1.errors_Operation, stats1.errortime_Operation); + else $display("Hint: Output '%s' has no mismatches.", "Operation"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule + + diff --git a/Verilog Gen/dataset_cpu_ip/Prob006_PC_REG_prompt.txt b/Verilog Gen/dataset_cpu_ip/Prob006_PC_REG_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..d90048c05025f026a21030b26d8f9999c4881324 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob006_PC_REG_prompt.txt @@ -0,0 +1,44 @@ +I would like you to implement a module named TopModule with the following +interface. + + - input clk + - input rst + - input stall (6 bits) + - input flush + - input new_pc (32 bits) + - input branch_flag_i + - input branch_target_address_i (32 bits) + - output pc (32 bits) + - output ce + +Define the macros `RegBus` and `InstAddrBus` used by the skeleton as `31:0` in TopModule.sv; nothing else defines them. + +This module is responsible for updating the program counter (PC) value each +clock cycle to control the CPU instruction fetch address. It changes the PC +accordingly during reset, stall, flush, or branch jump operations. + +The module behavior is as follows: + +- When ce (chip disenable) is invalid (0), pc is cleared to zero at the next clock edge. This + typically occurs during reset or system startup. + +- When reset is active (rst == 1): disable instruction fetch by setting ce = + 0. + +- After reset ends (rst == 0): enable instruction fetch by setting ce = 1. + ce changes only at a clock edge. + +- The stall signal has 6 bits, where stall[0] represents whether to fetch a + new instruction. When stall[0] is 1, do not fetch a new instruction: + pc holds, and a branch in that cycle is not taken. + +- The flush signal indicates pipeline flush (e.g., when branch prediction is + wrong). When flush is asserted, the PC should be updated to new_pc, + even when stall[0] is 1. + +- When branch_flag_i indicates a branch jump has occurred, update the PC to + branch_target_address_i. + +- Otherwise, pc advances by 4 each clock cycle. + +Assume all sequential logic is triggered on the positive edge of the clock. diff --git a/Verilog Gen/dataset_cpu_ip/Prob006_PC_REG_ref.sv b/Verilog Gen/dataset_cpu_ip/Prob006_PC_REG_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..6a3f3b40579028cd2fb330f7c411747d89b2e7d7 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob006_PC_REG_ref.sv @@ -0,0 +1,45 @@ + +module RefModule( + + input wire clk, + input wire rst, + + //Information from the control module + input wire[5:0] stall, + input wire flush, + input wire[`RegBus] new_pc, + + //Information from the decode stage + input wire branch_flag_i, + input wire[`RegBus] branch_target_address_i, + + output reg[`InstAddrBus] pc, + output reg ce + +); + + always @ (posedge clk) begin + if (ce == `ChipDisable) begin + pc <= 32'h00000000; + end else begin + if(flush == 1'b1) begin + pc <= new_pc; + end else if(stall[0] == `NoStop) begin + if(branch_flag_i == `Branch) begin + pc <= branch_target_address_i; + end else begin + pc <= pc + 4'h4; + end + end + end + end + + always @ (posedge clk) begin + if (rst == `RstEnable) begin + ce <= `ChipDisable; + end else begin + ce <= `ChipEnable; + end + end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_cpu_ip/Prob006_PC_REG_test.sv b/Verilog Gen/dataset_cpu_ip/Prob006_PC_REG_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..be033ca32d8fafcb888477f97849ebeb9014207f --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob006_PC_REG_test.sv @@ -0,0 +1,238 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 +`define RstEnable 1'b1 +`define RstDisable 1'b0 +`define NoStop 1'b0 +`define RegBus 31:0 +`define InstAddrBus 31:0 +`define Branch 1'b1 +`define ChipDisable 1'b0 +`define ChipEnable 1'b1 + +module stimulus_gen ( + input clk, + // === Start your code here === + output reg rst, + output reg [5:0] stall, + output reg flush, + output reg [31:0] new_pc, + output reg branch_flag_i, + output reg [31:0] branch_target_address_i + // === End your code here === +); + + + initial begin + // Initialize all signals + rst = `RstEnable; + stall = 6'b0; + flush = 1'b0; + new_pc = 32'h0; + branch_flag_i = 1'b0; + branch_target_address_i = 32'h0; + + // Test reset - hold reset for a few cycles + @(posedge clk); + @(posedge clk); + @(posedge clk); + + // Release reset + rst = `RstDisable; + @(posedge clk); + + // Test normal PC increment (should increment by 4 each cycle) + stall = 6'b0; + flush = 1'b0; + branch_flag_i = 1'b0; + repeat(10) @(posedge clk); + + // Test stall - PC should not increment when stall[0] = 1 + stall = 6'b000001; // stall[0] = 1 + repeat(5) @(posedge clk); + stall = 6'b0; + repeat(5) @(posedge clk); + + // Test flush - PC should jump to new_pc + new_pc = 32'h00001000; + flush = 1'b1; + @(posedge clk); + flush = 1'b0; + repeat(5) @(posedge clk); + + // Test branch - PC should jump to branch_target_address_i + stall = 6'b0; + branch_target_address_i = 32'h00002000; + branch_flag_i = `Branch; + @(posedge clk); + branch_flag_i = 1'b0; + repeat(5) @(posedge clk); + + + // Test stall with branch - stall should prevent PC update + branch_target_address_i = 32'h00005000; + branch_flag_i = `Branch; + stall = 6'b000001; // stall[0] = 1 + @(posedge clk); + stall = 6'b0; + branch_flag_i = 1'b0; + repeat(5) @(posedge clk); + + // Random testing + repeat(1000) @(posedge clk) begin + stall <= $urandom; + flush <= ($urandom % 20 == 0) ? 1'b1 : 1'b0; + new_pc <= $urandom; + branch_flag_i <= ($urandom % 15 == 0) ? `Branch : 1'b0; + branch_target_address_i <= $urandom; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_pc; + int errortime_pc; + int errors_ce; + int errortime_ce; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic [5:0] stall; + logic flush; + logic [31:0] new_pc; + logic branch_flag_i; + logic [31:0] branch_target_address_i; + logic [31:0] pc_ref; + logic [31:0] pc_dut; + logic ce_ref; + logic ce_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, stall, flush, new_pc, branch_flag_i, branch_target_address_i, pc_ref, pc_dut, ce_ref, ce_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk(clk), + .rst(rst), + .stall(stall), + .flush(flush), + .new_pc(new_pc), + .branch_flag_i(branch_flag_i), + .branch_target_address_i(branch_target_address_i) + ); + + RefModule good1 ( + .clk(clk), + .rst(rst), + .stall(stall), + .flush(flush), + .new_pc(new_pc), + .branch_flag_i(branch_flag_i), + .branch_target_address_i(branch_target_address_i), + .pc(pc_ref), + .ce(ce_ref) + ); + + TopModule top_module1 ( + .clk(clk), + .rst(rst), + .stall(stall), + .flush(flush), + .new_pc(new_pc), + .branch_flag_i(branch_flag_i), + .branch_target_address_i(branch_target_address_i), + .pc(pc_dut), + .ce(ce_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_pc) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "pc", stats1.errors_pc, stats1.errortime_pc); + else $display("Hint: Output '%s' has no mismatches.", "pc"); + if (stats1.errors_ce) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "ce", stats1.errors_ce, stats1.errortime_ce); + else $display("Hint: Output '%s' has no mismatches.", "ce"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { pc_ref, ce_ref } === ( { pc_ref, ce_ref } ^ { pc_dut, ce_dut } ^ { pc_ref, ce_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (pc_ref !== ( pc_ref ^ pc_dut ^ pc_ref )) + begin + if (stats1.errors_pc == 0) stats1.errortime_pc = $time; + stats1.errors_pc = stats1.errors_pc+1'b1; + end + if (ce_ref !== ( ce_ref ^ ce_dut ^ ce_ref )) + begin + if (stats1.errors_ce == 0) stats1.errortime_ce = $time; + stats1.errors_ce = stats1.errors_ce+1'b1; + end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_cpu_ip/Prob007_div_prompt.txt b/Verilog Gen/dataset_cpu_ip/Prob007_div_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..60c2edfbc854c120d01143657e2bd7c41317d7a3 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob007_div_prompt.txt @@ -0,0 +1,57 @@ +I would like you to implement a module named TopModule with the following +interface. + + - input clk + - input rst + - input signed_div_i + - input opdata1_i (32 bits) + - input opdata2_i (32 bits) + - input start_i + - input annul_i + - output result_o (64 bits) + - output ready_o + +This module implements a division unit that receives two 32-bit operands +(dividend and divisor) and calculates their quotient and remainder using a +hardware algorithm based on shifting and subtraction. + +The module behavior is as follows: + +- When signed_div_i is 1, the division is signed; when it is 0, the division + is unsigned. For signed division, the module should automatically perform + two's complement conversion on the operands as needed. Result signs follow + signed_div_i, opdata1_i and opdata2_i at the clock edge before ready_o + rises: the quotient is negated if the operand signs differ, the + remainder if opdata1_i is negative. + +- The division operation is initiated when start_i is asserted (start_i = 1) + while idle; start_i is otherwise ignored. start_i must stay 1 until + ready_o = 1: if it is 0 on the edge where the result would appear, the + result is discarded and the module returns to idle. + +- During calculation, the module uses a hardware algorithm that performs + sequential shifting and subtraction operations to compute the quotient and + remainder. + +- If opdata2_i is 0, ready_o rises on the 2nd rising edge after the edge that + samples start_i = 1, with result_o = 0. + +- The annul_i signal can be used to cancel an ongoing division operation + mid-calculation. When annul_i is asserted, the current calculation should + be terminated. + +- The result_o output is a 64-bit value where the upper 32 bits contain the + remainder and the lower 32 bits contain the quotient: {remainder, quotient}. + +- The ready_o signal indicates when the division result is ready: ready_o = 1 + means the result is ready and valid, ready_o = 0 means the result is not + ready yet. ready_o rises on the 34th rising edge after the edge that samples + start_i = 1; ready_o and result_o then hold while start_i stays 1 and return + to 0 on the first edge that samples start_i = 0. result_o is 0 whenever + ready_o is 0. + +- When reset is active (rst == 1), the module should initialize to an idle + state. + +Assume all sequential logic is triggered on the positive edge of the clock. +The reset signal is active high (rst == 1 means reset is active). diff --git a/Verilog Gen/dataset_cpu_ip/Prob007_div_ref.sv b/Verilog Gen/dataset_cpu_ip/Prob007_div_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..28a5eb75bd052becb8d8c0cb3ef56ecb2ee0229b --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob007_div_ref.sv @@ -0,0 +1,99 @@ +module RefModule( + + input wire clk, + input wire rst, + + input wire signed_div_i, + input wire[31:0] opdata1_i, + input wire[31:0] opdata2_i, + input wire start_i, + input wire annul_i, + + output reg[63:0] result_o, + output reg ready_o +); + + wire[32:0] div_temp; + reg[5:0] cnt; + reg[64:0] dividend; + reg[1:0] state; + reg[31:0] divisor; + reg[31:0] temp_op1; + reg[31:0] temp_op2; + + assign div_temp = {1'b0,dividend[63:32]} - {1'b0,divisor}; + + always @ (posedge clk) begin + if (rst == `RstEnable) begin + state <= `DivFree; + ready_o <= `DivResultNotReady; + result_o <= {`ZeroWord,`ZeroWord}; + end else begin + case (state) + `DivFree: begin //DivFree state + if(start_i == `DivStart && annul_i == 1'b0) begin + if(opdata2_i == `ZeroWord) begin + state <= `DivByZero; + end else begin + state <= `DivOn; + cnt <= 6'b000000; + if(signed_div_i == 1'b1 && opdata1_i[31] == 1'b1 ) begin + temp_op1 = ~opdata1_i + 1; + end else begin + temp_op1 = opdata1_i; + end + if(signed_div_i == 1'b1 && opdata2_i[31] == 1'b1 ) begin + temp_op2 = ~opdata2_i + 1; + end else begin + temp_op2 = opdata2_i; + end + dividend <= {`ZeroWord,`ZeroWord}; + dividend[32:1] <= temp_op1; + divisor <= temp_op2; + end + end else begin + ready_o <= `DivResultNotReady; + result_o <= {`ZeroWord,`ZeroWord}; + end + end + `DivByZero: begin //DivByZero state + dividend <= {`ZeroWord,`ZeroWord}; + state <= `DivEnd; + end + `DivOn: begin //DivOn state + if(annul_i == 1'b0) begin + if(cnt != 6'b100000) begin + if(div_temp[32] == 1'b1) begin + dividend <= {dividend[63:0] , 1'b0}; + end else begin + dividend <= {div_temp[31:0] , dividend[31:0] , 1'b1}; + end + cnt <= cnt + 1; + end else begin + if((signed_div_i == 1'b1) && ((opdata1_i[31] ^ opdata2_i[31]) == 1'b1)) begin + dividend[31:0] <= (~dividend[31:0] + 1); + end + if((signed_div_i == 1'b1) && ((opdata1_i[31] ^ dividend[64]) == 1'b1)) begin + dividend[64:33] <= (~dividend[64:33] + 1); + end + state <= `DivEnd; + cnt <= 6'b000000; + end + end else begin + state <= `DivFree; + end + end + `DivEnd: begin //DivEnd state + result_o <= {dividend[64:33], dividend[31:0]}; + ready_o <= `DivResultReady; + if(start_i == `DivStop) begin + state <= `DivFree; + ready_o <= `DivResultNotReady; + result_o <= {`ZeroWord,`ZeroWord}; + end + end + endcase + end + end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_cpu_ip/Prob007_div_test.sv b/Verilog Gen/dataset_cpu_ip/Prob007_div_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..2d333b4691fc421d2792137bb55a573e76c248be --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob007_div_test.sv @@ -0,0 +1,291 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 +`define RstEnable 1'b1 +`define RstDisable 1'b0 +`define DivFree 2'b00 +`define DivByZero 2'b01 +`define DivOn 2'b10 +`define DivEnd 2'b11 +`define DivResultReady 1'b1 +`define DivResultNotReady 1'b0 +`define DivStart 1'b1 +`define DivStop 1'b0 +`define ZeroWord 32'h00000000 + +module stimulus_gen ( + input clk, + // === Start your code here === + output reg rst, + output reg signed_div_i, + output reg [31:0] opdata1_i, + output reg [31:0] opdata2_i, + output reg start_i, + output reg annul_i + // === End your code here === +); + // === Start your code here === + initial begin + // Initialize all signals + rst = `RstEnable; + signed_div_i = 1'b0; + opdata1_i = 32'h0; + opdata2_i = 32'h0; + start_i = `DivStop; + annul_i = 1'b0; + + // Hold reset for a few cycles + @(posedge clk); + @(posedge clk); + @(posedge clk); + + // Release reset + rst = `RstDisable; + @(posedge clk); + + // Test 1: Simple unsigned division (100 / 5 = 20 remainder 0) + signed_div_i = 1'b0; + opdata1_i = 32'd100; + opdata2_i = 32'd5; + start_i = `DivStart; + annul_i = 1'b0; + // Keep start_i asserted for 33 cycles (32 cycles for calculation + 1 for DivEnd) + repeat(33) @(posedge clk); + start_i = `DivStop; + @(posedge clk); // Wait for result to be cleared + repeat(5) @(posedge clk); + + // Test 2: Unsigned division with remainder (100 / 7 = 14 remainder 2) + opdata1_i = 32'd100; + opdata2_i = 32'd7; + start_i = `DivStart; + repeat(33) @(posedge clk); + start_i = `DivStop; + @(posedge clk); + repeat(5) @(posedge clk); + + // Test 3: Division by zero + opdata1_i = 32'd100; + opdata2_i = `ZeroWord; + start_i = `DivStart; + repeat(33) @(posedge clk); + start_i = `DivStop; + @(posedge clk); + repeat(5) @(posedge clk); + + // Test 4: Signed division (positive / positive) + signed_div_i = 1'b1; + opdata1_i = 32'd100; + opdata2_i = 32'd5; + start_i = `DivStart; + repeat(33) @(posedge clk); + start_i = `DivStop; + @(posedge clk); + repeat(5) @(posedge clk); + + // Test 5: Signed division (negative / positive) + opdata1_i = 32'hFFFFFF9C; // -100 in two's complement + opdata2_i = 32'd5; + start_i = `DivStart; + repeat(33) @(posedge clk); + start_i = `DivStop; + @(posedge clk); + repeat(5) @(posedge clk); + + // Test 6: Signed division (positive / negative) + opdata1_i = 32'd100; + opdata2_i = 32'hFFFFFFFB; // -5 in two's complement + start_i = `DivStart; + repeat(33) @(posedge clk); + start_i = `DivStop; + @(posedge clk); + repeat(5) @(posedge clk); + + // Test 7: Signed division (negative / negative) + opdata1_i = 32'hFFFFFF9C; // -100 + opdata2_i = 32'hFFFFFFFB; // -5 + start_i = `DivStart; + repeat(33) @(posedge clk); + start_i = `DivStop; + @(posedge clk); + repeat(5) @(posedge clk); + + // Test 8: Annul test - cancel during calculation + opdata1_i = 32'd1000; + opdata2_i = 32'd3; + start_i = `DivStart; + annul_i = 1'b0; + repeat(10) @(posedge clk); // Wait a bit + annul_i = 1'b1; // Cancel the operation + @(posedge clk); + annul_i = 1'b0; + start_i = `DivStop; + repeat(5) @(posedge clk); + + // Test 9: Large numbers + opdata1_i = 32'hFFFFFFFF; + opdata2_i = 32'd1; + start_i = `DivStart; + repeat(33) @(posedge clk); + start_i = `DivStop; + @(posedge clk); + repeat(5) @(posedge clk); + + + // Random testing + repeat(200) @(posedge clk) begin + signed_div_i <= $urandom % 2; + opdata1_i <= $urandom; + opdata2_i <= $urandom; + start_i <= ($urandom % 10 == 0) ? `DivStart : `DivStop; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_result_o; + int errortime_result_o; + int errors_ready_o; + int errortime_ready_o; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic signed_div_i; + logic [31:0] opdata1_i; + logic [31:0] opdata2_i; + logic start_i; + logic annul_i; + logic [63:0] result_o_ref; + logic [63:0] result_o_dut; + logic ready_o_ref; + logic ready_o_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, signed_div_i, opdata1_i, opdata2_i, start_i, annul_i, result_o_ref, result_o_dut, ready_o_ref, ready_o_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk(clk), + .rst(rst), + .signed_div_i(signed_div_i), + .opdata1_i(opdata1_i), + .opdata2_i(opdata2_i), + .start_i(start_i), + .annul_i(annul_i) + ); + + RefModule good1 ( + .clk(clk), + .rst(rst), + .signed_div_i(signed_div_i), + .opdata1_i(opdata1_i), + .opdata2_i(opdata2_i), + .start_i(start_i), + .annul_i(annul_i), + .result_o(result_o_ref), + .ready_o(ready_o_ref) + ); + + TopModule top_module1 ( + .clk(clk), + .rst(rst), + .signed_div_i(signed_div_i), + .opdata1_i(opdata1_i), + .opdata2_i(opdata2_i), + .start_i(start_i), + .annul_i(annul_i), + .result_o(result_o_dut), + .ready_o(ready_o_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_result_o) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "result_o", stats1.errors_result_o, stats1.errortime_result_o); + else $display("Hint: Output '%s' has no mismatches.", "result_o"); + if (stats1.errors_ready_o) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "ready_o", stats1.errors_ready_o, stats1.errortime_ready_o); + else $display("Hint: Output '%s' has no mismatches.", "ready_o"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { result_o_ref, ready_o_ref } === ( { result_o_ref, ready_o_ref } ^ { result_o_dut, ready_o_dut } ^ { result_o_ref, ready_o_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (result_o_ref !== ( result_o_ref ^ result_o_dut ^ result_o_ref )) + begin + if (stats1.errors_result_o == 0) stats1.errortime_result_o = $time; + stats1.errors_result_o = stats1.errors_result_o+1'b1; + end + if (ready_o_ref !== ( ready_o_ref ^ ready_o_dut ^ ready_o_ref )) + begin + if (stats1.errors_ready_o == 0) stats1.errortime_ready_o = $time; + stats1.errors_ready_o = stats1.errors_ready_o+1'b1; + end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_cpu_ip/Prob008_chapter12_ctrl_prompt.txt b/Verilog Gen/dataset_cpu_ip/Prob008_chapter12_ctrl_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..a36f2eb8c57a22de7989a4f7cb7a774496ad6537 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob008_chapter12_ctrl_prompt.txt @@ -0,0 +1,42 @@ +I would like you to implement a module named `TopModule` with the following interface. All input and output ports are one bit unless otherwise specified. + +- **Input ports:** + - `rst`: Active-high reset signal. + - `excepttype_i [31:0]`: Exception type input. + - `cp0_epc_i [31:0]`: Input for EPC (program counter) from CP0, used during exceptions. + - `stallreq_from_if`: Stall request signal from the instruction fetch stage. + - `stallreq_from_id`: Stall request signal from the instruction decode stage. + - `stallreq_from_ex`: Stall request signal from the execution stage. + - `stallreq_from_mem`: Stall request signal from the memory stage. + +- **Output ports:** + - `new_pc [31:0]`: Output for the new program counter value, updated during exceptions. + - `flush`: Signal to flush the pipeline. + - `stall [5:0]`: Stall control signal for the pipeline. + +The module should implement the following functionality: +1. If the reset signal (`rst`) is active, all outputs should be reset: + - `stall` should be set to `6'b000000`. + - `flush` should be set to `0`. + - `new_pc` should be set to `0`. + +2. If an exception occurs (`excepttype_i` is not zero), the pipeline should be flushed (`flush = 1`), and the `stall` signal should be cleared (`stall = 6'b000000`). The `new_pc` output should be set to the appropriate exception vector based on the type of exception: + - Interrupt (`excepttype_i == 32'h00000001`): Set `new_pc` to `32'h00000020`. + - System call (`excepttype_i == 32'h00000008`): Set `new_pc` to `32'h00000040`. + - Invalid instruction (`excepttype_i == 32'h0000000a`): Set `new_pc` to `32'h00000040`. + - Trap (`excepttype_i == 32'h0000000d`): Set `new_pc` to `32'h00000040`. + - Arithmetic overflow (`excepttype_i == 32'h0000000c`): Set `new_pc` to `32'h00000040`. + - ERET (`excepttype_i == 32'h0000000e`): Set `new_pc` to the value of `cp0_epc_i`. + +3. If no exception occurs, the module should handle stall requests from different stages: + - If `stallreq_from_mem` is asserted, set `stall` to `6'b011111` and do not flush the pipeline. + - If `stallreq_from_ex` is asserted, set `stall` to `6'b001111` and do not flush the pipeline. + - If `stallreq_from_id` is asserted, set `stall` to `6'b000111` and do not flush the pipeline. + - If `stallreq_from_if` is asserted, set `stall` to `6'b000111` and do not flush the pipeline. + +4. If no stall request or exception occurs, the outputs should remain in their default state: + - `stall = 6'b000000` + - `flush = 0` + - `new_pc = 0` + +Make sure to implement this functionality in a clean and modular way, using combinational or sequential logic as appropriate. \ No newline at end of file diff --git a/Verilog Gen/dataset_cpu_ip/Prob008_chapter12_ctrl_ref.sv b/Verilog Gen/dataset_cpu_ip/Prob008_chapter12_ctrl_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..717af392500b4fbf75a16922d706c9bbc0988d4c --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob008_chapter12_ctrl_ref.sv @@ -0,0 +1,73 @@ + +module RefModule( + + input wire rst, + + input wire[31:0] excepttype_i, + input wire[`RegBus] cp0_epc_i, + + input wire stallreq_from_if, + input wire stallreq_from_id, + + //����ִ�н׶ε���ͣ���� + input wire stallreq_from_ex, + input wire stallreq_from_mem, + + output reg[`RegBus] new_pc, + output reg flush, + output reg[5:0] stall + +); + + + always @ (*) begin + if(rst == `RstEnable) begin + stall <= 6'b000000; + flush <= 1'b0; + new_pc <= `ZeroWord; + end else if(excepttype_i != `ZeroWord) begin + flush <= 1'b1; + stall <= 6'b000000; + case (excepttype_i) + 32'h00000001: begin //interrupt + new_pc <= 32'h00000020; + end + 32'h00000008: begin //syscall + new_pc <= 32'h00000040; + end + 32'h0000000a: begin //inst_invalid + new_pc <= 32'h00000040; + end + 32'h0000000d: begin //trap + new_pc <= 32'h00000040; + end + 32'h0000000c: begin //ov + new_pc <= 32'h00000040; + end + 32'h0000000e: begin //eret + new_pc <= cp0_epc_i; + end + default : begin + end + endcase + end else if(stallreq_from_mem == `Stop) begin + stall <= 6'b011111; + flush <= 1'b0; + end else if(stallreq_from_ex == `Stop) begin + stall <= 6'b001111; + flush <= 1'b0; + end else if(stallreq_from_id == `Stop) begin + stall <= 6'b000111; + flush <= 1'b0; + end else if(stallreq_from_if == `Stop) begin + stall <= 6'b000111; + flush <= 1'b0; + end else begin + stall <= 6'b000000; + flush <= 1'b0; + new_pc <= `ZeroWord; + end //if + end //always + + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_cpu_ip/Prob008_chapter12_ctrl_test.sv b/Verilog Gen/dataset_cpu_ip/Prob008_chapter12_ctrl_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..fa8b10b0607ad4bd4114d6d265d6975a3c1d01bc --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob008_chapter12_ctrl_test.sv @@ -0,0 +1,487 @@ +`define RstEnable 1'b1 +`define RstDisable 1'b0 +`define ZeroWord 32'h00000000 +`define WriteEnable 1'b1 +`define WriteDisable 1'b0 +`define ReadEnable 1'b1 +`define ReadDisable 1'b0 +`define AluOpBus 7:0 +`define AluSelBus 2:0 +`define InstValid 1'b0 +`define InstInvalid 1'b1 +`define Stop 1'b1 +`define NoStop 1'b0 +`define InDelaySlot 1'b1 +`define NotInDelaySlot 1'b0 +`define Branch 1'b1 +`define NotBranch 1'b0 +`define InterruptAssert 1'b1 +`define InterruptNotAssert 1'b0 +`define TrapAssert 1'b1 +`define TrapNotAssert 1'b0 +`define True_v 1'b1 +`define False_v 1'b0 +`define ChipEnable 1'b1 +`define ChipDisable 1'b0 + + +//Instructions +`define EXE_AND 6'b100100 +`define EXE_OR 6'b100101 +`define EXE_XOR 6'b100110 +`define EXE_NOR 6'b100111 +`define EXE_ANDI 6'b001100 +`define EXE_ORI 6'b001101 +`define EXE_XORI 6'b001110 +`define EXE_LUI 6'b001111 + +`define EXE_SLL 6'b000000 +`define EXE_SLLV 6'b000100 +`define EXE_SRL 6'b000010 +`define EXE_SRLV 6'b000110 +`define EXE_SRA 6'b000011 +`define EXE_SRAV 6'b000111 +`define EXE_SYNC 6'b001111 +`define EXE_PREF 6'b110011 + +`define EXE_MOVZ 6'b001010 +`define EXE_MOVN 6'b001011 +`define EXE_MFHI 6'b010000 +`define EXE_MTHI 6'b010001 +`define EXE_MFLO 6'b010010 +`define EXE_MTLO 6'b010011 + +`define EXE_SLT 6'b101010 +`define EXE_SLTU 6'b101011 +`define EXE_SLTI 6'b001010 +`define EXE_SLTIU 6'b001011 +`define EXE_ADD 6'b100000 +`define EXE_ADDU 6'b100001 +`define EXE_SUB 6'b100010 +`define EXE_SUBU 6'b100011 +`define EXE_ADDI 6'b001000 +`define EXE_ADDIU 6'b001001 +`define EXE_CLZ 6'b100000 +`define EXE_CLO 6'b100001 + +`define EXE_MULT 6'b011000 +`define EXE_MULTU 6'b011001 +`define EXE_MUL 6'b000010 +`define EXE_MADD 6'b000000 +`define EXE_MADDU 6'b000001 +`define EXE_MSUB 6'b000100 +`define EXE_MSUBU 6'b000101 + +`define EXE_DIV 6'b011010 +`define EXE_DIVU 6'b011011 + +`define EXE_J 6'b000010 +`define EXE_JAL 6'b000011 +`define EXE_JALR 6'b001001 +`define EXE_JR 6'b001000 +`define EXE_BEQ 6'b000100 +`define EXE_BGEZ 5'b00001 +`define EXE_BGEZAL 5'b10001 +`define EXE_BGTZ 6'b000111 +`define EXE_BLEZ 6'b000110 +`define EXE_BLTZ 5'b00000 +`define EXE_BLTZAL 5'b10000 +`define EXE_BNE 6'b000101 + +`define EXE_LB 6'b100000 +`define EXE_LBU 6'b100100 +`define EXE_LH 6'b100001 +`define EXE_LHU 6'b100101 +`define EXE_LL 6'b110000 +`define EXE_LW 6'b100011 +`define EXE_LWL 6'b100010 +`define EXE_LWR 6'b100110 +`define EXE_SB 6'b101000 +`define EXE_SC 6'b111000 +`define EXE_SH 6'b101001 +`define EXE_SW 6'b101011 +`define EXE_SWL 6'b101010 +`define EXE_SWR 6'b101110 + +`define EXE_SYSCALL 6'b001100 + +`define EXE_TEQ 6'b110100 +`define EXE_TEQI 5'b01100 +`define EXE_TGE 6'b110000 +`define EXE_TGEI 5'b01000 +`define EXE_TGEIU 5'b01001 +`define EXE_TGEU 6'b110001 +`define EXE_TLT 6'b110010 +`define EXE_TLTI 5'b01010 +`define EXE_TLTIU 5'b01011 +`define EXE_TLTU 6'b110011 +`define EXE_TNE 6'b110110 +`define EXE_TNEI 5'b01110 + +`define EXE_ERET 32'b01000010000000000000000000011000 + +`define EXE_NOP 6'b000000 +`define SSNOP 32'b00000000000000000000000001000000 + +`define EXE_SPECIAL_INST 6'b000000 +`define EXE_REGIMM_INST 6'b000001 +`define EXE_SPECIAL2_INST 6'b011100 + +//AluOp +`define EXE_AND_OP 8'b00100100 +`define EXE_OR_OP 8'b00100101 +`define EXE_XOR_OP 8'b00100110 +`define EXE_NOR_OP 8'b00100111 +`define EXE_ANDI_OP 8'b01011001 +`define EXE_ORI_OP 8'b01011010 +`define EXE_XORI_OP 8'b01011011 +`define EXE_LUI_OP 8'b01011100 + +`define EXE_SLL_OP 8'b01111100 +`define EXE_SLLV_OP 8'b00000100 +`define EXE_SRL_OP 8'b00000010 +`define EXE_SRLV_OP 8'b00000110 +`define EXE_SRA_OP 8'b00000011 +`define EXE_SRAV_OP 8'b00000111 + +`define EXE_MOVZ_OP 8'b00001010 +`define EXE_MOVN_OP 8'b00001011 +`define EXE_MFHI_OP 8'b00010000 +`define EXE_MTHI_OP 8'b00010001 +`define EXE_MFLO_OP 8'b00010010 +`define EXE_MTLO_OP 8'b00010011 + +`define EXE_SLT_OP 8'b00101010 +`define EXE_SLTU_OP 8'b00101011 +`define EXE_SLTI_OP 8'b01010111 +`define EXE_SLTIU_OP 8'b01011000 +`define EXE_ADD_OP 8'b00100000 +`define EXE_ADDU_OP 8'b00100001 +`define EXE_SUB_OP 8'b00100010 +`define EXE_SUBU_OP 8'b00100011 +`define EXE_ADDI_OP 8'b01010101 +`define EXE_ADDIU_OP 8'b01010110 +`define EXE_CLZ_OP 8'b10110000 +`define EXE_CLO_OP 8'b10110001 + +`define EXE_MULT_OP 8'b00011000 +`define EXE_MULTU_OP 8'b00011001 +`define EXE_MUL_OP 8'b10101001 +`define EXE_MADD_OP 8'b10100110 +`define EXE_MADDU_OP 8'b10101000 +`define EXE_MSUB_OP 8'b10101010 +`define EXE_MSUBU_OP 8'b10101011 + +`define EXE_DIV_OP 8'b00011010 +`define EXE_DIVU_OP 8'b00011011 + +`define EXE_J_OP 8'b01001111 +`define EXE_JAL_OP 8'b01010000 +`define EXE_JALR_OP 8'b00001001 +`define EXE_JR_OP 8'b00001000 +`define EXE_BEQ_OP 8'b01010001 +`define EXE_BGEZ_OP 8'b01000001 +`define EXE_BGEZAL_OP 8'b01001011 +`define EXE_BGTZ_OP 8'b01010100 +`define EXE_BLEZ_OP 8'b01010011 +`define EXE_BLTZ_OP 8'b01000000 +`define EXE_BLTZAL_OP 8'b01001010 +`define EXE_BNE_OP 8'b01010010 + +`define EXE_LB_OP 8'b11100000 +`define EXE_LBU_OP 8'b11100100 +`define EXE_LH_OP 8'b11100001 +`define EXE_LHU_OP 8'b11100101 +`define EXE_LL_OP 8'b11110000 +`define EXE_LW_OP 8'b11100011 +`define EXE_LWL_OP 8'b11100010 +`define EXE_LWR_OP 8'b11100110 +`define EXE_PREF_OP 8'b11110011 +`define EXE_SB_OP 8'b11101000 +`define EXE_SC_OP 8'b11111000 +`define EXE_SH_OP 8'b11101001 +`define EXE_SW_OP 8'b11101011 +`define EXE_SWL_OP 8'b11101010 +`define EXE_SWR_OP 8'b11101110 +`define EXE_SYNC_OP 8'b00001111 + +`define EXE_MFC0_OP 8'b01011101 +`define EXE_MTC0_OP 8'b01100000 + +`define EXE_SYSCALL_OP 8'b00001100 + +`define EXE_TEQ_OP 8'b00110100 +`define EXE_TEQI_OP 8'b01001000 +`define EXE_TGE_OP 8'b00110000 +`define EXE_TGEI_OP 8'b01000100 +`define EXE_TGEIU_OP 8'b01000101 +`define EXE_TGEU_OP 8'b00110001 +`define EXE_TLT_OP 8'b00110010 +`define EXE_TLTI_OP 8'b01000110 +`define EXE_TLTIU_OP 8'b01000111 +`define EXE_TLTU_OP 8'b00110011 +`define EXE_TNE_OP 8'b00110110 +`define EXE_TNEI_OP 8'b01001001 + +`define EXE_ERET_OP 8'b01101011 + +`define EXE_NOP_OP 8'b00000000 + +//AluSel +`define EXE_RES_LOGIC 3'b001 +`define EXE_RES_SHIFT 3'b010 +`define EXE_RES_MOVE 3'b011 +`define EXE_RES_ARITHMETIC 3'b100 +`define EXE_RES_MUL 3'b101 +`define EXE_RES_JUMP_BRANCH 3'b110 +`define EXE_RES_LOAD_STORE 3'b111 + +`define EXE_RES_NOP 3'b000 + + +//Instruction memory inst_rom +`define InstAddrBus 31:0 +`define InstBus 31:0 +`define InstMemNum 131071 +`define InstMemNumLog2 17 + +//Data memory data_ram +`define DataAddrBus 31:0 +`define DataBus 31:0 +`define DataMemNum 131071 +`define DataMemNumLog2 17 +`define ByteWidth 7:0 + +//General purpose registers regfile +`define RegAddrBus 4:0 +`define RegBus 31:0 +`define RegWidth 32 +`define DoubleRegWidth 64 +`define DoubleRegBus 63:0 +`define RegNum 32 +`define RegNumLog2 5 +`define NOPRegAddr 5'b00000 + +//Division div +`define DivFree 2'b00 +`define DivByZero 2'b01 +`define DivOn 2'b10 +`define DivEnd 2'b11 +`define DivResultReady 1'b1 +`define DivResultNotReady 1'b0 +`define DivStart 1'b1 +`define DivStop 1'b0 + +//CP0 register addresses +`define CP0_REG_COUNT 5'b01001 //Read-write +`define CP0_REG_COMPARE 5'b01011 //Read-write +`define CP0_REG_STATUS 5'b01100 //Read-write +`define CP0_REG_CAUSE 5'b01101 //Read-only +`define CP0_REG_EPC 5'b01110 //Read-write +`define CP0_REG_PrId 5'b01111 //Read-only +`define CP0_REG_CONFIG 5'b10000 //Read-only + +//Wishbone bus state machine +`define WB_IDLE 2'b00 +`define WB_BUSY 2'b01 +`define WB_WAIT_FOR_FLUSHING 2'b10 +`define WB_WAIT_FOR_STALL 2'b11 + +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + +module stimulus_gen ( + input wire clk, + output reg rst, + output reg [31:0] excepttype_i, + output reg [`RegBus] cp0_epc_i, + output reg stallreq_from_if, + output reg stallreq_from_id, + output reg stallreq_from_ex, + output reg stallreq_from_mem, + input wire tb_match +); + + bit failed = 0; + initial begin + @(posedge clk); + rst <= `RstEnable; + excepttype_i <= `ZeroWord; + cp0_epc_i <= `ZeroWord; + stallreq_from_if <= `NoStop; + stallreq_from_id <= `NoStop; + stallreq_from_ex <= `NoStop; + stallreq_from_mem <= `NoStop; + + @(posedge clk); + rst <= `RstDisable; + + // Test interrupt exception + excepttype_i <= 32'h00000001; + @(posedge clk); + excepttype_i <= `ZeroWord; + + // Test syscall exception + excepttype_i <= 32'h00000008; + @(posedge clk); + excepttype_i <= `ZeroWord; + + // Test invalid instruction exception + excepttype_i <= 32'h0000000a; + @(posedge clk); + excepttype_i <= `ZeroWord; + + // Test trap exception + excepttype_i <= 32'h0000000d; + @(posedge clk); + excepttype_i <= `ZeroWord; + + // Test overflow exception + excepttype_i <= 32'h0000000c; + @(posedge clk); + excepttype_i <= `ZeroWord; + + // Test eret exception + cp0_epc_i <= 32'h12345678; + excepttype_i <= 32'h0000000e; + @(posedge clk); + excepttype_i <= `ZeroWord; + + // Test stall from memory + stallreq_from_mem <= `Stop; + @(posedge clk); + stallreq_from_mem <= `NoStop; + + // Test stall from execution + stallreq_from_ex <= `Stop; + @(posedge clk); + stallreq_from_ex <= `NoStop; + + // Test stall from decode + stallreq_from_id <= `Stop; + @(posedge clk); + stallreq_from_id <= `NoStop; + + // Test stall from fetch + stallreq_from_if <= `Stop; + @(posedge clk); + stallreq_from_if <= `NoStop; + + #1000; // Wait for the simulation to run + #1 $finish; + end + + always @(posedge clk) + if (!tb_match) + failed <= 1; + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_new_pc; + int errortime_new_pc; + int clocks; + } stats; + + stats stats1; + + reg clk = 0; + initial forever #5 clk = ~clk; + + logic rst; + logic [31:0] excepttype_i; + logic [`RegBus] cp0_epc_i; + logic stallreq_from_if; + logic stallreq_from_id; + logic stallreq_from_ex; + logic stallreq_from_mem; + logic [5:0] stall_ref, stall_dut; + logic flush_ref, flush_dut; + logic [`RegBus] new_pc_ref, new_pc_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, excepttype_i, cp0_epc_i, stallreq_from_if, stallreq_from_id, stallreq_from_ex, stallreq_from_mem, stall_ref, stall_dut, flush_ref, flush_dut, new_pc_ref, new_pc_dut); + end + + wire tb_match; + wire tb_mismatch = ~tb_match; + + stimulus_gen stim1 ( + .clk(clk), + .rst(rst), + .excepttype_i(excepttype_i), + .cp0_epc_i(cp0_epc_i), + .stallreq_from_if(stallreq_from_if), + .stallreq_from_id(stallreq_from_id), + .stallreq_from_ex(stallreq_from_ex), + .stallreq_from_mem(stallreq_from_mem), + .tb_match(tb_match) + ); + + RefModule good1 ( + .rst(rst), + .excepttype_i(excepttype_i), + .cp0_epc_i(cp0_epc_i), + .stallreq_from_if(stallreq_from_if), + .stallreq_from_id(stallreq_from_id), + .stallreq_from_ex(stallreq_from_ex), + .stallreq_from_mem(stallreq_from_mem), + .new_pc(new_pc_ref), + .flush(flush_ref), + .stall(stall_ref) + ); + + TopModule top_module1 ( + .rst(rst), + .excepttype_i(excepttype_i), + .cp0_epc_i(cp0_epc_i), + .stallreq_from_if(stallreq_from_if), + .stallreq_from_id(stallreq_from_id), + .stallreq_from_ex(stallreq_from_ex), + .stallreq_from_mem(stallreq_from_mem), + .new_pc(new_pc_dut), + .flush(flush_dut), + .stall(stall_dut) + ); + + // Standard ChipBench compare: an X in the reference (its outputs before any input is driven) matches anything. + // new_pc is not compared while a stall request is served without exception: the spec does not define it there, + // and the reference's hold of the old value depends on the zero-time order in which two inputs change together. + wire pc_chk = !(rst === 1'b0 && excepttype_i === 32'h0 && (stallreq_from_mem || stallreq_from_ex || stallreq_from_id || stallreq_from_if) === 1'b1); + wire pc_ok = !pc_chk || (new_pc_ref === (new_pc_ref ^ new_pc_dut ^ new_pc_ref)); + assign tb_match = pc_ok && ({flush_ref, stall_ref} === ({flush_ref, stall_ref} ^ {flush_dut, stall_dut} ^ {flush_ref, stall_ref})); + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (!pc_ok) begin + if (stats1.errors_new_pc == 0) stats1.errortime_new_pc = $time; + stats1.errors_new_pc++; + end + end + + final begin + if (stats1.errors_new_pc) $display("Hint: Output 'new_pc' has %0d mismatches. First mismatch occurred at time %0d.", stats1.errors_new_pc, stats1.errortime_new_pc); + else $display("Hint: Output 'new_pc' has no mismatches."); + if (stats1.errors) $display("Hint: Total mismatched samples is %1d out of %1d samples.", stats1.errors, stats1.clocks); + else $display("Hint: No mismatched samples."); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + initial begin + #1000000; + $display("TIMEOUT"); + $finish(); + end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_cpu_ip/Prob009_cp0_reg_prompt.txt b/Verilog Gen/dataset_cpu_ip/Prob009_cp0_reg_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..6cdcbd6abdee5e2a42f439899feb6e57a01be301 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob009_cp0_reg_prompt.txt @@ -0,0 +1,62 @@ +I would like you to implement a module named `TopModule` with the following interface. + +### Ports: +- **Inputs:** + - `clk`: A clock signal. + - `rst`: A reset signal. Active high. + - `we_i`: Write enable signal. + - `waddr_i [4:0]`: Write address for control registers. + - `raddr_i [4:0]`: Read address for control registers. + - `data_i [31:0]`: Data input for writing into registers. + - `excepttype_i [31:0]`: Exception type input. + - `int_i [5:0]`: Hardware interrupt signals. + - `current_inst_addr_i [31:0]`: Current instruction address. + - `is_in_delayslot_i`: Indicates if the instruction is in a delay slot. + +- **Outputs:** + - `data_o [31:0]`: Data output from registers based on the read address. + - `count_o [31:0]`: A counter register that increments on every clock cycle. + - `compare_o [31:0]`: A compare register used to trigger a timer interrupt. + - `status_o [31:0]`: Status register containing status flags. + - `cause_o [31:0]`: Cause register containing exception and interrupt information. + - `epc_o [31:0]`: Exception Program Counter register. + - `config_o [31:0]`: Configuration register. + - `prid_o [31:0]`: Processor ID register. + - `timer_int_o`: Timer interrupt output signal. + +### Functionality: +1. **Reset Behavior**: On reset (`rst`), all output registers should be initialized to their default values: + - `count_o` and `compare_o`: 0. + - `status_o`: Set `CU[0]` to `1` (indicating CP0 support), i.e. `0x10000000`. + - `cause_o`, `epc_o`, `config_o`: 0, with `config_o` having the `BE` bit set to `1` (Big-Endian), i.e. `0x00008000`. + - `prid_o`: `0x004C0102` (version and processor ID). + - `timer_int_o`: De-asserted (0). + +2. **Counter and Timer Interrupt**: + - The `count_o` register increments every clock cycle. + - If `compare_o` is non-zero and `count_o` equals `compare_o`, the `timer_int_o` signal is asserted (set to `1`). + +3. **Register Writes**: + - When `we_i` is asserted, the module writes `data_i` into one of the following registers based on the `waddr_i` value (COUNT 9, COMPARE 11, STATUS 12, CAUSE 13, EPC 14): + - `CP0_REG_COUNT`: Writes to `count_o` (instead of that cycle's increment). + - `CP0_REG_COMPARE`: Writes to `compare_o` and de-asserts `timer_int_o`. + - `CP0_REG_STATUS`: Writes to `status_o`. + - `CP0_REG_CAUSE`: Modifies specific writable bits of `cause_o`. + - `CP0_REG_EPC`: Writes to `epc_o`. + +4. **Exception Handling**: + - Based on the value of `excepttype_i`, the module updates `epc_o`, `cause_o`, and `status_o` to handle exceptions: `0x1` (interrupt), `0x8`, `0xa`, `0xc` and `0xd` set `EXL` (`status_o[1]`) and write `ExcCode` (`cause_o[6:2]`) with 0, 8, 10, 12 or 13; other values except ERET change nothing. + - If the instruction causing the exception is in a delay slot (`is_in_delayslot_i`), the module sets the `BD` bit (`cause_o[31]`) and loads `epc_o` with `current_inst_addr_i - 4`; otherwise it clears `BD` and loads `current_inst_addr_i`. Except for `0x1`, `epc_o` and `BD` change only while `EXL` is 0. + +5. **Register Reads**: + - The `data_o` output provides the value of the register specified by `raddr_i` in the same cycle (numbers as above, plus PrId 15 and Config 16); it is 0 while `rst` is high. + +6. **Interrupt Handling**: + - The module updates the `IP` field in `cause_o` with the value of `int_i`. + +7. **ERET Handling**: + - If the `excepttype_i` input indicates an ERET instruction (`0xe`), the module clears the `EXL` bit in `status_o`. + +### Additional Notes: +- Use descriptive signal names and comments to clarify the functionality. +- Ensure that the design is synthesizable and adheres to best practices for SystemVerilog coding. diff --git a/Verilog Gen/dataset_cpu_ip/Prob009_cp0_reg_ref.sv b/Verilog Gen/dataset_cpu_ip/Prob009_cp0_reg_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..d5d548636db6580e789c2f6c146f51ef0a03f0f8 --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob009_cp0_reg_ref.sv @@ -0,0 +1,183 @@ +module RefModule( + + input wire clk, + input wire rst, + + + input wire we_i, + input wire[4:0] waddr_i, + input wire[4:0] raddr_i, + input wire[`RegBus] data_i, + + input wire[31:0] excepttype_i, + input wire[5:0] int_i, + input wire[`RegBus] current_inst_addr_i, + input wire is_in_delayslot_i, + + output reg[`RegBus] data_o, + output reg[`RegBus] count_o, + output reg[`RegBus] compare_o, + output reg[`RegBus] status_o, + output reg[`RegBus] cause_o, + output reg[`RegBus] epc_o, + output reg[`RegBus] config_o, + output reg[`RegBus] prid_o, + + output reg timer_int_o + +); + + always @ (posedge clk) begin + if(rst == `RstEnable) begin + count_o <= `ZeroWord; + compare_o <= `ZeroWord; + //status register: CU is 0001, indicating coprocessor CP0 is available + status_o <= 32'b00010000000000000000000000000000; + cause_o <= `ZeroWord; + epc_o <= `ZeroWord; + //config register: BE is 1, indicating Big-Endian; MT is 00, indicating no MMU + config_o <= 32'b00000000000000001000000000000000; + //Processor ID: company L corresponds to 0x48, processor number 0x1, processor type, version 1.0 + prid_o <= 32'b00000000010011000000000100000010; + timer_int_o <= `InterruptNotAssert; + end else begin + count_o <= count_o + 1 ; + cause_o[15:10] <= int_i; + + if(compare_o != `ZeroWord && count_o == compare_o) begin + timer_int_o <= `InterruptAssert; + end + + if(we_i == `WriteEnable) begin + case (waddr_i) + `CP0_REG_COUNT: begin + count_o <= data_i; + end + `CP0_REG_COMPARE: begin + compare_o <= data_i; + //count_o <= `ZeroWord; + timer_int_o <= `InterruptNotAssert; + end + `CP0_REG_STATUS: begin + status_o <= data_i; + end + `CP0_REG_EPC: begin + epc_o <= data_i; + end + `CP0_REG_CAUSE: begin + //cause register: only IP[1:0], IV, and WP fields are writable + cause_o[9:8] <= data_i[9:8]; + cause_o[23] <= data_i[23]; + cause_o[22] <= data_i[22]; + end + endcase //case addr_i + end + + case (excepttype_i) + 32'h00000001: begin + if(is_in_delayslot_i == `InDelaySlot ) begin + epc_o <= current_inst_addr_i - 4 ; + cause_o[31] <= 1'b1; + end else begin + epc_o <= current_inst_addr_i; + cause_o[31] <= 1'b0; + end + status_o[1] <= 1'b1; + cause_o[6:2] <= 5'b00000; + + end + 32'h00000008: begin + if(status_o[1] == 1'b0) begin + if(is_in_delayslot_i == `InDelaySlot ) begin + epc_o <= current_inst_addr_i - 4 ; + cause_o[31] <= 1'b1; + end else begin + epc_o <= current_inst_addr_i; + cause_o[31] <= 1'b0; + end + end + status_o[1] <= 1'b1; + cause_o[6:2] <= 5'b01000; + end + 32'h0000000a: begin + if(status_o[1] == 1'b0) begin + if(is_in_delayslot_i == `InDelaySlot ) begin + epc_o <= current_inst_addr_i - 4 ; + cause_o[31] <= 1'b1; + end else begin + epc_o <= current_inst_addr_i; + cause_o[31] <= 1'b0; + end + end + status_o[1] <= 1'b1; + cause_o[6:2] <= 5'b01010; + end + 32'h0000000d: begin + if(status_o[1] == 1'b0) begin + if(is_in_delayslot_i == `InDelaySlot ) begin + epc_o <= current_inst_addr_i - 4 ; + cause_o[31] <= 1'b1; + end else begin + epc_o <= current_inst_addr_i; + cause_o[31] <= 1'b0; + end + end + status_o[1] <= 1'b1; + cause_o[6:2] <= 5'b01101; + end + 32'h0000000c: begin + if(status_o[1] == 1'b0) begin + if(is_in_delayslot_i == `InDelaySlot ) begin + epc_o <= current_inst_addr_i - 4 ; + cause_o[31] <= 1'b1; + end else begin + epc_o <= current_inst_addr_i; + cause_o[31] <= 1'b0; + end + end + status_o[1] <= 1'b1; + cause_o[6:2] <= 5'b01100; + end + 32'h0000000e: begin + status_o[1] <= 1'b0; + end + default: begin + end + endcase + + end //if + end //always + + always @ (*) begin + if(rst == `RstEnable) begin + data_o <= `ZeroWord; + end else begin + case (raddr_i) + `CP0_REG_COUNT: begin + data_o <= count_o ; + end + `CP0_REG_COMPARE: begin + data_o <= compare_o ; + end + `CP0_REG_STATUS: begin + data_o <= status_o ; + end + `CP0_REG_CAUSE: begin + data_o <= cause_o ; + end + `CP0_REG_EPC: begin + data_o <= epc_o ; + end + `CP0_REG_PrId: begin + data_o <= prid_o ; + end + `CP0_REG_CONFIG: begin + data_o <= config_o ; + end + default: begin + end + endcase //case addr_i + end //if + end //always + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_cpu_ip/Prob009_cp0_reg_test.sv b/Verilog Gen/dataset_cpu_ip/Prob009_cp0_reg_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..988814c2a48f3130219030def18eb51981aa636c --- /dev/null +++ b/Verilog Gen/dataset_cpu_ip/Prob009_cp0_reg_test.sv @@ -0,0 +1,539 @@ +`define RstEnable 1'b1 +`define RstDisable 1'b0 +`define ZeroWord 32'h00000000 +`define WriteEnable 1'b1 +`define WriteDisable 1'b0 +`define ReadEnable 1'b1 +`define ReadDisable 1'b0 +`define AluOpBus 7:0 +`define AluSelBus 2:0 +`define InstValid 1'b0 +`define InstInvalid 1'b1 +`define Stop 1'b1 +`define NoStop 1'b0 +`define InDelaySlot 1'b1 +`define NotInDelaySlot 1'b0 +`define Branch 1'b1 +`define NotBranch 1'b0 +`define InterruptAssert 1'b1 +`define InterruptNotAssert 1'b0 +`define TrapAssert 1'b1 +`define TrapNotAssert 1'b0 +`define True_v 1'b1 +`define False_v 1'b0 +`define ChipEnable 1'b1 +`define ChipDisable 1'b0 + + +//Instructions +`define EXE_AND 6'b100100 +`define EXE_OR 6'b100101 +`define EXE_XOR 6'b100110 +`define EXE_NOR 6'b100111 +`define EXE_ANDI 6'b001100 +`define EXE_ORI 6'b001101 +`define EXE_XORI 6'b001110 +`define EXE_LUI 6'b001111 + +`define EXE_SLL 6'b000000 +`define EXE_SLLV 6'b000100 +`define EXE_SRL 6'b000010 +`define EXE_SRLV 6'b000110 +`define EXE_SRA 6'b000011 +`define EXE_SRAV 6'b000111 +`define EXE_SYNC 6'b001111 +`define EXE_PREF 6'b110011 + +`define EXE_MOVZ 6'b001010 +`define EXE_MOVN 6'b001011 +`define EXE_MFHI 6'b010000 +`define EXE_MTHI 6'b010001 +`define EXE_MFLO 6'b010010 +`define EXE_MTLO 6'b010011 + +`define EXE_SLT 6'b101010 +`define EXE_SLTU 6'b101011 +`define EXE_SLTI 6'b001010 +`define EXE_SLTIU 6'b001011 +`define EXE_ADD 6'b100000 +`define EXE_ADDU 6'b100001 +`define EXE_SUB 6'b100010 +`define EXE_SUBU 6'b100011 +`define EXE_ADDI 6'b001000 +`define EXE_ADDIU 6'b001001 +`define EXE_CLZ 6'b100000 +`define EXE_CLO 6'b100001 + +`define EXE_MULT 6'b011000 +`define EXE_MULTU 6'b011001 +`define EXE_MUL 6'b000010 +`define EXE_MADD 6'b000000 +`define EXE_MADDU 6'b000001 +`define EXE_MSUB 6'b000100 +`define EXE_MSUBU 6'b000101 + +`define EXE_DIV 6'b011010 +`define EXE_DIVU 6'b011011 + +`define EXE_J 6'b000010 +`define EXE_JAL 6'b000011 +`define EXE_JALR 6'b001001 +`define EXE_JR 6'b001000 +`define EXE_BEQ 6'b000100 +`define EXE_BGEZ 5'b00001 +`define EXE_BGEZAL 5'b10001 +`define EXE_BGTZ 6'b000111 +`define EXE_BLEZ 6'b000110 +`define EXE_BLTZ 5'b00000 +`define EXE_BLTZAL 5'b10000 +`define EXE_BNE 6'b000101 + +`define EXE_LB 6'b100000 +`define EXE_LBU 6'b100100 +`define EXE_LH 6'b100001 +`define EXE_LHU 6'b100101 +`define EXE_LL 6'b110000 +`define EXE_LW 6'b100011 +`define EXE_LWL 6'b100010 +`define EXE_LWR 6'b100110 +`define EXE_SB 6'b101000 +`define EXE_SC 6'b111000 +`define EXE_SH 6'b101001 +`define EXE_SW 6'b101011 +`define EXE_SWL 6'b101010 +`define EXE_SWR 6'b101110 + +`define EXE_SYSCALL 6'b001100 + +`define EXE_TEQ 6'b110100 +`define EXE_TEQI 5'b01100 +`define EXE_TGE 6'b110000 +`define EXE_TGEI 5'b01000 +`define EXE_TGEIU 5'b01001 +`define EXE_TGEU 6'b110001 +`define EXE_TLT 6'b110010 +`define EXE_TLTI 5'b01010 +`define EXE_TLTIU 5'b01011 +`define EXE_TLTU 6'b110011 +`define EXE_TNE 6'b110110 +`define EXE_TNEI 5'b01110 + +`define EXE_ERET 32'b01000010000000000000000000011000 + +`define EXE_NOP 6'b000000 +`define SSNOP 32'b00000000000000000000000001000000 + +`define EXE_SPECIAL_INST 6'b000000 +`define EXE_REGIMM_INST 6'b000001 +`define EXE_SPECIAL2_INST 6'b011100 + +//AluOp +`define EXE_AND_OP 8'b00100100 +`define EXE_OR_OP 8'b00100101 +`define EXE_XOR_OP 8'b00100110 +`define EXE_NOR_OP 8'b00100111 +`define EXE_ANDI_OP 8'b01011001 +`define EXE_ORI_OP 8'b01011010 +`define EXE_XORI_OP 8'b01011011 +`define EXE_LUI_OP 8'b01011100 + +`define EXE_SLL_OP 8'b01111100 +`define EXE_SLLV_OP 8'b00000100 +`define EXE_SRL_OP 8'b00000010 +`define EXE_SRLV_OP 8'b00000110 +`define EXE_SRA_OP 8'b00000011 +`define EXE_SRAV_OP 8'b00000111 + +`define EXE_MOVZ_OP 8'b00001010 +`define EXE_MOVN_OP 8'b00001011 +`define EXE_MFHI_OP 8'b00010000 +`define EXE_MTHI_OP 8'b00010001 +`define EXE_MFLO_OP 8'b00010010 +`define EXE_MTLO_OP 8'b00010011 + +`define EXE_SLT_OP 8'b00101010 +`define EXE_SLTU_OP 8'b00101011 +`define EXE_SLTI_OP 8'b01010111 +`define EXE_SLTIU_OP 8'b01011000 +`define EXE_ADD_OP 8'b00100000 +`define EXE_ADDU_OP 8'b00100001 +`define EXE_SUB_OP 8'b00100010 +`define EXE_SUBU_OP 8'b00100011 +`define EXE_ADDI_OP 8'b01010101 +`define EXE_ADDIU_OP 8'b01010110 +`define EXE_CLZ_OP 8'b10110000 +`define EXE_CLO_OP 8'b10110001 + +`define EXE_MULT_OP 8'b00011000 +`define EXE_MULTU_OP 8'b00011001 +`define EXE_MUL_OP 8'b10101001 +`define EXE_MADD_OP 8'b10100110 +`define EXE_MADDU_OP 8'b10101000 +`define EXE_MSUB_OP 8'b10101010 +`define EXE_MSUBU_OP 8'b10101011 + +`define EXE_DIV_OP 8'b00011010 +`define EXE_DIVU_OP 8'b00011011 + +`define EXE_J_OP 8'b01001111 +`define EXE_JAL_OP 8'b01010000 +`define EXE_JALR_OP 8'b00001001 +`define EXE_JR_OP 8'b00001000 +`define EXE_BEQ_OP 8'b01010001 +`define EXE_BGEZ_OP 8'b01000001 +`define EXE_BGEZAL_OP 8'b01001011 +`define EXE_BGTZ_OP 8'b01010100 +`define EXE_BLEZ_OP 8'b01010011 +`define EXE_BLTZ_OP 8'b01000000 +`define EXE_BLTZAL_OP 8'b01001010 +`define EXE_BNE_OP 8'b01010010 + +`define EXE_LB_OP 8'b11100000 +`define EXE_LBU_OP 8'b11100100 +`define EXE_LH_OP 8'b11100001 +`define EXE_LHU_OP 8'b11100101 +`define EXE_LL_OP 8'b11110000 +`define EXE_LW_OP 8'b11100011 +`define EXE_LWL_OP 8'b11100010 +`define EXE_LWR_OP 8'b11100110 +`define EXE_PREF_OP 8'b11110011 +`define EXE_SB_OP 8'b11101000 +`define EXE_SC_OP 8'b11111000 +`define EXE_SH_OP 8'b11101001 +`define EXE_SW_OP 8'b11101011 +`define EXE_SWL_OP 8'b11101010 +`define EXE_SWR_OP 8'b11101110 +`define EXE_SYNC_OP 8'b00001111 + +`define EXE_MFC0_OP 8'b01011101 +`define EXE_MTC0_OP 8'b01100000 + +`define EXE_SYSCALL_OP 8'b00001100 + +`define EXE_TEQ_OP 8'b00110100 +`define EXE_TEQI_OP 8'b01001000 +`define EXE_TGE_OP 8'b00110000 +`define EXE_TGEI_OP 8'b01000100 +`define EXE_TGEIU_OP 8'b01000101 +`define EXE_TGEU_OP 8'b00110001 +`define EXE_TLT_OP 8'b00110010 +`define EXE_TLTI_OP 8'b01000110 +`define EXE_TLTIU_OP 8'b01000111 +`define EXE_TLTU_OP 8'b00110011 +`define EXE_TNE_OP 8'b00110110 +`define EXE_TNEI_OP 8'b01001001 + +`define EXE_ERET_OP 8'b01101011 + +`define EXE_NOP_OP 8'b00000000 + +//AluSel +`define EXE_RES_LOGIC 3'b001 +`define EXE_RES_SHIFT 3'b010 +`define EXE_RES_MOVE 3'b011 +`define EXE_RES_ARITHMETIC 3'b100 +`define EXE_RES_MUL 3'b101 +`define EXE_RES_JUMP_BRANCH 3'b110 +`define EXE_RES_LOAD_STORE 3'b111 + +`define EXE_RES_NOP 3'b000 + + +//Instruction memory inst_rom +`define InstAddrBus 31:0 +`define InstBus 31:0 +`define InstMemNum 131071 +`define InstMemNumLog2 17 + +//Data memory data_ram +`define DataAddrBus 31:0 +`define DataBus 31:0 +`define DataMemNum 131071 +`define DataMemNumLog2 17 +`define ByteWidth 7:0 + +//General purpose registers regfile +`define RegAddrBus 4:0 +`define RegBus 31:0 +`define RegWidth 32 +`define DoubleRegWidth 64 +`define DoubleRegBus 63:0 +`define RegNum 32 +`define RegNumLog2 5 +`define NOPRegAddr 5'b00000 + +//Division div +`define DivFree 2'b00 +`define DivByZero 2'b01 +`define DivOn 2'b10 +`define DivEnd 2'b11 +`define DivResultReady 1'b1 +`define DivResultNotReady 1'b0 +`define DivStart 1'b1 +`define DivStop 1'b0 + +//CP0 register addresses +`define CP0_REG_COUNT 5'b01001 //Read-write +`define CP0_REG_COMPARE 5'b01011 //Read-write +`define CP0_REG_STATUS 5'b01100 //Read-write +`define CP0_REG_CAUSE 5'b01101 //Read-only +`define CP0_REG_EPC 5'b01110 //Read-write +`define CP0_REG_PrId 5'b01111 //Read-only +`define CP0_REG_CONFIG 5'b10000 //Read-only + +//Wishbone bus state machine +`define WB_IDLE 2'b00 +`define WB_BUSY 2'b01 +`define WB_WAIT_FOR_FLUSHING 2'b10 +`define WB_WAIT_FOR_STALL 2'b11 + +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + +module stimulus_gen ( + input wire clk, + output reg rst, + output reg we_i, + output reg [4:0] waddr_i, + output reg [4:0] raddr_i, + output reg [`RegBus] data_i, + output reg [31:0] excepttype_i, + output reg [5:0] int_i, + output reg [`RegBus] current_inst_addr_i, + output reg is_in_delayslot_i, + input wire tb_match +); + + bit failed = 0; + + initial begin + @(posedge clk); + rst <= `RstEnable; + we_i <= `WriteDisable; + waddr_i <= 5'b00000; + raddr_i <= 5'b00000; + data_i <= `ZeroWord; + excepttype_i <= `ZeroWord; + int_i <= 6'b000000; + current_inst_addr_i <= `ZeroWord; + is_in_delayslot_i <= 0; + + @(posedge clk); + rst <= `RstDisable; + + // Test: Write and read CP0 registers + @(posedge clk); + we_i <= `WriteEnable; + waddr_i <= `CP0_REG_COUNT; + data_i <= 32'h12345678; + @(posedge clk); + we_i <= `WriteDisable; + raddr_i <= `CP0_REG_COUNT; + + // Test: Exception handling (Interrupt) + @(posedge clk); + excepttype_i <= 32'h00000001; + current_inst_addr_i <= 32'h00400010; + is_in_delayslot_i <= 1'b1; + @(posedge clk); + excepttype_i <= `ZeroWord; + + // Test: Overflow exception + @(posedge clk); + excepttype_i <= 32'h0000000c; + current_inst_addr_i <= 32'h00400020; + is_in_delayslot_i <= 1'b0; + @(posedge clk); + excepttype_i <= `ZeroWord; + + // Test: Timer interrupt trigger + @(posedge clk); + we_i <= `WriteEnable; + waddr_i <= `CP0_REG_COMPARE; + data_i <= 32'h00000005; + @(posedge clk); + we_i <= `WriteDisable; + repeat(10) @(posedge clk); + + // Test: ERET exception + @(posedge clk); + excepttype_i <= 32'h0000000e; + @(posedge clk); + excepttype_i <= `ZeroWord; + + // Test: Random stalls and reads + repeat(10) @(posedge clk); + raddr_i <= `CP0_REG_STATUS; + repeat(10) @(posedge clk); + + #1000; // Wait for the simulation to run + #1 $finish; + end + + always @(posedge clk) + if (!tb_match) + failed <= 1; + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_data_o; + int errortime_data_o; + int errors_count_o; + int errortime_count_o; + int errors_timer_int_o; + int errortime_timer_int_o; + + int clocks; + } stats; + + stats stats1; + + reg clk = 0; + initial forever #5 clk = ~clk; + + logic rst; + logic we_i; + logic [4:0] waddr_i; + logic [4:0] raddr_i; + logic [`RegBus] data_i; + logic [31:0] excepttype_i; + logic [5:0] int_i; + logic [`RegBus] current_inst_addr_i; + logic is_in_delayslot_i; + + logic [`RegBus] data_o_ref, data_o_dut; + logic [`RegBus] count_o_ref, count_o_dut; + logic [`RegBus] compare_o_ref, compare_o_dut; + logic [`RegBus] status_o_ref, status_o_dut; + logic [`RegBus] cause_o_ref, cause_o_dut; + logic [`RegBus] epc_o_ref, epc_o_dut; + logic [`RegBus] config_o_ref, config_o_dut; + logic [`RegBus] prid_o_ref, prid_o_dut; + logic timer_int_o_ref, timer_int_o_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, we_i, waddr_i, raddr_i, data_i, excepttype_i, int_i, + current_inst_addr_i, is_in_delayslot_i, data_o_ref, data_o_dut, count_o_ref, count_o_dut, + timer_int_o_ref, timer_int_o_dut); + end + + wire tb_match; + wire tb_mismatch = ~tb_match; + + stimulus_gen stim1 ( + .clk(clk), + .rst(rst), + .we_i(we_i), + .waddr_i(waddr_i), + .raddr_i(raddr_i), + .data_i(data_i), + .excepttype_i(excepttype_i), + .int_i(int_i), + .current_inst_addr_i(current_inst_addr_i), + .is_in_delayslot_i(is_in_delayslot_i), + .tb_match(tb_match) + ); + + RefModule good1 ( + .clk(clk), + .rst(rst), + .we_i(we_i), + .waddr_i(waddr_i), + .raddr_i(raddr_i), + .data_i(data_i), + .excepttype_i(excepttype_i), + .int_i(int_i), + .current_inst_addr_i(current_inst_addr_i), + .is_in_delayslot_i(is_in_delayslot_i), + .data_o(data_o_ref), + .count_o(count_o_ref), + .compare_o(compare_o_ref), + .status_o(status_o_ref), + .cause_o(cause_o_ref), + .epc_o(epc_o_ref), + .config_o(config_o_ref), + .prid_o(prid_o_ref), + .timer_int_o(timer_int_o_ref) + ); + + TopModule top_module1 ( + .clk(clk), + .rst(rst), + .we_i(we_i), + .waddr_i(waddr_i), + .raddr_i(raddr_i), + .data_i(data_i), + .excepttype_i(excepttype_i), + .int_i(int_i), + .current_inst_addr_i(current_inst_addr_i), + .is_in_delayslot_i(is_in_delayslot_i), + .data_o(data_o_dut), + .count_o(count_o_dut), + .compare_o(compare_o_dut), + .status_o(status_o_dut), + .cause_o(cause_o_dut), + .epc_o(epc_o_dut), + .config_o(config_o_dut), + .prid_o(prid_o_dut), + .timer_int_o(timer_int_o_dut) + ); + + // Standard ChipBench compare: an X in the reference (its registers before the first reset edge) matches anything. + assign tb_match = ( { data_o_ref, count_o_ref, compare_o_ref, status_o_ref, cause_o_ref, epc_o_ref, config_o_ref, prid_o_ref, timer_int_o_ref } === + ( { data_o_ref, count_o_ref, compare_o_ref, status_o_ref, cause_o_ref, epc_o_ref, config_o_ref, prid_o_ref, timer_int_o_ref } ^ { data_o_dut, count_o_dut, compare_o_dut, status_o_dut, cause_o_dut, epc_o_dut, config_o_dut, prid_o_dut, timer_int_o_dut } ^ { data_o_ref, count_o_ref, compare_o_ref, status_o_ref, cause_o_ref, epc_o_ref, config_o_ref, prid_o_ref, timer_int_o_ref } )); + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (data_o_ref !== (data_o_ref ^ data_o_dut ^ data_o_ref)) begin + if (stats1.errors_data_o == 0) stats1.errortime_data_o = $time; + stats1.errors_data_o++; + end + if (count_o_ref !== (count_o_ref ^ count_o_dut ^ count_o_ref)) begin + if (stats1.errors_count_o == 0) stats1.errortime_count_o = $time; + stats1.errors_count_o++; + end + if (timer_int_o_ref !== (timer_int_o_ref ^ timer_int_o_dut ^ timer_int_o_ref)) begin + if (stats1.errors_timer_int_o == 0) stats1.errortime_timer_int_o = $time; + stats1.errors_timer_int_o++; + end + end + + final begin + if (stats1.errors_data_o) + $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_o", stats1.errors_data_o, stats1.errortime_data_o); + else + $display("Hint: Output '%s' has no mismatches.", "data_o"); + + if (stats1.errors_count_o) + $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "count_o", stats1.errors_count_o, stats1.errortime_count_o); + else + $display("Hint: Output '%s' has no mismatches.", "count_o"); + + if (stats1.errors_timer_int_o) + $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "timer_int_o", stats1.errors_timer_int_o, stats1.errortime_timer_int_o); + else + $display("Hint: Output '%s' has no mismatches.", "timer_int_o"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples.", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + initial begin + #1000000; + $display("TIMEOUT"); + $finish(); + end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_not_self_contain/Prob001_submodules_to_implement_comparison_of_three_input_numbers_prompt.txt b/Verilog Gen/dataset_not_self_contain/Prob001_submodules_to_implement_comparison_of_three_input_numbers_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..b062e4b2006d88be1f1bb84a0dbd6d5eaca2282e --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob001_submodules_to_implement_comparison_of_three_input_numbers_prompt.txt @@ -0,0 +1,31 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input a (8 bits) + - input b (8 bits) + - input c (8 bits) + - output d (8 bits) + +In digital chip design, code that performs a specific function and is +relatively independent is usually written as a submodule. This submodule +is then instantiated in the main module when needed, to improve code +reusability and design hierarchy, and to facilitate subsequent modifications. + +Design a hierarchical module structure where you first create a submodule +that compares two 8-bit unsigned numbers and outputs the smaller one. +Then, in the main module (TopModule), instantiate this submodule to find +the minimum among three 8-bit unsigned inputs. + +The submodule should take two 8-bit inputs (data_a and data_b) and output +the smaller of the two values. In the main module, use this submodule to +determine the minimum value among a, b, and c. + +d has two cycles of latency: at each rising edge of clk, d takes the minimum +of a, b and c sampled at the previous rising edge. d is 0 while rst_n is low +and until the second rising edge after rst_n is released. + +The rst_n signal is an active-low asynchronous reset signal. Assume all +sequential logic is triggered on the positive edge of the clock. diff --git a/Verilog Gen/dataset_not_self_contain/Prob001_submodules_to_implement_comparison_of_three_input_numbers_ref.sv b/Verilog Gen/dataset_not_self_contain/Prob001_submodules_to_implement_comparison_of_three_input_numbers_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..1fc0609f9ded6646c483deb2546f6e17dc028257 --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob001_submodules_to_implement_comparison_of_three_input_numbers_ref.sv @@ -0,0 +1,56 @@ + +module RefModule( + input clk, + input rst_n, + input [7:0]a, + input [7:0]b, + input [7:0]c, + + output [7:0]d +); + + wire [7:0] c_out1; + slave_mod s1( + .clk(clk), + .rst_n(rst_n), + .a(a), + .b(b), + .c_out(c_out1) + ); + + reg [7:0] c1; + always @(posedge clk, negedge rst_n) begin + if(!rst_n) + c1 <= 8'b0; + else + c1 <= c; + end + + slave_mod s2( + .clk(clk), + .rst_n(rst_n), + .a(c_out1), + .b(c1), + .c_out(d) + ); + +endmodule + +module slave_mod( + input clk, + input rst_n, + input [7:0]a, + input [7:0]b, + output reg [7:0] c_out +); + always @(posedge clk,negedge rst_n) begin + if(!rst_n) + c_out <= 8'b0; + else + if(a>b) + c_out <= b; + else + c_out <= a; + end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_not_self_contain/Prob001_submodules_to_implement_comparison_of_three_input_numbers_test.sv b/Verilog Gen/dataset_not_self_contain/Prob001_submodules_to_implement_comparison_of_three_input_numbers_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..dac7253fe01aa222b8819572ec17b18bc8f56875 --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob001_submodules_to_implement_comparison_of_three_input_numbers_test.sv @@ -0,0 +1,207 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [7:0] a, + output logic [7:0] b, + output logic [7:0] c + // === End your code here === +); + // === Start your code here === + initial begin + rst_n <= 0; + a <= 0; + b <= 0; + c <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test case 1: a is minimum + a <= 8'd10; + b <= 8'd20; + c <= 8'd15; + @(posedge clk); + + // Test case 2: b is minimum + a <= 8'd25; + b <= 8'd5; + c <= 8'd30; + @(posedge clk); + + // Test case 3: c is minimum + a <= 8'd50; + b <= 8'd40; + c <= 8'd15; + @(posedge clk); + + // Test case 4: all equal + a <= 8'd100; + b <= 8'd100; + c <= 8'd100; + @(posedge clk); + + // Test case 5: two values equal and minimum + a <= 8'd5; + b <= 8'd5; + c <= 8'd10; + @(posedge clk); + + // Test case 6: large values + a <= 8'd255; + b <= 8'd200; + c <= 8'd150; + @(posedge clk); + + // Test case 7: edge cases + a <= 8'd0; + b <= 8'd1; + c <= 8'd2; + @(posedge clk); + + a <= 8'd254; + b <= 8'd255; + c <= 8'd253; + @(posedge clk); + + // Random test sequence + repeat(200) @(posedge clk) begin + a <= $random & 255; + b <= $random & 255; + c <= $random & 255; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_d; + int errortime_d; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [7:0] a; + logic [7:0] b; + logic [7:0] c; + logic [7:0] d_ref; + logic [7:0] d_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, a, b, c, d_ref, d_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .a, + .b, + .c + ); + + RefModule good1 ( + .clk, + .rst_n, + .a, + .b, + .c, + .d(d_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .a, + .b, + .c, + .d(d_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_d) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "d", stats1.errors_d, stats1.errortime_d); + else $display("Hint: Output '%s' has no mismatches.", "d"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { d_ref } === ( { d_ref } ^ { d_dut } ^ { d_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (d_ref !== ( d_ref ^ d_dut ^ d_ref )) + begin if (stats1.errors_d == 0) stats1.errortime_d = $time; + stats1.errors_d = stats1.errors_d+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_not_self_contain/Prob002_implement_full_subtractor_using_three_to_eight_decoder_prompt.txt b/Verilog Gen/dataset_not_self_contain/Prob002_implement_full_subtractor_using_three_to_eight_decoder_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..7dd0b2243f9f966e0ef502d30796ccc66390e2d6 --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob002_implement_full_subtractor_using_three_to_eight_decoder_prompt.txt @@ -0,0 +1,96 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input A + - input B + - input Ci + - output D + - output Co + +Implement a full subtractor using a 3-to-8 decoder and necessary logic +gates. In the full subtractor: A is the minuend (the number being +subtracted from), B is the subtrahend (the number being subtracted), Ci +is the borrow input from the lower bit position, D is the difference, and +Co is the borrow output to the higher bit position. + +You are provided with the following 3-to-8 decoder module code, which you +should include and instantiate in your solution: + +``` +module decoder_38( + input E , + input A0 , + input A1 , + input A2 , + + output reg Y0n , + output reg Y1n , + output reg Y2n , + output reg Y3n , + output reg Y4n , + output reg Y5n , + output reg Y6n , + output reg Y7n +); + +always @(*)begin + if(!E)begin + Y0n = 1'b1; + Y1n = 1'b1; + Y2n = 1'b1; + Y3n = 1'b1; + Y4n = 1'b1; + Y5n = 1'b1; + Y6n = 1'b1; + Y7n = 1'b1; + end + else begin + case({A2,A1,A0}) + 3'b000 : begin + Y0n = 1'b0; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b001 : begin + Y0n = 1'b1; Y1n = 1'b0; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b010 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b0; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b011 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b0; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b100 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b0; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b101 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b0; Y6n = 1'b1; Y7n = 1'b1; + end + 3'b110 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b0; Y7n = 1'b1; + end + 3'b111 : begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b0; + end + default: begin + Y0n = 1'b1; Y1n = 1'b1; Y2n = 1'b1; Y3n = 1'b1; + Y4n = 1'b1; Y5n = 1'b1; Y6n = 1'b1; Y7n = 1'b1; + end + endcase + end +end + +endmodule +``` + +You should instantiate this decoder module in your implementation and use +it along with logic gates to implement the full subtractor functionality. +The decoder outputs are active-low, where only one output is low (0) at a +time based on the input address. diff --git a/Verilog Gen/dataset_not_self_contain/Prob002_implement_full_subtractor_using_three_to_eight_decoder_ref.sv b/Verilog Gen/dataset_not_self_contain/Prob002_implement_full_subtractor_using_three_to_eight_decoder_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..cfe1f270a91fa303a28afb6e33951c980b8b1eee --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob002_implement_full_subtractor_using_three_to_eight_decoder_ref.sv @@ -0,0 +1,41 @@ + +module RefModule( + input A , + input B , + input Ci , + + output wire D , + output wire Co +); + +wire Y0_n ; +wire Y1_n ; +wire Y2_n ; +wire Y3_n ; +wire Y4_n ; +wire Y5_n ; +wire Y6_n ; +wire Y7_n ; + +decoder_38 U0( + .E (1'b1), + .A0 (Ci ), + .A1 (B ), + .A2 (A ), + + .Y0n (Y0_n), + .Y1n (Y1_n), + .Y2n (Y2_n), + .Y3n (Y3_n), + .Y4n (Y4_n), + .Y5n (Y5_n), + .Y6n (Y6_n), + .Y7n (Y7_n) +); + +assign D = ~(Y1_n & Y2_n & Y4_n & Y7_n); +assign Co = ~(Y1_n & Y2_n & Y3_n & Y7_n); + +endmodule + +// decoder_38 module is in prompt file. \ No newline at end of file diff --git a/Verilog Gen/dataset_not_self_contain/Prob002_implement_full_subtractor_using_three_to_eight_decoder_test.sv b/Verilog Gen/dataset_not_self_contain/Prob002_implement_full_subtractor_using_three_to_eight_decoder_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..7d73473e5e674a64e29c7e9848b6f1e24b1481bb --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob002_implement_full_subtractor_using_three_to_eight_decoder_test.sv @@ -0,0 +1,178 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic A, + output logic B, + output logic Ci + // === End your code here === +); + // === Start your code here === + initial begin + A <= 0; + B <= 0; + Ci <= 0; + + // Test all 8 possible combinations + @(posedge clk); + A <= 0; B <= 0; Ci <= 0; + @(posedge clk); + + A <= 0; B <= 0; Ci <= 1; + @(posedge clk); + + A <= 0; B <= 1; Ci <= 0; + @(posedge clk); + + A <= 0; B <= 1; Ci <= 1; + @(posedge clk); + + A <= 1; B <= 0; Ci <= 0; + @(posedge clk); + + A <= 1; B <= 0; Ci <= 1; + @(posedge clk); + + A <= 1; B <= 1; Ci <= 0; + @(posedge clk); + + A <= 1; B <= 1; Ci <= 1; + @(posedge clk); + + repeat(20) @(posedge clk); + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_D; + int errortime_D; + int errors_Co; + int errortime_Co; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic A; + logic B; + logic Ci; + logic D_ref; + logic D_dut; + logic Co_ref; + logic Co_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, A, B, Ci, D_ref, D_dut, Co_ref, Co_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .A, + .B, + .Ci + ); + + RefModule good1 ( + .A, + .B, + .Ci, + .D(D_ref), + .Co(Co_ref) + ); + + TopModule top_module1 ( + .A, + .B, + .Ci, + .D(D_dut), + .Co(Co_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_D) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "D", stats1.errors_D, stats1.errortime_D); + else $display("Hint: Output '%s' has no mismatches.", "D"); + if (stats1.errors_Co) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "Co", stats1.errors_Co, stats1.errortime_Co); + else $display("Hint: Output '%s' has no mismatches.", "Co"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { D_ref, Co_ref } === ( { D_ref, Co_ref } ^ { D_dut, Co_dut } ^ { D_ref, Co_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (D_ref !== ( D_ref ^ D_dut ^ D_ref )) + begin if (stats1.errors_D == 0) stats1.errortime_D = $time; + stats1.errors_D = stats1.errors_D+1'b1; end + if (Co_ref !== ( Co_ref ^ Co_dut ^ Co_ref )) + begin if (stats1.errors_Co == 0) stats1.errortime_Co = $time; + stats1.errors_Co = stats1.errors_Co+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_not_self_contain/Prob003_asynchronous_FIFO_prompt.txt b/Verilog Gen/dataset_not_self_contain/Prob003_asynchronous_FIFO_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..96c294f2171dbcb95360bcbc7ce3053c21909ec2 --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob003_asynchronous_FIFO_prompt.txt @@ -0,0 +1,67 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input wclk + - input rclk + - input wrstn + - input rrstn + - input winc + - input rinc + - input wdata (WIDTH bits) + - output wfull + - output rempty + - output rdata (WIDTH bits) + +The module should implement an asynchronous FIFO (First-In-First-Out) buffer +with parameterizable width and depth. The FIFO uses a dual-port RAM for +data storage and implements proper read/write pointer management with +clock domain crossing. + +The asynchronous FIFO should work as follows: +- Write interface: Uses wclk domain with winc (write enable) and wdata +- Read interface: Uses rclk domain with rinc (read enable) and rdata +- Reset: wrstn (write domain reset) and rrstn (read domain reset) +- Status signals: wfull (write full) and rempty (read empty) + +FIFO operation: +- Data is written when winc is high and wfull is low +- Data is read when rinc is high and rempty is low +- wfull indicates the FIFO is full and cannot accept more writes +- rempty indicates the FIFO is empty and has no data to read +- The FIFO should handle clock domain crossing properly +- wrstn and rrstn are asynchronous active-low resets, each for its own clock domain +- wfull goes high one wclk edge after the write that fills the FIFO, and rempty one rclk edge after + the read that empties it; a write (read) at that edge is still accepted +- A pointer change reaches the other domain one edge of its own clock plus two edges of the other clock later + + +Dual-port RAM module (provided): +```verilog +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk +,input wenc +,input [$clog2(DEPTH)-1:0] waddr +,input [WIDTH-1:0] wdata +,input rclk +,input renc +,input [$clog2(DEPTH)-1:0] raddr +,output reg [WIDTH-1:0] rdata +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin +if(wenc) +RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin +if(renc) +rdata <= RAM_MEM[raddr]; +end + +endmodule +``` +The final evaluation compiles this module together with TopModule.sv, so do not define a module named dual_port_RAM in TopModule.sv. diff --git a/Verilog Gen/dataset_not_self_contain/Prob003_asynchronous_FIFO_ref.sv b/Verilog Gen/dataset_not_self_contain/Prob003_asynchronous_FIFO_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..795d58f089d5e69688191a77ecbf071073eb49bd --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob003_asynchronous_FIFO_ref.sv @@ -0,0 +1,144 @@ + +module RefModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input wclk , + input rclk , + input wrstn , + input rrstn , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wire wfull , + output wire rempty , + output wire [WIDTH-1:0] rdata +); + +parameter ADDR_WIDTH = $clog2(DEPTH); + +/**********************addr bin gen*************************/ +reg [ADDR_WIDTH:0] waddr_bin; +reg [ADDR_WIDTH:0] raddr_bin; + +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + waddr_bin <= 'd0; + end + else if(!wfull && winc)begin + waddr_bin <= waddr_bin + 1'd1; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + raddr_bin <= 'd0; + end + else if(!rempty && rinc)begin + raddr_bin <= raddr_bin + 1'd1; + end +end + +/**********************addr gray gen*************************/ +wire [ADDR_WIDTH:0] waddr_gray; +wire [ADDR_WIDTH:0] raddr_gray; +reg [ADDR_WIDTH:0] wptr; +reg [ADDR_WIDTH:0] rptr; +assign waddr_gray = waddr_bin ^ (waddr_bin>>1); +assign raddr_gray = raddr_bin ^ (raddr_bin>>1); +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + wptr <= 'd0; + end + else begin + wptr <= waddr_gray; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + rptr <= 'd0; + end + else begin + rptr <= raddr_gray; + end +end +/**********************syn addr gray*************************/ +reg [ADDR_WIDTH:0] wptr_buff; +reg [ADDR_WIDTH:0] wptr_syn; +reg [ADDR_WIDTH:0] rptr_buff; +reg [ADDR_WIDTH:0] rptr_syn; +always @(posedge wclk or negedge wrstn) begin + if(~wrstn) begin + rptr_buff <= 'd0; + rptr_syn <= 'd0; + end + else begin + rptr_buff <= rptr; + rptr_syn <= rptr_buff; + end +end +always @(posedge rclk or negedge rrstn) begin + if(~rrstn) begin + wptr_buff <= 'd0; + wptr_syn <= 'd0; + end + else begin + wptr_buff <= wptr; + wptr_syn <= wptr_buff; + end +end +/**********************full empty gen*************************/ +assign wfull = (wptr == {~rptr_syn[ADDR_WIDTH:ADDR_WIDTH-1],rptr_syn[ADDR_WIDTH-2:0]}); +assign rempty = (rptr == wptr_syn); + +/**********************RAM*************************/ +wire wen ; +wire ren ; +wire wren;//high write +wire [ADDR_WIDTH-1:0] waddr; +wire [ADDR_WIDTH-1:0] raddr; +assign wen = winc & !wfull; +assign ren = rinc & !rempty; +assign waddr = waddr_bin[ADDR_WIDTH-1:0]; +assign raddr = raddr_bin[ADDR_WIDTH-1:0]; + +dual_port_RAM #(.DEPTH(DEPTH), + .WIDTH(WIDTH) +)dual_port_RAM( + .wclk (wclk), + .wenc (wen), + .waddr(waddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .wdata(wdata), //Data write + .rclk (rclk), + .renc (ren), + .raddr(raddr[ADDR_WIDTH-1:0]), //Take log2 of depth to get address bit width. + .rdata(rdata) //Data output +); + +endmodule + +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk + ,input wenc + ,input [$clog2(DEPTH)-1:0] waddr //Take log2 of depth to get address bit width. + ,input [WIDTH-1:0] wdata //Data write + ,input rclk + ,input renc + ,input [$clog2(DEPTH)-1:0] raddr //Take log2 of depth to get address bit width. + ,output reg [WIDTH-1:0] rdata //Data output +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin + if(wenc) + RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin + if(renc) + rdata <= RAM_MEM[raddr]; +end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_not_self_contain/Prob003_asynchronous_FIFO_test.sv b/Verilog Gen/dataset_not_self_contain/Prob003_asynchronous_FIFO_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..f1aeed44facc973689edaa5b4d63a9b6a84f574d --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob003_asynchronous_FIFO_test.sv @@ -0,0 +1,252 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic wclk, + output logic rclk, + output logic wrstn, + output logic rrstn, + output logic winc, + output logic rinc, + output logic [7:0] wdata + // === End your code here === +); + // === Start your code here === + reg wclk_reg = 0; + reg rclk_reg = 0; + assign wclk = wclk_reg; + assign rclk = rclk_reg; + + initial forever #10 wclk_reg = ~wclk_reg; // 50MHz write clock + initial forever #15 rclk_reg = ~rclk_reg; // 33.3MHz read clock + + initial begin + wrstn = 0; + rrstn = 0; + winc = 0; + rinc = 0; + wdata = 0; + repeat(20) @(posedge wclk); @(posedge rclk); + wrstn <= 1; + rrstn <= 1; + @(posedge wclk); @(posedge rclk); + + // Test basic write and read operations + winc <= 1; + wdata <= 8'hAA; @(posedge wclk); + wdata <= 8'h55; @(posedge wclk); + wdata <= 8'h33; @(posedge wclk); + wdata <= 8'hCC; @(posedge wclk); + winc <= 0; + @(posedge wclk); + + // Test read operations + rinc <= 1; + repeat(4) @(posedge rclk); + rinc <= 0; + @(posedge rclk); + + // Test simultaneous write and read + winc <= 1; + rinc <= 1; + wdata <= 8'h11; @(posedge wclk); @(posedge rclk); + wdata <= 8'h22; @(posedge wclk); @(posedge rclk); + wdata <= 8'h44; @(posedge wclk); @(posedge rclk); + wdata <= 8'h88; @(posedge wclk); @(posedge rclk); + winc <= 0; + rinc <= 0; + @(posedge wclk); + + // Test FIFO full condition + winc <= 1; + wdata <= 8'hF0; @(posedge wclk); + wdata <= 8'hF1; @(posedge wclk); + wdata <= 8'hF2; @(posedge wclk); + wdata <= 8'hF3; @(posedge wclk); + wdata <= 8'hF4; @(posedge wclk); + wdata <= 8'hF5; @(posedge wclk); + wdata <= 8'hF6; @(posedge wclk); + wdata <= 8'hF7; @(posedge wclk); + wdata <= 8'hF8; @(posedge wclk); + wdata <= 8'hF9; @(posedge wclk); + wdata <= 8'hFA; @(posedge wclk); + wdata <= 8'hFB; @(posedge wclk); + wdata <= 8'hFC; @(posedge wclk); + wdata <= 8'hFD; @(posedge wclk); + wdata <= 8'hFE; @(posedge wclk); + wdata <= 8'hFF; @(posedge wclk); + winc <= 0; + @(posedge wclk); + + // Test FIFO empty condition + rinc <= 1; + repeat(20) @(posedge rclk); // Should trigger rempty + rinc <= 0; + @(posedge rclk); + + // Random testing + repeat(200) @(posedge wclk, posedge rclk) begin + winc <= $random; + rinc <= $random; + wdata <= $random % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_wfull; + int errortime_wfull; + int errors_rempty; + int errortime_rempty; + int errors_rdata; + int errortime_rdata; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic wclk; + logic rclk; + logic wrstn; + logic rrstn; + logic winc; + logic rinc; + logic [7:0] wdata; + logic wfull_ref; + logic rempty_ref; + logic [7:0] rdata_ref; + logic wfull_dut; + logic rempty_dut; + logic [7:0] rdata_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, wclk, rclk, wrstn, rrstn, winc, rinc, wdata, wfull_ref, rempty_ref, rdata_ref, wfull_dut, rempty_dut, rdata_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .wclk, + .rclk, + .wrstn, + .rrstn, + .winc, + .rinc, + .wdata ); + + RefModule good1 ( + .wclk, + .rclk, + .wrstn, + .rrstn, + .winc, + .rinc, + .wdata, + .wfull(wfull_ref), + .rempty(rempty_ref), + .rdata(rdata_ref) ); + + TopModule top_module1 ( + .wclk, + .rclk, + .wrstn, + .rrstn, + .winc, + .rinc, + .wdata, + .wfull(wfull_dut), + .rempty(rempty_dut), + .rdata(rdata_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_wfull) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "wfull", stats1.errors_wfull, stats1.errortime_wfull); + else $display("Hint: Output '%s' has no mismatches.", "wfull"); + if (stats1.errors_rempty) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rempty", stats1.errors_rempty, stats1.errortime_rempty); + else $display("Hint: Output '%s' has no mismatches.", "rempty"); + if (stats1.errors_rdata) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rdata", stats1.errors_rdata, stats1.errortime_rdata); + else $display("Hint: Output '%s' has no mismatches.", "rdata"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { wfull_ref, rempty_ref, rdata_ref } === ( { wfull_ref, rempty_ref, rdata_ref } ^ { wfull_dut, rempty_dut, rdata_dut } ^ { wfull_ref, rempty_ref, rdata_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (wfull_ref !== ( wfull_ref ^ wfull_dut ^ wfull_ref )) + begin if (stats1.errors_wfull == 0) stats1.errortime_wfull = $time; + stats1.errors_wfull = stats1.errors_wfull+1'b1; end + if (rempty_ref !== ( rempty_ref ^ rempty_dut ^ rempty_ref )) + begin if (stats1.errors_rempty == 0) stats1.errortime_rempty = $time; + stats1.errors_rempty = stats1.errors_rempty+1'b1; end + if (rdata_ref !== ( rdata_ref ^ rdata_dut ^ rdata_ref )) + begin if (stats1.errors_rdata == 0) stats1.errortime_rdata = $time; + stats1.errors_rdata = stats1.errors_rdata+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_not_self_contain/Prob004_synchronous_FIFO_prompt.txt b/Verilog Gen/dataset_not_self_contain/Prob004_synchronous_FIFO_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..7f1e12eee88e4eb66ebc581ef43fb4405983cee9 --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob004_synchronous_FIFO_prompt.txt @@ -0,0 +1,66 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input winc + - input rinc + - input wdata (WIDTH bits) + - output wfull + - output rempty + - output rdata (WIDTH bits) + +The module should implement a synchronous FIFO (First In, First Out) buffer +that uses a dual-port RAM for data storage. The FIFO should be parameterizable +in both WIDTH and DEPTH, with configurable bit WIDTH and DEPTH parameters. + +The synchronous FIFO should work as follows: +- Input interface: + - clk: Single clock signal for both read and write operations + - rst_n: Active low reset signal + - winc: Write enable signal (pulse to write data) + - rinc: Read enable signal (pulse to read data) + - wdata: 8-bit input data to be written +- Output interface: + - wfull: FIFO full flag (high when FIFO is full) + - rempty: FIFO empty flag (high when FIFO is empty) + - rdata: 8-bit output data read from FIFO + +FIFO operation: +- Write operations occur on the positive edge of clk when winc is high +- Read operations occur on the positive edge of clk when rinc is high +- The FIFO uses a dual-port RAM for data storage +- Write and read pointers are managed to track FIFO status +- Full and empty conditions are detected based on pointer comparison +- The FIFO should handle simultaneous read and write operations correctly + +Dual-port RAM module (provided): +``` +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk +,input wenc +,input [$clog2(DEPTH)-1:0] waddr +,input [WIDTH-1:0] wdata +,input rclk +,input renc +,input [$clog2(DEPTH)-1:0] raddr +,output reg [WIDTH-1:0] rdata +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin + if(wenc) + RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin + if(renc) + rdata <= RAM_MEM[raddr]; +end + +endmodule +``` +The final evaluation compiles this module together with TopModule.sv, so do not define a module named dual_port_RAM in TopModule.sv. diff --git a/Verilog Gen/dataset_not_self_contain/Prob004_synchronous_FIFO_ref.sv b/Verilog Gen/dataset_not_self_contain/Prob004_synchronous_FIFO_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..56d8eef402a8c831f04631a322a0d7a520809e67 --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob004_synchronous_FIFO_ref.sv @@ -0,0 +1,88 @@ + +module RefModule#( + parameter WIDTH = 8, + parameter DEPTH = 16 +)( + input clk , + input rst_n , + input winc , + input rinc , + input [WIDTH-1:0] wdata , + + output wfull , + output rempty , + output wire [WIDTH-1:0] rdata +); + +localparam DP_WD = $clog2(DEPTH); + +reg [DP_WD :0]waddr; +wire wenc; +wire waddr_d_h; +wire [DP_WD -1:0]waddr_d_l; +assign wenc = winc & (!wfull); +assign waddr_d_h = (waddr[DP_WD-1:0] == DEPTH-1) ? ~waddr[DP_WD] : waddr[DP_WD]; +assign waddr_d_l = (waddr[DP_WD-1:0] == DEPTH-1) ? 0 : waddr[DP_WD-1:0] + 1; +always @(posedge clk or negedge rst_n)begin + if(~rst_n) waddr <= 0; + else if(wenc) waddr <= {waddr_d_h, waddr_d_l}; +end + +reg [DP_WD :0]raddr; +wire renc; +wire raddr_d_h; +wire [DP_WD -1:0]raddr_d_l; +assign renc = rinc & (!rempty); +assign raddr_d_h = (raddr[DP_WD-1:0] == DEPTH-1) ? ~raddr[DP_WD] : raddr[DP_WD]; +assign raddr_d_l = (raddr[DP_WD-1:0] == DEPTH-1) ? 0 : raddr[DP_WD-1:0] + 1; +always @(posedge clk or negedge rst_n)begin + if(~rst_n) raddr <= 0; + else if(renc) raddr <= {raddr_d_h, raddr_d_l}; +end + +wire [DP_WD :0]fifo_cnt = (waddr[DP_WD] == raddr[DP_WD]) ? waddr[DP_WD-1:0] - raddr[DP_WD-1:0]: + (waddr[DP_WD-1:0] + DEPTH - raddr[DP_WD-1:0]); + +assign rempty = (fifo_cnt == 0); +assign wfull = (fifo_cnt == DEPTH); + +dual_port_RAM #(.DEPTH(DEPTH), .WIDTH(WIDTH)) +u_ram ( + .wclk (clk), + .wenc (wenc), + .waddr (waddr[$clog2(DEPTH)-1:0]), + .wdata (wdata), + .rclk (clk), + .renc (renc), + .raddr (raddr[$clog2(DEPTH)-1:0]), + .rdata (rdata) +); + +endmodule + +/**********************************RAM************************************/ +module dual_port_RAM #(parameter DEPTH = 16, + parameter WIDTH = 8)( + input wclk + ,input wenc + ,input [$clog2(DEPTH)-1:0] waddr //Take log2 of depth to get address bit width. + ,input [WIDTH-1:0] wdata //Data write + ,input rclk + ,input renc + ,input [$clog2(DEPTH)-1:0] raddr //Take log2 of depth to get address bit width. + ,output reg [WIDTH-1:0] rdata //Data output +); + +reg [WIDTH-1:0] RAM_MEM [0:DEPTH-1]; + +always @(posedge wclk) begin + if(wenc) + RAM_MEM[waddr] <= wdata; +end + +always @(posedge rclk) begin + if(renc) + rdata <= RAM_MEM[raddr]; +end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_not_self_contain/Prob004_synchronous_FIFO_test.sv b/Verilog Gen/dataset_not_self_contain/Prob004_synchronous_FIFO_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..ce3e41170df8cd9e2bb3160877e307951fb99970 --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob004_synchronous_FIFO_test.sv @@ -0,0 +1,229 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic winc, + output logic rinc, + output logic [7:0] wdata + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + winc = 0; + rinc = 0; + wdata = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic write operations + winc <= 1; + wdata <= 8'hAA; @(posedge clk); + wdata <= 8'h55; @(posedge clk); + wdata <= 8'h33; @(posedge clk); + wdata <= 8'hCC; @(posedge clk); + winc <= 0; + @(posedge clk); + + // Test basic read operations + rinc <= 1; + repeat(4) @(posedge clk); + rinc <= 0; + @(posedge clk); + + // Test simultaneous write and read + winc <= 1; + rinc <= 1; + wdata <= 8'h11; @(posedge clk); + wdata <= 8'h22; @(posedge clk); + wdata <= 8'h44; @(posedge clk); + wdata <= 8'h88; @(posedge clk); + winc <= 0; + rinc <= 0; + @(posedge clk); + + // Test FIFO full condition + winc <= 1; + wdata <= 8'hF0; @(posedge clk); + wdata <= 8'hF1; @(posedge clk); + wdata <= 8'hF2; @(posedge clk); + wdata <= 8'hF3; @(posedge clk); + wdata <= 8'hF4; @(posedge clk); + wdata <= 8'hF5; @(posedge clk); + wdata <= 8'hF6; @(posedge clk); + wdata <= 8'hF7; @(posedge clk); + wdata <= 8'hF8; @(posedge clk); + wdata <= 8'hF9; @(posedge clk); + wdata <= 8'hFA; @(posedge clk); + wdata <= 8'hFB; @(posedge clk); + wdata <= 8'hFC; @(posedge clk); + wdata <= 8'hFD; @(posedge clk); + wdata <= 8'hFE; @(posedge clk); + wdata <= 8'hFF; @(posedge clk); // Should trigger wfull + winc <= 0; + @(posedge clk); + + // Test FIFO empty condition + rinc <= 1; + repeat(20) @(posedge clk); // Should trigger rempty + rinc <= 0; + @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + winc <= $random; + rinc <= $random; + wdata <= $random % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_wfull; + int errortime_wfull; + int errors_rempty; + int errortime_rempty; + int errors_rdata; + int errortime_rdata; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic winc; + logic rinc; + logic [7:0] wdata; + logic wfull_ref; + logic rempty_ref; + logic [7:0] rdata_ref; + logic wfull_dut; + logic rempty_dut; + logic [7:0] rdata_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, winc, rinc, wdata, wfull_ref, rempty_ref, rdata_ref, wfull_dut, rempty_dut, rdata_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .winc, + .rinc, + .wdata ); + + RefModule good1 ( + .clk, + .rst_n, + .winc, + .rinc, + .wdata, + .wfull(wfull_ref), + .rempty(rempty_ref), + .rdata(rdata_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .winc, + .rinc, + .wdata, + .wfull(wfull_dut), + .rempty(rempty_dut), + .rdata(rdata_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_wfull) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "wfull", stats1.errors_wfull, stats1.errortime_wfull); + else $display("Hint: Output '%s' has no mismatches.", "wfull"); + if (stats1.errors_rempty) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rempty", stats1.errors_rempty, stats1.errortime_rempty); + else $display("Hint: Output '%s' has no mismatches.", "rempty"); + if (stats1.errors_rdata) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "rdata", stats1.errors_rdata, stats1.errortime_rdata); + else $display("Hint: Output '%s' has no mismatches.", "rdata"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { wfull_ref, rempty_ref, rdata_ref } === ( { wfull_ref, rempty_ref, rdata_ref } ^ { wfull_dut, rempty_dut, rdata_dut } ^ { wfull_ref, rempty_ref, rdata_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (wfull_ref !== ( wfull_ref ^ wfull_dut ^ wfull_ref )) + begin if (stats1.errors_wfull == 0) stats1.errortime_wfull = $time; + stats1.errors_wfull = stats1.errors_wfull+1'b1; end + if (rempty_ref !== ( rempty_ref ^ rempty_dut ^ rempty_ref )) + begin if (stats1.errors_rempty == 0) stats1.errortime_rempty = $time; + stats1.errors_rempty = stats1.errors_rempty+1'b1; end + if (rdata_ref !== ( rdata_ref ^ rdata_dut ^ rdata_ref )) + begin if (stats1.errors_rdata == 0) stats1.errortime_rdata = $time; + stats1.errors_rdata = stats1.errors_rdata+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_not_self_contain/Prob005_8_bit_alu_prompt.txt b/Verilog Gen/dataset_not_self_contain/Prob005_8_bit_alu_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..328ea8ac5d82db1c59286c1a53da06b0cbe33ef9 --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob005_8_bit_alu_prompt.txt @@ -0,0 +1,119 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input in_A (8 bits) + - input in_B (8 bits) + - input in_operation (2 bits) + - output out_c (8 bits) + +This project implements a concise 8-bit Arithmetic Logic Unit (ALU). It +receives two 8-bit input operands (in_A and in_B) and a 2-bit control signal +(in_operation), and through the combination of internal bitwise AND, bitwise +OR, adder (ADDER), and multiplexer (MUX) modules, it implements four basic +operations: A AND B, A OR B, A + B, and A + ~B (A - B - 1 modulo 256). The design structure is clear +and modular, with each submodule responsible for a single function, and +finally the top-level ALU module selects the corresponding operation result +based on the control signal and outputs it to out_c. This design approach not +only facilitates understanding and debugging but also reflects the hierarchical +design philosophy in hardware systems. + +Port descriptions: + +Input ports: +- in_A (8 bits): The first operand of the ALU. It is an 8-bit binary number, + such as 8'b10101100. It is used in all operations (AND, OR, addition, + subtraction). + +- in_B (8 bits): The second operand. It is also an 8-bit binary number, such + as 8'b00111010. It participates in all calculations together with in_A. + +- in_operation (2 bits): This is the control signal that tells the ALU which + operation to perform. These 2 bits can be viewed as an opcode, where each + combination corresponds to a different operation: + - 00 → Perform bitwise AND (A AND B) + - 01 → Perform bitwise OR (A OR B) + - 10 → Perform addition (A + B) + - 11 → Perform subtraction as A + ~B (A - B - 1 modulo 256) + + The control signal structure: + - The low bit in_operation[0] simultaneously controls the OR/AND selection + and the adder's add/subtract mode. + - The high bit in_operation[1] controls whether the output is a logic + operation result or an arithmetic operation result. + +Output ports: +- out_c (8 bits): This is the final output result of the ALU. Its value + depends on the in_operation setting: + - When in_operation = 00 → Output A AND B + - When in_operation = 01 → Output A OR B + - When in_operation = 10 → Output A + B + - When in_operation = 11 → Output A + ~B (A - B - 1 modulo 256) + + Therefore, out_c may be either a logic result or an arithmetic result. + + +The following submodules are already provided and should be included as submodules in +your design: +- bit_8_AND: Performs bitwise AND operation on two 8-bit inputs +- bit_8_OR: Performs bitwise OR operation on two 8-bit inputs +- bit_8_ADDER: Performs addition or subtraction on two 8-bit inputs +- mux_2X1: A 2-to-1 multiplexer that selects between two 8-bit inputs +You need to add below code to implementation without any edit. +provided: +``` +//AND +module bit_8_AND(aANDb,A,B); + output reg [7:0] aANDb; + input [7:0]A,B; + always @(*) + begin + aANDb <= A&B; + end +endmodule + +//OR +module bit_8_OR( + output reg [7:0]aORb, + input [7:0]A, + input [7:0]B +); + always @(*) + begin + aORb <= A|B; + end +endmodule + +//8 bit adder +module bit_8_ADDER( + output reg [7:0]aPlusb, + input [7:0]A, + input [7:0]B, + input carry_in); + reg [7:0]BB; + always @(*) + begin + if(carry_in == 1) + BB <= ~B; + else + BB <= B; + aPlusb <= A + BB; + end +endmodule + +//2 X 1 multiplxer +module mux_2X1( + output reg [7:0]O, + input [7:0]I0, + input [7:0]I1, + input sel); + + always @(*) + begin + if(sel == 0) + O <= I0; + else + O <= I1; + end +endmodule +``` diff --git a/Verilog Gen/dataset_not_self_contain/Prob005_8_bit_alu_ref.sv b/Verilog Gen/dataset_not_self_contain/Prob005_8_bit_alu_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..31a69ce8a480e9f6878371edab60b4a04b01665f --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob005_8_bit_alu_ref.sv @@ -0,0 +1,18 @@ + +module RefModule( + input [7:0]in_A, + input [7:0]in_B, + input [1:0]in_operation, + output [7:0]out_c +); + + wire [7:0]out_and,out_or,out_adder,mux1; + + bit_8_AND i1(out_and,in_A,in_B); + bit_8_OR i2(out_or,in_A,in_B); + bit_8_ADDER i3(out_adder,in_A,in_B,in_operation[0]); + + mux_2X1 i4(mux1,out_and,out_or,in_operation[0]); + mux_2X1 i5(out_c,mux1,out_adder,in_operation[1]); + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_not_self_contain/Prob005_8_bit_alu_test.sv b/Verilog Gen/dataset_not_self_contain/Prob005_8_bit_alu_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..e1d2eceedaff39115625d48cb2b0e802bf620afe --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob005_8_bit_alu_test.sv @@ -0,0 +1,232 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output reg [7:0] in_A, + output reg [7:0] in_B, + output reg [1:0] in_operation + // === End your code here === +); + // === Start your code here === + initial begin + // Initialize signals + in_A = 8'b0; + in_B = 8'b0; + in_operation = 2'b0; + + @(posedge clk); + + // Test AND operation (in_operation = 00) + in_operation = 2'b00; + in_A = 8'b10101100; + in_B = 8'b00111010; + repeat(2) @(posedge clk); + + in_A = 8'b11111111; + in_B = 8'b00000000; + repeat(2) @(posedge clk); + + in_A = 8'b11111111; + in_B = 8'b11111111; + repeat(2) @(posedge clk); + + in_A = 8'b01010101; + in_B = 8'b10101010; + repeat(2) @(posedge clk); + + // Test OR operation (in_operation = 01) + in_operation = 2'b01; + in_A = 8'b10101100; + in_B = 8'b00111010; + repeat(2) @(posedge clk); + + in_A = 8'b11111111; + in_B = 8'b00000000; + repeat(2) @(posedge clk); + + in_A = 8'b00000000; + in_B = 8'b00000000; + repeat(2) @(posedge clk); + + in_A = 8'b01010101; + in_B = 8'b10101010; + repeat(2) @(posedge clk); + + // Test ADD operation (in_operation = 10) + in_operation = 2'b10; + in_A = 8'b00000001; + in_B = 8'b00000001; + repeat(2) @(posedge clk); + + in_A = 8'b11111111; + in_B = 8'b00000001; + repeat(2) @(posedge clk); + + in_A = 8'b01111111; + in_B = 8'b00000001; + repeat(2) @(posedge clk); + + in_A = 8'b10101100; + in_B = 8'b00111010; + repeat(2) @(posedge clk); + + in_A = 8'b00000000; + in_B = 8'b00000000; + repeat(2) @(posedge clk); + + // Test SUB operation (in_operation = 11) + in_operation = 2'b11; + in_A = 8'b00000010; + in_B = 8'b00000001; + repeat(2) @(posedge clk); + + in_A = 8'b00000001; + in_B = 8'b00000001; + repeat(2) @(posedge clk); + + in_A = 8'b10101100; + in_B = 8'b00111010; + repeat(2) @(posedge clk); + + in_A = 8'b11111111; + in_B = 8'b11111111; + repeat(2) @(posedge clk); + + in_A = 8'b00000000; + in_B = 8'b00000001; + repeat(2) @(posedge clk); + + // Random testing + repeat(1000) @(posedge clk) begin + in_A <= $urandom; + in_B <= $urandom; + in_operation <= $urandom; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_out_c; + int errortime_out_c; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic [7:0] in_A; + logic [7:0] in_B; + logic [1:0] in_operation; + logic [7:0] out_c_ref; + logic [7:0] out_c_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, in_A, in_B, in_operation, out_c_ref, out_c_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk(clk), + .in_A(in_A), + .in_B(in_B), + .in_operation(in_operation) + ); + + RefModule good1 ( + .in_A(in_A), + .in_B(in_B), + .in_operation(in_operation), + .out_c(out_c_ref) + ); + + TopModule top_module1 ( + .in_A(in_A), + .in_B(in_B), + .in_operation(in_operation), + .out_c(out_c_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_out_c) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out_c", stats1.errors_out_c, stats1.errortime_out_c); + else $display("Hint: Output '%s' has no mismatches.", "out_c"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { out_c_ref } === ( { out_c_ref } ^ { out_c_dut } ^ { out_c_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (out_c_ref !== ( out_c_ref ^ out_c_dut ^ out_c_ref )) + begin + if (stats1.errors_out_c == 0) stats1.errortime_out_c = $time; + stats1.errors_out_c = stats1.errors_out_c+1'b1; + end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule + diff --git a/Verilog Gen/dataset_not_self_contain/Prob006_cpu_top_prompt.txt b/Verilog Gen/dataset_not_self_contain/Prob006_cpu_top_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..97dfdd76d61cdf011adfc760aef4a51760cb77ea --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob006_cpu_top_prompt.txt @@ -0,0 +1,277 @@ +I would like you to implement a module named TopModule with the following +interface. + + - output readM + - output address (16 bits) + - inout data (16 bits) + - input inputReady + - input reset_n + - input clk + - output num_inst (16 bits) + - output output_port (16 bits) + +This CPU is a simplified 16-bit processor model. It contains basic stages +including Instruction Fetch, Decode, Execute, Memory Access, and Write Back. +The Control module generates various control signals to coordinate data flow +between the Register File (RF), Arithmetic Logic Unit (ALU), and memory. Each +instruction, the CPU updates the program counter (PC) based on the current +instruction (input from memory via the data bus), and executes arithmetic, +logic, jump, or store operations. When encountering the special WWD +instruction, it outputs register values to output_port, implementing a +"display result" functionality. + +Port descriptions: + +Input ports: +- inputReady: Indicates that data is ready from the input port +- reset_n: Active-low asynchronous RESET signal; while it is low, PC, num_inst and readM are 0. The fetch of address 0 (readM=1) starts as soon as reset_n is high +- clk: Clock signal + +Output ports: +- readM: Read from memory signal +- address: Current address for data (16 bits) +- num_inst: Number of instructions during execution (16 bits), for debugging/testing +- output_port: Used for WWD instruction to output register values (16 bits) + +Inout ports: +- data: Data being input or output (16 bits) + +Instruction Set: +The CPU supports the following instructions: +R-type instructions (function field determines operation): +- ADD (FUNC_ADD = 0): Register addition: rd = rs + rt +- SUB (FUNC_SUB = 1): Register subtraction: rd = rs - rt +- AND (FUNC_AND = 2): Bitwise AND: rd = rs & rt +- ORR (FUNC_ORR = 3): Bitwise OR: rd = rs | rt +- NOT (FUNC_NOT = 4): Bitwise NOT: rd = ~rs +- TCP (FUNC_TCP = 5): Two's complement: rd = -rs +- SHL (FUNC_SHL = 6): Shift left by 1: rd = rs << 1 +- SHR (FUNC_SHR = 7): Arithmetic shift right by 1: rd = rs >>> 1 +- JPR (FUNC_JPR = 25): Jump to register address: PC = rs +- JRL (FUNC_JRL = 26): Jump to register address and save return address: + rd = PC + 1; PC = rs +- RWD (FUNC_RWD = 27): Read from input port to register: reg = input port +- WWD (FUNC_WWD = 28): Write register value to output port: output port = reg +- HLT (FUNC_HLT = 29): Halt CPU execution +- ENI (FUNC_ENI = 30): Enable interrupt +- DSI (FUNC_DSI = 31): Disable interrupt + +I-type instructions (opcode determines operation): +- ADI (OPCODE_ADI = 4): Add immediate: rt = rs + imm +- ORI (OPCODE_ORI = 5): Bitwise OR immediate: rt = rs | imm +- LHI (OPCODE_LHI = 6): Load immediate to high 8 bits: rt = {imm, 8'b0} +- LWD (OPCODE_LWD = 7): Load from memory: rt = MEM[rs + imm] +- SWD (OPCODE_SWD = 8): Store to memory: MEM[rs + imm] = rt +- BNE (OPCODE_BNE = 0): Branch if not equal: if(rs != rt) PC += imm +- BEQ (OPCODE_BEQ = 1): Branch if equal: if(rs == rt) PC += imm +- BGZ (OPCODE_BGZ = 2): Branch if greater than zero: if(rs > 0) PC += imm +- BLZ (OPCODE_BLZ = 3): Branch if less than zero: if(rs < 0) PC += imm +- JMP (OPCODE_JMP = 9): Unconditional jump: PC = target +- JAL (OPCODE_JAL = 10): Jump and link: R3 = PC + 1; PC = target + +CPU Execution Flow: + +The CPU execution follows these stages: + +1. Instruction Fetch: + - When PC updates, assert readM=1 and set address=PC + - Wait for inputReady to become high + - When inputReady is asserted, read the instruction from the data bus + - Save the instruction for decoding + +2. Decode: + - Extract opcode (bits [15:12]), rs [11:10], rt [9:8], rd [7:6], imm [7:0] and function (bits [5:0]) from instruction + - Control module generates control signals based on opcode and function + - RF module reads source registers (rs and rt) based on instruction fields + +3. Execute: + - ALU performs computation based on ALUOp control signal + - For immediate instructions, sign-extend the immediate value + - ALU outputs result and branch condition (bcond) + +4. Memory Access: + - If instruction is LWD (Load Word) or SWD (Store Word): + * Set address to ALUResult (calculated memory address) + * Assert readM (for LWD) or MemWrite (for SWD) + * Wait for inputReady to become high + * For LWD: read data from data bus and save to dataReg + * For SWD: write ReadData2 to data bus + +5. Write Back: + - If RegWrite=1, write result to register file at next clock edge + - For LWD: write dataReg to destination register + - For other instructions: write ALUResult to destination register + - Destination register is determined by RegDst control signal + +6. PC Update: + - Calculate nextPC based on control signals: + * If Jump: nextPC = {PC[15:12], instruction[11:0]} + * Else if Branch & bcond: nextPC = PC + sign-extended(immediate) + * Else: nextPC = PC + 1 + - Update PC to nextPC at next clock edge + - An instruction without memory access has one execute cycle after the edge that captures it, with readM=0 and address=PC; the next edge writes the register file, updates PC and num_inst, and starts the next fetch (readM=1, address=nextPC) + +7. Output Stage (WWD instruction): + - If OpenPort=1 (WWD instruction), output ReadData1 to output_port + - output_port is driven only in the WWD's execute cycle; in every other cycle it is 16'bz + +The following submodules are already provided and should be instantiated and included in +your design, please do not edit this part: +- Control: Generates control signals based on instruction opcode and function + fields +- RF: Register File module for reading and writing register values +- ALU: Arithmetic Logic Unit module for performing arithmetic and logic + operations +provided: +``` +`define FUNC_ADD 6'd0 +`define FUNC_SUB 6'd1 +`define FUNC_AND 6'd2 +`define FUNC_ORR 6'd3 +`define FUNC_NOT 6'd4 +`define FUNC_TCP 6'd5 +`define FUNC_SHL 6'd6 +`define FUNC_SHR 6'd7 +`define FUNC_RWD 6'd27 +`define FUNC_WWD 6'd28 +`define FUNC_JPR 6'd25 +`define FUNC_JRL 6'd26 +`define FUNC_HLT 6'd29 +`define FUNC_ENI 6'd30 +`define FUNC_DSI 6'd31 + +`define OPCODE_ADI 4'd4 +`define OPCODE_ORI 4'd5 +`define OPCODE_LHI 4'd6 +`define OPCODE_LWD 4'd7 +`define OPCODE_SWD 4'd8 +`define OPCODE_BNE 4'd0 +`define OPCODE_BEQ 4'd1 +`define OPCODE_BGZ 4'd2 +`define OPCODE_BLZ 4'd3 +`define OPCODE_JMP 4'd9 +`define OPCODE_JAL 4'd10 +`define OPCODE_R 4'd15 + +module Control( + input [3:0] opcode, + input [5:0] func, + output RegDst, + output Jump, + output Branch, + output MemRead, + output MemtoReg, + output reg [3:0] ALUOp, + output MemWrite, + output ALUSrc, + output RegWrite, + output OpenPort + ); + + /* + Receives opcode and function code of an instruction, and generates control signals. + RegDst : Chooses RF write address (rt or rd) + Jump : Whether this instruction is a jump + Branch : Whether this instruction is a branch + MemRead : Whether we should read data memory + MemtoReg: Chooses what to write to RF (ALU result or data read from memory) + ALUOp : Controls ALU operation. Directly connected to the input OP of the ALU. (There is no ALU control module in this CPU) + MemWrite: Whether we should write data to memory + ALUSrc : Chooses ALU second input (Second RF read data or sign-extended immediate) + RegWrite: Whether we should write data to RF + OpenPort: Wheter to open output_port of CPU + */ + + assign RegDst = (opcode==`OPCODE_R); + assign Jump = (opcode==`OPCODE_JMP || opcode==`OPCODE_JAL); + assign Branch = (opcode==`OPCODE_BEQ || opcode==`OPCODE_BNE || opcode==`OPCODE_BGZ || opcode==`OPCODE_BLZ); + assign MemRead = (opcode==`OPCODE_LWD || opcode==`OPCODE_SWD); + assign MemtoReg = (opcode==`OPCODE_LWD); + assign MemWrite = (opcode==`OPCODE_SWD); + assign ALUSrc = !(opcode==`OPCODE_R || opcode==`OPCODE_BEQ || opcode==`OPCODE_BNE); + assign RegWrite = !(opcode==`OPCODE_SWD || opcode==`OPCODE_BEQ || opcode==`OPCODE_BNE || opcode==`OPCODE_BGZ || opcode==`OPCODE_BLZ || opcode==`OPCODE_JMP || func==`FUNC_WWD); + assign OpenPort = (opcode==`OPCODE_R & func==`FUNC_WWD); + + always @(*) begin + if (opcode==`OPCODE_R & func<8) ALUOp = func[3:0]; + else if (opcode==`OPCODE_ADI || opcode==`OPCODE_LWD || opcode==`OPCODE_SWD) ALUOp = 0; + else if (opcode==`OPCODE_ORI) ALUOp = 3; + else if (opcode==`OPCODE_LHI) ALUOp = 8; + else if (opcode==`OPCODE_BNE) ALUOp = 9; + else if (opcode==`OPCODE_BEQ) ALUOp = 10; + else if (opcode==`OPCODE_BGZ) ALUOp = 11; + else if (opcode==`OPCODE_BLZ) ALUOp = 12; + else ALUOp = 15; + end + +endmodule + +module RF( + input write, + input clk, + input reset_n, + input [1:0] addr1, + input [1:0] addr2, + input [1:0] addr3, + output [15:0] data1, + output [15:0] data2, + input [15:0] data3 + ); + + reg [63:0] register; + /* + register[63:48] == register[16*3+: 16] (addr is 2'b11) + register[47:32] == register[16*2+: 16] (addr is 2'b10) + register[31:16] == register[16*1+: 16] (addr is 2'b01) + register[15: 0] == register[16*0+: 16] (addr is 2'b00) + */ + + always @(posedge clk, negedge reset_n) begin + // Asynchronous active low reset -> This used to be Synchronous reset; modified to asynchronous for Project 4. + if (!reset_n) register <= 64'b0; + // Synchronous data write + else if (write) register[16*addr3+: 16] <= data3; + end + + // Asynchronous data read + assign data1 = register[16*addr1+: 16]; + assign data2 = register[16*addr2+: 16]; + +endmodule + +module ALU( + input signed [15:0] A, + input signed [15:0] B, + input [3:0] OP, + output reg [15:0] C, + output bcond + ); + + always @(*) begin + case (OP) + 0: C = A + B; // ADD, ADI, LWD, SWD + 1: C = A - B; // SUB + 2: C = A & B; // AND + 3: C = A | B; // ORR, ORI + 4: C = ~A; // NOT + 5: C = ~A + 1'b1; // TCP + 6: C = A << 1; // SHL + 7: C = A >>> 1; // SHR + 8: C = {B[7:0], 8'b0}; // LHI + 9: C = A - B; // BNE + 10: C = A - B; // BEQ + 11: C = A; // BGZ + 12: C = A; // BLZ + default: C = 16'bz; + endcase + end + + // Using assign, C and bcond change at the same time. + // The timing would have been different if they were inside a single always block. + assign bcond = OP==9 ? (C!=0) : // BNE + OP==10 ? (C==0) : // BEQ + OP==11 ? (C>0) : // BGZ + OP==12 ? (C<0) : 0; // BLZ + +endmodule +``` diff --git a/Verilog Gen/dataset_not_self_contain/Prob006_cpu_top_ref.sv b/Verilog Gen/dataset_not_self_contain/Prob006_cpu_top_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..6b14362621b6478d6ad5b20fa8b76552a2b401d8 --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob006_cpu_top_ref.sv @@ -0,0 +1,167 @@ + +module RefModule ( + output reg readM, // read from memory + output reg [`WORD_SIZE-1:0] address, // current address for data + inout [`WORD_SIZE-1:0] data, // data being input or output + input inputReady, // indicates that data is ready from the input port + input reset_n, // active-low RESET signal + input clk, // clock signal + + // for debuging/testing purpose + output reg [`WORD_SIZE-1:0] num_inst, // number of instruction during execution + output [`WORD_SIZE-1:0] output_port // this will be used for a "WWD" instruction +); + + // General CPU declarations + localparam FETCH = 3'd0; + localparam EXECUTE = 3'd1; + localparam MEMORY_READ = 3'd2; + localparam MEMORY_WRITE = 3'd3; + localparam WRITE_BACK = 3'd4; + localparam INPUT_READ = 3'd5; + localparam HALT = 3'd6; + + reg [2:0] state; + reg [`WORD_SIZE-1:0] PC, nextPC; + reg [`WORD_SIZE-1:0] instruction, dataReg; // Because data is only available for a short time, we need to save it. + + // Control signals from control module + wire RegDst, Jump, Branch, MemRead, MemtoReg, MemWrite, ALUSrc, RegWrite, OpenPort; + wire [3:0] ALUOp; + + // RF related declarations + wire [`WORD_SIZE-1:0] ReadData1, ReadData2; + wire [1:0] WriteAddress; + wire [`WORD_SIZE-1:0] WriteData; + wire WriteEnable; + + // ALU related declarations + wire bcond; + wire [`WORD_SIZE-1:0] ALUResult; + + // Actual modul declarations + Control control(.opcode(instruction[15:12]), + .func(instruction[5:0]), + .RegDst(RegDst), + .Jump(Jump), + .Branch(Branch), + .MemRead(MemRead), + .MemtoReg(MemtoReg), + .ALUOp(ALUOp), + .MemWrite(MemWrite), + .ALUSrc(ALUSrc), + .RegWrite(RegWrite), + .OpenPort(OpenPort)); + + assign WriteAddress = (instruction[15:12] == `OPCODE_JAL) ? 2'd3 : + (RegDst ? instruction[7:6] : instruction[9:8]); + assign WriteData = (instruction[15:12] == `OPCODE_JAL || + (instruction[15:12] == `OPCODE_R && instruction[5:0] == `FUNC_JRL)) ? PC + 1'b1 : + ((state == WRITE_BACK) ? dataReg : ALUResult); + assign WriteEnable = (state == WRITE_BACK) || + (state == EXECUTE && RegWrite && !MemtoReg && !MemWrite && + !(instruction[15:12] == `OPCODE_R && + (instruction[5:0] == `FUNC_RWD || instruction[5:0] == `FUNC_WWD || + instruction[5:0] == `FUNC_JPR || instruction[5:0] == `FUNC_HLT || + instruction[5:0] == `FUNC_ENI || instruction[5:0] == `FUNC_DSI))); + + RF rf(.write(WriteEnable), + .clk(clk), + .reset_n(reset_n), + .addr1(instruction[11:10]), + .addr2(instruction[9:8]), + .addr3(WriteAddress), + .data1(ReadData1), + .data2(ReadData2), + .data3(WriteData)); + + ALU alu(.A(ReadData1), + .B(ALUSrc ? {{8{instruction[7]}}, instruction[7:0]} : ReadData2), // Either sign-extended immediate or second register read from RF. + .OP(ALUOp), + .C(ALUResult), + .bcond(bcond)); + + always @(*) begin + if (instruction[15:12] == `OPCODE_R && + (instruction[5:0] == `FUNC_JPR || instruction[5:0] == `FUNC_JRL)) + nextPC = ReadData1; + else if (Jump) nextPC = {PC[15:12], instruction[11:0]}; // Jumping to target (12 bit), concatenated with the upper 4 bits of PC. + else if (Branch & bcond) nextPC = PC + {{8{instruction[7]}}, instruction[7:0]}; // Branching to PC + sign-extend(immediate) + else nextPC = PC + 1; // Next instruction + end + + always @(*) begin + readM = 1'b0; + address = PC; + + if (reset_n) begin + case (state) + FETCH: readM = 1'b1; + MEMORY_READ: begin + readM = 1'b1; + address = ALUResult; + end + MEMORY_WRITE: address = ALUResult; + endcase + end + end + + assign data = (state == MEMORY_WRITE) ? ReadData2 : `WORD_SIZE'bz; + + // Only open when OpenPort is asserted by the control module + assign output_port = (state == EXECUTE && OpenPort) ? ReadData1 : `WORD_SIZE'bz; + + always @(posedge clk, negedge reset_n) begin + if (!reset_n) begin // Asynchronous active low reset + PC <= 0; + instruction <= 0; + dataReg <= 0; + num_inst <= 0; + state <= FETCH; + end else begin + case (state) + FETCH: begin + if (inputReady) begin + instruction <= data; + state <= EXECUTE; + end + end + EXECUTE: begin + if (instruction[15:12] == `OPCODE_R && instruction[5:0] == `FUNC_HLT) begin + num_inst <= num_inst + 1'b1; + state <= HALT; + end else if (MemtoReg) begin + state <= MEMORY_READ; + end else if (MemWrite) begin + state <= MEMORY_WRITE; + end else if (instruction[15:12] == `OPCODE_R && instruction[5:0] == `FUNC_RWD) begin + state <= INPUT_READ; + end else begin + PC <= nextPC; + num_inst <= num_inst + 1'b1; + state <= FETCH; + end + end + MEMORY_READ, INPUT_READ: begin + if (inputReady) begin + dataReg <= data; + state <= WRITE_BACK; + end + end + MEMORY_WRITE: begin + PC <= nextPC; + num_inst <= num_inst + 1'b1; + state <= FETCH; + end + WRITE_BACK: begin + PC <= nextPC; + num_inst <= num_inst + 1'b1; + state <= FETCH; + end + HALT: state <= HALT; + default: state <= FETCH; + endcase + end + end + +endmodule diff --git a/Verilog Gen/dataset_not_self_contain/Prob006_cpu_top_test.sv b/Verilog Gen/dataset_not_self_contain/Prob006_cpu_top_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..8e61e30f724c6f163a79d588e3df62201d17cd3b --- /dev/null +++ b/Verilog Gen/dataset_not_self_contain/Prob006_cpu_top_test.sv @@ -0,0 +1,304 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 +`define WORD_SIZE 16 + +`define FUNC_ADD 6'd0 +`define FUNC_SUB 6'd1 +`define FUNC_AND 6'd2 +`define FUNC_ORR 6'd3 +`define FUNC_NOT 6'd4 +`define FUNC_TCP 6'd5 +`define FUNC_SHL 6'd6 +`define FUNC_SHR 6'd7 +`define FUNC_RWD 6'd27 +`define FUNC_WWD 6'd28 +`define FUNC_JPR 6'd25 +`define FUNC_JRL 6'd26 +`define FUNC_HLT 6'd29 +`define FUNC_ENI 6'd30 +`define FUNC_DSI 6'd31 + +`define OPCODE_ADI 4'd4 +`define OPCODE_ORI 4'd5 +`define OPCODE_LHI 4'd6 +`define OPCODE_LWD 4'd7 +`define OPCODE_SWD 4'd8 +`define OPCODE_BNE 4'd0 +`define OPCODE_BEQ 4'd1 +`define OPCODE_BGZ 4'd2 +`define OPCODE_BLZ 4'd3 +`define OPCODE_JMP 4'd9 +`define OPCODE_JAL 4'd10 +`define OPCODE_R 4'd15 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output reg reset_n, + output reg inputReady, + inout [`WORD_SIZE-1:0] data, + input readM_ref, + input readM_dut, + input [`WORD_SIZE-1:0] address_ref, + input [`WORD_SIZE-1:0] address_dut + // === End your code here === +); + // === Start your code here === + reg [`WORD_SIZE-1:0] mem [0:65535]; + reg readM_ref_prev, readM_dut_prev; + reg [`WORD_SIZE-1:0] data_out; + reg driving; + + assign data = driving ? data_out : `WORD_SIZE'bz; + + initial begin + integer i; + for (i = 0; i < 65536; i = i + 1) + mem[i] = '0; + + // Instruction program exercising arithmetic, branching, jumps, and WWD + mem[0] = {`OPCODE_ADI, 2'd0, 2'd1, 8'd5}; // R1 <- 5 + mem[1] = {`OPCODE_ADI, 2'd0, 2'd2, 8'hFD}; // R2 <- -3 + mem[2] = {`OPCODE_R, 2'd1, 2'd2, 2'd3, `FUNC_ADD}; // R3 <- R1 + R2 = 2 + mem[3] = {`OPCODE_R, 2'd3, 2'd1, 2'd2, `FUNC_SUB}; // R2 <- R3 - R1 = -3 (consistency check) + mem[4] = {`OPCODE_R, 2'd3, 2'd1, 2'd3, `FUNC_ORR}; // R3 <- R3 | R1 = 7 + mem[5] = {`OPCODE_R, 2'd3, 2'd0, 2'd0, `FUNC_WWD}; // Output R3 + mem[6] = {`OPCODE_BEQ, 2'd3, 2'd3, 8'd3}; // Always taken -> jump to 9 + mem[7] = {`OPCODE_ADI, 2'd0, 2'd1, 8'd99}; // Skipped when branch works + mem[8] = {`OPCODE_ADI, 2'd0, 2'd2, 8'd77}; // Skipped when branch works + mem[9] = {`OPCODE_LHI, 2'd0, 2'd1, 8'h12}; // R1 <- 0x1200 + mem[10] = {`OPCODE_ORI, 2'd1, 2'd1, 8'h34}; // R1 <- 0x1234 + mem[11] = {`OPCODE_R, 2'd1, 2'd0, 2'd0, `FUNC_WWD}; // Output R1 + mem[12] = {`OPCODE_JMP, 12'd9}; // Loop within program + + // Example data memory locations for potential loads + mem[256] = 16'hAAAA; + mem[257] = 16'h5555; + + reset_n = 1'b0; + inputReady = 1'b0; + driving = 1'b0; + readM_ref_prev = 1'b0; + readM_dut_prev = 1'b0; + data_out = '0; + + // Assert reset for a few cycles + repeat (5) @(posedge clk); + + reset_n = 1'b1; + + // Allow CPU to execute for many cycles + repeat (6000) @(posedge clk); + + // Pulse reset again to check restart behavior + reset_n = 1'b0; + repeat (4) @(posedge clk); + reset_n = 1'b1; + repeat (2000) @(posedge clk); + + #1 $finish; + end + + always @(posedge clk) begin + readM_ref_prev <= readM_ref; + readM_dut_prev <= readM_dut; + + if (readM_ref && !readM_ref_prev) begin + data_out <= mem[address_ref]; + driving <= 1'b1; + inputReady <= 1'b1; + end else if (readM_dut && !readM_dut_prev) begin + data_out <= mem[address_dut]; + driving <= 1'b1; + inputReady <= 1'b1; + end else begin + inputReady <= 1'b0; + if (!readM_ref && !readM_dut) + driving <= 1'b0; + end + end + // === End your code here === + +endmodule + + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_readM; + int errortime_readM; + int errors_address; + int errortime_address; + int errors_num_inst; + int errortime_num_inst; + int errors_output_port; + int errortime_output_port; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk = 0; + initial forever #5 clk = ~clk; + + // === Start your code here === + logic reset_n; + logic inputReady; + wire [`WORD_SIZE-1:0] data; + logic readM_ref; + logic readM_dut; + logic [`WORD_SIZE-1:0] address_ref; + logic [`WORD_SIZE-1:0] address_dut; + logic [`WORD_SIZE-1:0] num_inst_ref; + logic [`WORD_SIZE-1:0] num_inst_dut; + logic [`WORD_SIZE-1:0] output_port_ref; + logic [`WORD_SIZE-1:0] output_port_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, reset_n, inputReady, readM_ref, readM_dut, address_ref, address_dut, data, num_inst_ref, num_inst_dut, output_port_ref, output_port_dut); + // === End your code here === + end + + wire tb_match; + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk(clk), + .reset_n(reset_n), + .inputReady(inputReady), + .data(data), + .readM_ref(readM_ref), + .readM_dut(readM_dut), + .address_ref(address_ref), + .address_dut(address_dut) + ); + + RefModule good1 ( + .clk(clk), + .reset_n(reset_n), + .inputReady(inputReady), + .data(data), + .readM(readM_ref), + .address(address_ref), + .num_inst(num_inst_ref), + .output_port(output_port_ref) + ); + + TopModule top_module1 ( + .clk(clk), + .reset_n(reset_n), + .inputReady(inputReady), + .data(data), + .readM(readM_dut), + .address(address_dut), + .num_inst(num_inst_dut), + .output_port(output_port_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat (5) begin + strobe <= !strobe; + @(strobe); + end + endtask + + initial stats1 = '0; + + final begin + // === Start your code here === + if (stats1.errors_readM) + $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "readM", stats1.errors_readM, stats1.errortime_readM); + else + $display("Hint: Output '%s' has no mismatches.", "readM"); + + if (stats1.errors_address) + $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "address", stats1.errors_address, stats1.errortime_address); + else + $display("Hint: Output '%s' has no mismatches.", "address"); + + if (stats1.errors_num_inst) + $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "num_inst", stats1.errors_num_inst, stats1.errortime_num_inst); + else + $display("Hint: Output '%s' has no mismatches.", "num_inst"); + + if (stats1.errors_output_port) + $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "output_port", stats1.errors_output_port, stats1.errortime_output_port); + else + $display("Hint: Output '%s' has no mismatches.", "output_port"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + assign tb_match = ({readM_ref, address_ref, num_inst_ref} === + ({readM_ref, address_ref, num_inst_ref} ^ + {readM_dut, address_dut, num_inst_dut} ^ + {readM_ref, address_ref, num_inst_ref})) && + op_ok; + // output_port: 'z where the reference drives 'z (the spec states 16'bz), exact elsewhere; an X in the reference + // (before the first reset edge) matches anything, as for the other outputs. + wire op_ok = (output_port_ref === output_port_dut) || + (output_port_ref === (output_port_ref ^ output_port_dut ^ output_port_ref)); + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) + stats1.errortime = $time; + stats1.errors++; + end + + // === Start your code here === + if (readM_ref !== (readM_ref ^ readM_dut ^ readM_ref)) begin + if (stats1.errors_readM == 0) + stats1.errortime_readM = $time; + stats1.errors_readM++; + end + + if (address_ref !== (address_ref ^ address_dut ^ address_ref)) begin + if (stats1.errors_address == 0) + stats1.errortime_address = $time; + stats1.errors_address++; + end + + if (num_inst_ref !== (num_inst_ref ^ num_inst_dut ^ num_inst_ref)) begin + if (stats1.errors_num_inst == 0) + stats1.errortime_num_inst = $time; + stats1.errors_num_inst++; + end + + if (!op_ok) begin + if (stats1.errors_output_port == 0) + stats1.errortime_output_port = $time; + stats1.errors_output_port++; + end + // === End your code here === + end + + initial begin + #1000000; + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob000_Four-to-one_multiplexer_prompt.txt b/Verilog Gen/dataset_self_contain/Prob000_Four-to-one_multiplexer_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..41574f48d6141dd993bcfd9d79b1a72379852c53 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob000_Four-to-one_multiplexer_prompt.txt @@ -0,0 +1,54 @@ +### **Task Description** +Design a **4-to-1 multiplexer (4:1 MUX)** in Verilog. +The output must be defined as a **wire** type, and the logic should be implemented using an `assign` statement. + +--- + +### **Signal Description** + +**Inputs:** +I would like you to implement a module named TopModule with the following +interface. +- `d0`, `d1`, `d2`, `d3` — 2-bit data inputs (`[1:0]`, type: `wire`) +- `sel` — 2-bit selection signal (`[1:0]`, type: `wire`) + +**Output:** +- `mux_out` — 2-bit output signal (`[1:0]`, type: `wire`) + +--- + +### **Selection Behavior** + +| `sel` | Output (`mux_out`) | +| ----- | ------------------ | +| 00 | d0 | +| 01 | d1 | +| 10 | d2 | +| 11 | d3 | + +--- + +### **Given Input Values** + +| Signal | Binary Value | +| ------ | ------------- | +| d0 | 11 | +| d1 | 10 | +| d2 | 01 | +| d3 | 00 | + +--- + +### **Waveform Explanation** + +* The first diagram shows the 4:1 multiplexer connections: + * Inputs: `d0`, `d1`, `d2`, `d3`, and `sel` + * Output: `mux_out` +* The second diagram shows simulation waveforms: + * As `sel` changes (00 → 01 → 10 → 11), `mux_out` updates accordingly (11 → 10 → 01 → 00) + +--- + +### **Question Prompt** + +Write the **Verilog code** for this 4-to-1 multiplexer using a **wire-type output** and a single **assign** statement. diff --git a/Verilog Gen/dataset_self_contain/Prob000_Four-to-one_multiplexer_ref.sv b/Verilog Gen/dataset_self_contain/Prob000_Four-to-one_multiplexer_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..c1eec4dfca6b60f15daab9e13ec6438170a3e7fb --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob000_Four-to-one_multiplexer_ref.sv @@ -0,0 +1,9 @@ + +module RefModule( + input [1:0] d0, d1, d2, d3, + input [1:0] sel, + output [1:0] mux_out +); + assign mux_out = sel[1] ? (sel[0] ? d3 : d2) + : (sel[0] ? d1 : d0); +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob000_Four-to-one_multiplexer_test.sv b/Verilog Gen/dataset_self_contain/Prob000_Four-to-one_multiplexer_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..55a69c46402b5fc7161dff923c9e21c3eef7c49d --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob000_Four-to-one_multiplexer_test.sv @@ -0,0 +1,143 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + output logic [1:0] d0, d1, d2, d3, + output logic [1:0] sel +); + // === Start your code here === + initial begin + repeat(100) @(posedge clk, negedge clk) begin + d0 <= $urandom_range(0,3); + d1 <= $urandom_range(0,3); + d2 <= $urandom_range(0,3); + d3 <= $urandom_range(0,3); + sel <= $urandom_range(0,3); + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_out; + int errortime_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic mux_out_ref; + logic mux_out_dut; + logic [1:0] d0, d1, d2, d3; + logic [1:0] sel; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, d0, d1, d2, d3, sel, mux_out_ref, mux_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .d0, + .d1, + .d2, + .d3, + .sel ); + + RefModule good1 ( + .d0, + .d1, + .d2, + .d3, + .sel, + .mux_out(mux_out_ref) ); + + TopModule top_module1 ( + .d0, + .d1, + .d2, + .d3, + .sel, + .mux_out(mux_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out", stats1.errors_out, stats1.errortime_out); + else $display("Hint: Output '%s' has no mismatches.", "out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { mux_out_ref } === ( { mux_out_ref } ^ { mux_out_dut } ^ { mux_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (mux_out_ref !== ( mux_out_ref ^ mux_out_dut ^ mux_out_ref )) + begin if (stats1.errors_out == 0) stats1.errortime_out = $time; + stats1.errors_out = stats1.errors_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule + diff --git a/Verilog Gen/dataset_self_contain/Prob001_continuous_input_sequence_detect_prompt.txt b/Verilog Gen/dataset_self_contain/Prob001_continuous_input_sequence_detect_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..64b1cf3168e935317b23bb11751c0b02b1ab70b1 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob001_continuous_input_sequence_detect_prompt.txt @@ -0,0 +1,25 @@ +I would like you to implement a module named `TopModule` with the following interface. All input and output ports are one bit unless otherwise specified. + +- **Input ports**: + - `clk` + - `rst_n` + - `a` + +- **Output ports**: + - `match` + +The module should implement an 8-bit shift register that shifts in the input `a` on every positive edge of the clock. The shift register is named `a_tem` and retains the 8 most recent bits of `a`. The module should perform the following functionality: + +1. **Reset Behavior**: + - When `rst_n` is de-asserted (logic 0), the shift register `a_tem` should be cleared to `8'b0`, and the output `match` should be set to `0`. + +2. **Shift Register Behavior**: + - On every positive edge of the clock, if `rst_n` is asserted (logic 1), the input `a` should be shifted into the least significant bit (LSB) of the shift register `a_tem`, and the previous bits should shift left by one position. + +3. **Matching Logic**: + - The output `match` should be asserted (logic 1) whenever the value of the shift register `a_tem` matches the specific 8-bit pattern `8'b0111_0001`. + - If the shift register does not match this pattern, the output `match` should remain de-asserted (logic 0). + +The design must operate synchronously with the clock and use a negative edge active reset (`rst_n`). Ensure the module correctly implements this functionality. + +Clarification: match is a registered output. On each clock edge, match is set to the result of comparing the value the shift register held as of the previous clock edge against the target pattern, so match lags the shift register by one additional cycle (1-cycle latency). \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob001_continuous_input_sequence_detect_ref.sv b/Verilog Gen/dataset_self_contain/Prob001_continuous_input_sequence_detect_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..7f331ec29a6c80c9534b2e3dc96a5cc965d76324 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob001_continuous_input_sequence_detect_ref.sv @@ -0,0 +1,33 @@ +module RefModule( + input clk, + input rst_n, + input a, + output reg match + ); + + reg [7:0] a_tem; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match <= 1'b0; + end + else if (a_tem == 8'b0111_0001) + begin + match <= 1'b1; + end + else + begin + match <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 8'b0; + end + else + begin + a_tem <= {a_tem[6:0],a}; + end +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob001_continuous_input_sequence_detect_test.sv b/Verilog Gen/dataset_self_contain/Prob001_continuous_input_sequence_detect_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..56fff2845c0e13a454426921612199ae849048d5 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob001_continuous_input_sequence_detect_test.sv @@ -0,0 +1,190 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 +`default_nettype none + +module stimulus_gen ( + input wire clk, + output reg rst_n, + output reg a, + input wire tb_match, + input wire match_dut +); + bit failed = 0; + // additional check stimulus vectors (applied left to right) + localparam bit [13:0] OVL_BITS = 14'b01110001110001; + localparam bit [6:0] PRE_BITS = 7'b0111000; + bit ovl [14]; + bit pre [7]; + initial begin + for (int i = 0; i < 14; i++) ovl[i] = OVL_BITS[13-i]; + for (int i = 0; i < 7; i++) pre[i] = PRE_BITS[6-i]; + end + + always @(posedge clk, negedge clk) + if (!tb_match) + failed <= 1; + + initial begin + @(posedge clk); + failed <= 0; + rst_n <= 0; + a <= 0; + + // Initial reset + @(posedge clk); + rst_n <= 1; + + // Test Case 1: No match + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + @(posedge clk) a <= 1; + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + + // Test Case 2: Matching sequence (0111_0001) + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + @(posedge clk) a <= 1; + @(posedge clk) a <= 1; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + @(posedge clk) a <= 0; + @(posedge clk) a <= 1; + + // Test Case 3: Randomized input + repeat (100) @(posedge clk) begin + a <= $random; + end + + // Test Case 4: Edge cases with reset + @(posedge clk); + rst_n <= 0; + @(posedge clk); + rst_n <= 1; + repeat (8) @(posedge clk) a <= 1; + + // Add delay to observe outputs + repeat (100) @(posedge clk); + + // Additional checks, added after all original stimulus: + // (1) overlapping occurrences: the spec asserts match whenever a_tem equals 8'b0111_0001, + // so 0111_0001_110001 must give two matches six cycles apart. + foreach (ovl[i]) @(posedge clk) a <= ovl[i]; + repeat (10) @(posedge clk) a <= 1; + // (2) reset clears a_tem: shift in the first seven pattern bits, reset (match is 0 and a_tem is + // not the pattern when rst_n falls), then shift the last pattern bit. A cleared a_tem holds + // 8'b0000_0001 (no match); an uncleared one would complete the pattern. + foreach (pre[i]) @(posedge clk) a <= pre[i]; + @(posedge clk) begin rst_n <= 0; a <= 1; end + @(posedge clk) rst_n <= 1; + repeat (12) @(posedge clk) a <= 1; + // (3) long random run (more pattern occurrences than the original 100 random bits). + repeat (3000) @(posedge clk) a <= $urandom; + repeat (10) @(posedge clk) a <= 0; + + #1 $finish; + end +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + + int clocks; + } stats; + + stats stats1; + + wire [511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk = 0; + initial forever #5 clk = ~clk; + + logic rst_n; + logic a; + logic match_ref; + logic match_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, a, match_ref, match_dut); + end + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + stimulus_gen stim1 ( + .clk(clk), + .rst_n(rst_n), + .a(a), + .tb_match(tb_match), + .match_dut(match_dut) + ); + + RefModule good1 ( + .clk(clk), + .rst_n(rst_n), + .a(a), + .match(match_ref) + ); + + TopModule top_module1 ( + .clk(clk), + .rst_n(rst_n), + .a(a), + .match(match_dut) + ); + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat (5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + final begin + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( + {match_ref} === + ({match_ref} ^ {match_dut} ^ {match_ref}) + ); + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (match_ref !== (match_ref ^ match_dut ^ match_ref)) begin + if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match++; + end + end + + // Add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob002_extraneous_items_sequence_detect_prompt.txt b/Verilog Gen/dataset_self_contain/Prob002_extraneous_items_sequence_detect_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..947476db865f58e8c564e590c5974958a2dbc1f6 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob002_extraneous_items_sequence_detect_prompt.txt @@ -0,0 +1,34 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input a + - output match + +The module should implement a sequence detection circuit that detects the +pattern "011XXX110" in the input data stream. The pattern consists of 9 bits +where the first 3 bits are "011", the last 3 bits are "110", and the middle +3 bits can be any value (don't care). + +The sequence detection should work as follows: +- Monitor the input data stream continuously +- Detect when the pattern "011XXX110" appears in the input +- Output match signal should be high when the pattern is detected +- Output match signal should be low at all other times + +Implementation approach: +The module should use a 9-bit shift register to store the last 9 input bits. +The match signal should be asserted when: +- The first 3 bits (bits 8:6) equal "011" AND +- The last 3 bits (bits 2:0) equal "110" +- The middle 3 bits (bits 5:3) can be any value + +### Timing and Behavior: +- When rst_n is low, the module should reset to its initial state asynchronously +- On every positive clock edge (clk), the module should shift in the new input bit +- The output match is updated sequentially +- The detection is continuous - the module checks for the pattern on every clock cycle + +Clarification: match is produced with two cycles of latency. The front-half and back-half checks are each registered once and then ANDed together, so match goes high on the second rising clk edge after the pattern's last bit is applied to a. diff --git a/Verilog Gen/dataset_self_contain/Prob002_extraneous_items_sequence_detect_ref.sv b/Verilog Gen/dataset_self_contain/Prob002_extraneous_items_sequence_detect_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..9aa18d6688f374e6ffa77394980b17c15b7107df --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob002_extraneous_items_sequence_detect_ref.sv @@ -0,0 +1,51 @@ +module RefModule( + input clk, + input rst_n, + input a, + output match + ); + + reg [8:0] a_tem; + reg match_f; + reg match_b; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match_f <= 1'b0; + end + else if (a_tem[8:6] == 3'b011) + begin + match_f <= 1'b1; + end + else + begin + match_f <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + match_b <= 1'b0; + end + else if (a_tem[2:0] == 3'b110) + begin + match_b <= 1'b1; + end + else + begin + match_b <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + a_tem <= 9'b0; + end + else + begin + a_tem <= {a_tem[7:0],a}; + end + + assign match = match_b && match_f; +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob002_extraneous_items_sequence_detect_test.sv b/Verilog Gen/dataset_self_contain/Prob002_extraneous_items_sequence_detect_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..7ebedeb9cae8bb2584af627b24db2b13c1bc3147 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob002_extraneous_items_sequence_detect_test.sv @@ -0,0 +1,178 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic a, + output logic rst_n, + output reg[511:0] wavedrom_title, + output reg wavedrom_enable, + input tb_match + // === End your code here === +); + // === Start your code here === + reg reset; + assign rst_n = ~reset; + + task wavedrom_start(input[511:0] title = ""); + endtask + + task wavedrom_stop; + #1; + endtask + + initial begin + reset = 1; + a = 0; + @(posedge clk); + reset = 0; + + wavedrom_start("Sequence Detection Test"); + + // Test specific sequences for "011XXX110" pattern + @(posedge clk) a = 0; // Start of 011101110 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 1; // Middle bit 1 + @(posedge clk) a = 0; // Middle bit 0 + @(posedge clk) a = 1; // Middle bit 1 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 0; // Should detect pattern here + + @(posedge clk) a = 0; // Start of 011001110 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 0; // Middle bit 0 + @(posedge clk) a = 0; // Middle bit 0 + @(posedge clk) a = 1; // Middle bit 1 + @(posedge clk) a = 1; + @(posedge clk) a = 1; + @(posedge clk) a = 0; // Should detect pattern here + + wavedrom_stop(); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + a <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic a; + logic rst_n; + logic match_ref; + logic match_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, a, rst_n, match_ref, match_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .* , + .a, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .a, + .match(match_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .a, + .match(match_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { match_ref } === ( { match_ref } ^ { match_dut } ^ { match_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (match_ref !== ( match_ref ^ match_dut ^ match_ref )) + begin if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match = stats1.errors_match+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob003_non-overlapping_sequence_detect_prompt.txt b/Verilog Gen/dataset_self_contain/Prob003_non-overlapping_sequence_detect_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..7596c037f2c03cec53455a4a793c4ca74529b5f4 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob003_non-overlapping_sequence_detect_prompt.txt @@ -0,0 +1,31 @@ +I would like you to implement a module named `TopModule` with the following interface. All input and output ports are one bit unless otherwise specified. + +### Inputs: +- `clk`: System clock signal. +- `rst_n`: Asynchronous reset signal, active low. +- `data`: Single-bit input signal, representing the data to be checked. + +### Outputs: +- `match`: A signal that outputs `1` when the input signal `data` matches the target sequence `011100`. It remains `0` at all other times. +- `not_match`: A signal that outputs `1` when the input signal `data` does not match the target sequence `011100`. It remains `0` at all other times. + +### Module Description: +The module must detect the sequence `011100` from the input signal `data`. Please use the state machine to solve this problem. The detection rules are as follows: +1. The sequence check begins with every sixth bit of input data, meaning the module processes the input in groups of six bits and does not overlap or reuse bits between groups. +2. If the first bit of a group does not match the sequence, the module skips the entire group and starts checking from the next group beginning at the seventh bit. +3. When the sequence `011100` is detected, the `match` output should assert (go high). +4. If the sequence is not detected in a group, the `not_match` output should assert (go high). + +### Output Logic: +The output signals `match` and `not_match` should be implemented as follows: +- Declare both outputs as `reg` type: `output reg match, output reg not_match` +- Use combinational logic with Wildcard Sensitivity List block to control the outputs +- The outputs are mutually exclusive - only one can be high at a time +- While `rst_n` is low, both outputs are 0 + +### Timing and Behavior: +- When `rst_n` is low, the module should reset to its initial state and begin sequence detection from the start. +- On every positive clock edge (`clk`), the module should evaluate the input signal `data` and transition states accordingly. +- The first positive clock edge after power-up only initializes the module and consumes no `data` bit. + +Clarification: match and not_match are combinational (Moore) functions of the current state only, not of the current input bit, except that both are 0 while `rst_n` is low. Each completed 6-bit group produces exactly one pulse aligned to the group's 6th bit: match=1 for a matching group or not_match=1 for a non-matching group, regardless of which bit first diverged. diff --git a/Verilog Gen/dataset_self_contain/Prob003_non-overlapping_sequence_detect_ref.sv b/Verilog Gen/dataset_self_contain/Prob003_non-overlapping_sequence_detect_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..10012372821357962ecd8de990a1bdcfe95a7ef1 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob003_non-overlapping_sequence_detect_ref.sv @@ -0,0 +1,95 @@ +module RefModule( + input clk, + input rst_n, + input data, + output reg match, + output reg not_match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1=4'd1, + s2=4'd2, + s3=4'd3, + s4=4'd4, + s5=4'd5, + s6=4'd6, + sf1=4'd7, + sf2=4'd8, + sf3=4'd9, + sf4=4'd10, + sf5=4'd11, + sf6=4'd12; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data) +begin + case(pstate) + idle: + if(data==0) + nstate=s1; //First bit matched + else + nstate=sf1; + s1: + nstate=data?s2:sf2; + s2: + nstate=data?s3:sf3; + s3: + nstate=data?s4:sf4; + s4: + nstate=data?sf5:s5; + s5: + nstate=data?sf6:s6; + s6: + nstate=data?sf1:s1; + sf1: + nstate=sf2; + sf2: + nstate=sf3; + sf3: + nstate=sf4; + sf4: + nstate=sf5; + sf5: + nstate=sf6; + sf6: + nstate=data?sf1:s1; + default: + nstate=idle; + endcase +end + +always @(pstate or data or rst_n) +begin + if(!rst_n==1) + begin + match=1'b0; + not_match = 1'b0; + end + else if(pstate==s6) + begin + match=1'b1; + not_match = 1'b0; + end + else if(pstate==sf6) + begin + match=1'b0; + not_match = 1'b1; + end + else + begin + match=1'b0; + not_match = 1'b0; + end +end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob003_non-overlapping_sequence_detect_test.sv b/Verilog Gen/dataset_self_contain/Prob003_non-overlapping_sequence_detect_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..a12f598932e4d622c31e6fc5509e36ea4fd58f47 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob003_non-overlapping_sequence_detect_test.sv @@ -0,0 +1,189 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + +module stimulus_gen ( + input wire clk, + output reg rst_n, + output reg data +); + + // additional check: drive one 6-bit group, MSB first, one bit per falling edge + task automatic send_group(input logic [5:0] g); + for (int i = 5; i >= 0; i--) @(negedge clk) data <= g[i]; + endtask + + initial begin + // Initialize signals + rst_n = 1'b0; + data = 1'b0; + + // Apply reset + @(posedge clk); + rst_n = 1'b1; + @(posedge clk) + @(posedge clk) + + // Test sequence: Provide the sequence "011100" + @(posedge clk) data = 1'b0; // First bit + @(posedge clk) data = 1'b1; // Second bit + @(posedge clk) data = 1'b1; // Third bit + @(posedge clk) data = 1'b1; // Fourth bit + @(posedge clk) data = 1'b0; // Fifth bit + @(posedge clk) data = 1'b0; // Sixth bit + + // Additional test cases + repeat(500) @(negedge clk) begin + data <= $random; + end + + // End simulation after sufficient cycles + repeat(100) @(posedge clk); + + // Additional checks, after all original stimulus. Inputs change on the falling edge only. + // (1) mid-run asynchronous reset: "When rst_n is low, the module should reset to its initial state + // and begin sequence detection from the start" and "While rst_n is low, both outputs are 0". + // The first rising edge with rst_n high after a mid-run reset evaluates data (only the first edge + // after power-up consumes no bit), so groups restart at that bit. + // (2) after the restart, whole groups: matching groups back to back, groups that diverge at each bit + // position, and a long run of random groups (half of them the target sequence). + for (int r = 0; r < 4; r++) begin + repeat (3 + r) @(negedge clk) data <= $random; // stop part-way into a group + if (r == 3) begin + // short pulse between two rising edges: asynchronous, so it still resets + @(negedge clk); + #1 rst_n <= 1'b0; + #2 rst_n <= 1'b1; + end else begin + @(negedge clk) rst_n <= 1'b0; // assert between rising edges + repeat (1 + r % 2) @(negedge clk); + rst_n <= 1'b1; // release between rising edges + end + send_group(6'b011100); + send_group(6'b011100); + for (int b = 0; b < 6; b++) send_group(6'b011100 ^ (6'b100000 >> b)); + send_group(6'b011100); + repeat (60) begin + if ($urandom_range(0, 1)) send_group(6'b011100); + else send_group($urandom); + end + end + // (3) reset asserted while match is high, held low to the end: outputs must drop to 0 at once. + send_group(6'b011100); + @(negedge clk); + #1 rst_n <= 1'b0; + repeat (5) @(posedge clk); + #1 $finish; + end + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + int errors_not_match; + int errortime_not_match; + + int clocks; + } stats; + + stats stats1; + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk = 0; + initial forever + #5 clk = ~clk; + + logic rst_n; + logic data; + logic match_ref; + logic match_dut; + logic not_match_ref; + logic not_match_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data, match_ref, match_dut, not_match_ref, not_match_dut); + end + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // Instantiate stimulus generator + stimulus_gen stim1 ( + .clk(clk), + .rst_n(rst_n), + .data(data) + ); + + // Instantiate reference module + RefModule good1 ( + .clk(clk), + .rst_n(rst_n), + .data(data), + .match(match_ref), + .not_match(not_match_ref) + ); + + // Instantiate DUT (TopModule) + TopModule top_module1 ( + .clk(clk), + .rst_n(rst_n), + .data(data), + .match(match_dut), + .not_match(not_match_dut) + ); + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Delay until the end of the time step + @(strobe); + end + endtask + + final begin + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + if (stats1.errors_not_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "not_match", stats1.errors_not_match, stats1.errortime_not_match); + else $display("Hint: Output '%s' has no mismatches.", "not_match"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: Ensure outputs match the reference module + assign tb_match = ( { match_ref, not_match_ref } === ( { match_ref, not_match_ref } ^ { match_dut, not_match_dut } ^ { match_ref, not_match_ref } ) ); + + always @(posedge clk, negedge clk) begin + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (match_ref !== ( match_ref ^ match_dut ^ match_ref )) begin + if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match++; + end + if (not_match_ref !== ( not_match_ref ^ not_match_dut ^ not_match_ref )) begin + if (stats1.errors_not_match == 0) stats1.errortime_not_match = $time; + stats1.errors_not_match++; + end + end + + // Add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob004_continuous_input_sequence_detect2_prompt.txt b/Verilog Gen/dataset_self_contain/Prob004_continuous_input_sequence_detect2_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..db0ddeaf6f70f4d7bb43d4cfdf0e3caeb03d4ea1 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob004_continuous_input_sequence_detect2_prompt.txt @@ -0,0 +1,30 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input data + - input data_valid + - output match + +The module should implement a sequence detection circuit that detects the +sequence "0110" in the input data stream. The data_valid signal indicates +when the input data is valid and should be considered for sequence detection. +When data_valid is high, the current input data is valid and participates +in sequence detection. When data_valid is low, the current input data is +invalid and should be ignored (not considered for sequence detection). + +The sequence detection should work as follows: +- Use state machine and update it sequentially +- Monitor the input data stream when data_valid is high +- Detect when the sequence "0110" appears in the valid data +- Output match signal should be high when the sequence "0110" is detected +- Output match signal should be low at all other times + +Output Logic for match signal: +- When reset is active (rst_n = 0): match = 0 +- When reset is inactive (rst_n = 1) AND sequence is 0110: match = 1 +- When reset is inactive (rst_n = 1) AND sequence is NOT 0110: match = 0 + +Clarification: Use a Moore output. Provide a dedicated accept state for the sequence 0110 and assert match combinationally from the current state only, so match goes high the clock cycle after the final valid 0 has been registered (not in the same cycle the bit is presented). match is high for one clock cycle per detection; if data_valid is low in that cycle, detection restarts from scratch. Detections do not overlap. diff --git a/Verilog Gen/dataset_self_contain/Prob004_continuous_input_sequence_detect2_ref.sv b/Verilog Gen/dataset_self_contain/Prob004_continuous_input_sequence_detect2_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..fd4d4fac324f9ea18f5516aa4e469fdf53c8a931 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob004_continuous_input_sequence_detect2_ref.sv @@ -0,0 +1,78 @@ +`timescale 1ns/1ns +module RefModule( + input clk, + input rst_n, + input data, + input data_valid, + output reg match + ); + + +reg [3:0] pstate,nstate; + +parameter idle=4'd0, + s1_d0=4'd1, + s2_d01=4'd2, + s3_d011=4'd3, + s4_d0110=4'd4; + +always @(posedge clk or negedge rst_n) +begin + if(!rst_n) + pstate<=idle; + else + pstate<=nstate; +end + +always @(pstate or data or data_valid) +begin + case(pstate) + idle: + if(data_valid && !data) + nstate=s1_d0; //First bit matched + else + nstate=idle; + s1_d0: + if (data_valid) + begin + if (data) nstate = s2_d01; //Data valid and is 1, first two bits 01 matched, next state is s2_d01 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s1_d0; //Data invalid, stay in s1_d0 + s2_d01: + if (data_valid) + begin + if (data) nstate = s3_d011; //Data valid and is 1, first three bits 011 matched, next state is s3_d011 + else nstate = s1_d0; //Data valid but is 0, only first bit 0 matched, next state is s1_d0 + end + else nstate = s2_d01; //Data invalid, stay in s2_d01 + s3_d011: + if (data_valid) + begin + if (!data) nstate = s4_d0110; //Data valid and is 0, all four bits 0110 matched, next state is s4_d0110 + else nstate = idle; //Data valid but is 1, no match, next state is idle + end + else nstate = s3_d011; //Data invalid, stay in s3_d011 + s4_d0110: + if (data_valid) + begin + if (!data) nstate = s1_d0; //Data valid and is 0, matches first bit 0 of target sequence, next state is s1_d0 + else nstate = idle; //Data valid but is 1, does not match target sequence, next state is idle + end + else nstate = idle; //Data invalid, next state is idle + default: + nstate=idle; + endcase +end + +always @(pstate or rst_n) +begin + if(!rst_n==1) + match=1'b0; + else if(pstate==s4_d0110) //Entering state s4_d0110 means all four bits matched, assert match signal high + match=1'b1; + else + match=1'b0; +end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob004_continuous_input_sequence_detect2_test.sv b/Verilog Gen/dataset_self_contain/Prob004_continuous_input_sequence_detect2_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..82239fb98ff463683ce3e113ae82b524feb863f9 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob004_continuous_input_sequence_detect2_test.sv @@ -0,0 +1,163 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + output logic rst_n, + output logic data, + output logic data_valid +); + + initial begin + rst_n = 0; + data = 0; + data_valid = 0; + @(posedge clk); + @(posedge clk); + rst_n = 1; + @(posedge clk); + @(posedge clk); + + // Test specific sequence: 0110 + data_valid = 1; + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + data_valid = 0; + @(posedge clk); + @(posedge clk); + + // Test with data_valid low (should ignore data) + data_valid = 0; + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + @(posedge clk); + + // Test overlapping sequences + data_valid = 1; + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + data = 1; @(posedge clk); + data = 1; @(posedge clk); + data = 0; @(posedge clk); + @(posedge clk); + + // Random testing + repeat(500) @(negedge clk) begin + data_valid <= $random; + data <= $random; + end + + #1 $finish; + end + +endmodule + +module tb(); + + typedef struct packed { + int errors; + int errortime; + int errors_match; + int errortime_match; + + int clocks; + } stats; + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + logic rst_n; + logic data; + logic data_valid; + logic match_ref; + logic match_dut; + + initial begin + $dumpfile("wave.vcd"); + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data, data_valid, match_ref, match_dut ); + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + stimulus_gen stim1 ( + .clk, + .* , + .rst_n, + .data, + .data_valid ); + RefModule good1 ( + .clk, + .rst_n, + .data, + .data_valid, + .match(match_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .data, + .data_valid, + .match(match_dut) ); + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + if (stats1.errors_match) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "match", stats1.errors_match, stats1.errortime_match); + else $display("Hint: Output '%s' has no mismatches.", "match"); + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { match_ref } === ( { match_ref } ^ { match_dut } ^ { match_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + if (match_ref !== ( match_ref ^ match_dut ^ match_ref )) + begin if (stats1.errors_match == 0) stats1.errortime_match = $time; + stats1.errors_match = stats1.errors_match+1'b1; end + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob005_signal_generator_prompt.txt b/Verilog Gen/dataset_self_contain/Prob005_signal_generator_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..d93bd3d51a665823623da596e576ffed95a6b864 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob005_signal_generator_prompt.txt @@ -0,0 +1,35 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input wave_choice (2 bits) + - output wave (5 bits) + +The module should implement a signal generator that outputs different +waveforms based on the wave_choice input signal. The generator should +produce three different types of waveforms: + +When wave_choice = 0: Generate a square wave signal +When wave_choice = 1: Generate a sawtooth wave signal +When wave_choice = 2: Generate a triangular wave signal + +The waveform generation should work as follows: +- The wave output should be a 5-bit signal that cycles through different + values to create the desired waveform patterns. wave takes integer values + 0 to 20 (decimal); it never uses the full 5-bit 0-31 range. +- rst_n is an active-low asynchronous reset that sets wave to 0. +- Changing wave_choice does not restart the waveform: the newly selected + waveform continues from the current value of wave. +- Each waveform should complete one full cycle and then repeat +- The square wave should alternate between high and low values: square = 20 + for 10 clocks, then 0 for 10 clocks (period 20 cycles, 50% duty). When + selected, including at reset release, wave first keeps its current value + for 10 clocks, then is 20 for 10 clocks, 0 for 10 clocks, and so on. +- The sawtooth wave should increment from 0 to maximum value (20) and then + reset: sawtooth counts 0->1->...->20, then wraps to 0. Each clock adds 1 + to the current value of wave, so the first clock after reset shows 1. +- The triangular wave should increment from 0 to maximum (20), then decrement + back to 0: triangle ramps 0->20 then 20->0, one step per clock. When + selected, it first counts down from the current value of wave to 0. diff --git a/Verilog Gen/dataset_self_contain/Prob005_signal_generator_ref.sv b/Verilog Gen/dataset_self_contain/Prob005_signal_generator_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..44347d220d9c2edfbbd24566a4245c5a61b6c379 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob005_signal_generator_ref.sv @@ -0,0 +1,48 @@ + +module RefModule( + input clk, + input rst_n, + input [1:0] wave_choice, + output reg [4:0]wave + ); + + reg [4:0] cnt; + reg flag; + + // Counter control in square wave mode + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= wave_choice!=0 ? 0: + cnt ==19? 0: + cnt + 1; + end + + // Flag control in triangle wave mode + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + flag <= 0; + else + flag <= wave_choice!=2 ? 0: + wave ==1 ? 1: + wave ==19? 0: + flag; + end + + + // Update wave signal + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + wave <= 0; + else + case(wave_choice) + 0 : wave <= cnt == 9? 20 : + cnt ==19? 0 : + wave; + 1 : wave <= wave==20? 0 : wave+1; + 2 : wave <= flag==0 ? wave-1: wave+1; + default: wave <= 0; + endcase + end +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob005_signal_generator_test.sv b/Verilog Gen/dataset_self_contain/Prob005_signal_generator_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..bd10db973c22a6fd3ee41da31fe465c604253b2b --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob005_signal_generator_test.sv @@ -0,0 +1,146 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [1:0] wave_choice + // === End your code here === +); + + + initial begin + rst_n = 0; + wave_choice = 0; + repeat(20) @(posedge clk); + rst_n = 1; + @(posedge clk); + // Test square wave (wave_choice = 0) + wave_choice = 0; + repeat(800) @(posedge clk); + + // Test sawtooth wave (wave_choice = 1) + wave_choice = 1; + repeat(800) @(posedge clk); + + // Test triangular wave (wave_choice = 2) + wave_choice = 2; + repeat(800) @(posedge clk); + + wave_choice = 0; + repeat(800) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_wave; + int errortime_wave; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [1:0] wave_choice; + logic [4:0] wave_ref; + logic [4:0] wave_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, wave_choice, wave_ref, wave_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .wave_choice ); + + RefModule good1 ( + .clk, + .rst_n, + .wave_choice(wave_choice), + .wave(wave_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .wave_choice, + .wave(wave_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_wave) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "wave", stats1.errors_wave, stats1.errortime_wave); + else $display("Hint: Output '%s' has no mismatches.", "wave"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { wave_ref } === ( { wave_ref } ^ { wave_dut } ^ { wave_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (wave_ref !== ( wave_ref ^ wave_dut ^ wave_ref )) + begin if (stats1.errors_wave == 0) stats1.errortime_wave = $time; + stats1.errors_wave = stats1.errors_wave+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob006_data_serial-to-parallel_circuit_prompt.txt b/Verilog Gen/dataset_self_contain/Prob006_data_serial-to-parallel_circuit_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..7eac6f729b10d97595e18893a0202a712d5d82de --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob006_data_serial-to-parallel_circuit_prompt.txt @@ -0,0 +1,39 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_a + - input data_a + - output ready_a + - output valid_b + - output data_b (6 bits) + +rst_n is an active-low asynchronous reset: while it is low, all outputs are +0 and any partially received word is discarded. + +The module should implement a serial-to-parallel conversion circuit that +converts single-bit input data into 6-bit parallel output data. The circuit +uses different handshake protocols for input and output interfaces. + +The serial-to-parallel conversion should work as follows: +- Input interface: Uses valid-ready handshake mechanism + - valid_a indicates the validity of input data_a + - ready_a indicates the module is ready to receive upstream data +- Output interface: Uses valid-only handshake mechanism + - valid_b indicates the validity of output data_b + - data_b is the 6-bit parallel output data + +Data conversion process: +- The module receives single-bit input data_a when valid_a is high +- After receiving 6 input data bits, the module outputs the concatenated + 6-bit data_b with valid_b asserted; data_b changes only at the clock edge + that receives the sixth bit +- valid_b is high in each cycle after one in which five bits of the current + word had been received, even while the sixth bit is awaited +- The first received data bit should be placed in the LSB (bit 0) of data_b +- The last received data bit should be placed in the MSB (bit 5) of data_b +- ready_a is 0 during reset, rises at the first clock edge after reset is + released and then stays high; bits presented while ready_a is 0 are not + received diff --git a/Verilog Gen/dataset_self_contain/Prob006_data_serial-to-parallel_circuit_ref.sv b/Verilog Gen/dataset_self_contain/Prob006_data_serial-to-parallel_circuit_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..280616e82b10504600dea0c2e26db481db5b0642 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob006_data_serial-to-parallel_circuit_ref.sv @@ -0,0 +1,50 @@ +module RefModule( + input clk , + input rst_n , + input valid_a , + input data_a , + + output reg ready_a , + output reg valid_b , + output reg [5:0] data_b +); + reg [5:0] data_r; + reg [2:0] cnt; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~ready_a||~valid_a? cnt: + cnt == 5? 0 : + cnt+1; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_r <= 6'b0; + else + data_r <= ready_a&&valid_a? {data_a, data_r[5:1]}: data_r; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_b <= 6'b0; + else + data_b <= cnt==5&&valid_a? {data_a, data_r[5:1]}: data_b; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_b <= 0; + else + valid_b <= cnt==5; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + ready_a <= 0; + else + ready_a <= 1; + end +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob006_data_serial-to-parallel_circuit_test.sv b/Verilog Gen/dataset_self_contain/Prob006_data_serial-to-parallel_circuit_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..9bc0f7def68c7b80860c7728741aa7160d8dc7b1 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob006_data_serial-to-parallel_circuit_test.sv @@ -0,0 +1,202 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_a, + output logic data_a + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_a = 0; + data_a = 0; + repeat(4)@(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test serial data input: 6 bits per group + // First group: 101010 + valid_a <= 1; + data_a <= 1; @(posedge clk); + data_a <= 0; @(posedge clk); + data_a <= 1; @(posedge clk); + data_a <= 0; @(posedge clk); + data_a <= 1; @(posedge clk); + data_a <= 0; @(posedge clk); + + // Second group: 110011 + data_a <= 1; @(posedge clk); + data_a <= 1; @(posedge clk); + data_a <= 0; @(posedge clk); + data_a <= 0; @(posedge clk); + data_a <= 1; @(posedge clk); + data_a <= 1; @(posedge clk); + + // Test with valid_a low (should ignore data) + valid_a <= 0; + data_a <= 1; @(posedge clk); + data_a <= 0; @(posedge clk); + data_a <= 1; @(posedge clk); + + // Third group: 111000 + valid_a <= 1; + data_a <= 1; @(posedge clk); + data_a <= 1; @(posedge clk); + data_a <= 1; @(posedge clk); + data_a <= 0; @(posedge clk); + data_a <= 0; @(posedge clk); + data_a <= 0; @(posedge clk); + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + valid_a <= $random; + data_a <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_ready_a; + int errortime_ready_a; + int errors_valid_b; + int errortime_valid_b; + int errors_data_b; + int errortime_data_b; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_a; + logic data_a; + logic ready_a_ref; + logic ready_a_dut; + logic valid_b_ref; + logic valid_b_dut; + logic [5:0] data_b_ref; + logic [5:0] data_b_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_a, data_a, ready_a_ref, ready_a_dut, valid_b_ref, valid_b_dut, data_b_ref, data_b_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_a, + .data_a ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_a, + .data_a, + .ready_a(ready_a_ref), + .valid_b(valid_b_ref), + .data_b(data_b_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_a, + .data_a, + .ready_a(ready_a_dut), + .valid_b(valid_b_dut), + .data_b(data_b_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_ready_a) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "ready_a", stats1.errors_ready_a, stats1.errortime_ready_a); + else $display("Hint: Output '%s' has no mismatches.", "ready_a"); + if (stats1.errors_valid_b) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_b", stats1.errors_valid_b, stats1.errortime_valid_b); + else $display("Hint: Output '%s' has no mismatches.", "valid_b"); + if (stats1.errors_data_b) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_b", stats1.errors_data_b, stats1.errortime_data_b); + else $display("Hint: Output '%s' has no mismatches.", "data_b"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { ready_a_ref, valid_b_ref, data_b_ref } === ( { ready_a_ref, valid_b_ref, data_b_ref } ^ { ready_a_dut, valid_b_dut, data_b_dut } ^ { ready_a_ref, valid_b_ref, data_b_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (ready_a_ref !== ( ready_a_ref ^ ready_a_dut ^ ready_a_ref )) + begin if (stats1.errors_ready_a == 0) stats1.errortime_ready_a = $time; + stats1.errors_ready_a = stats1.errors_ready_a+1'b1; end + if (valid_b_ref !== ( valid_b_ref ^ valid_b_dut ^ valid_b_ref )) + begin if (stats1.errors_valid_b == 0) stats1.errortime_valid_b = $time; + stats1.errors_valid_b = stats1.errors_valid_b+1'b1; end + if (data_b_ref !== ( data_b_ref ^ data_b_dut ^ data_b_ref )) + begin if (stats1.errors_data_b == 0) stats1.errortime_data_b = $time; + stats1.errors_data_b = stats1.errors_data_b+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob007_data_accumulation_output_prompt.txt b/Verilog Gen/dataset_self_contain/Prob007_data_accumulation_output_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..f7a880e13ffbd06151dd62d7676465ad8550aa12 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob007_data_accumulation_output_prompt.txt @@ -0,0 +1,37 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input data_in (8 bits) + - input valid_a + - input ready_b + - output ready_a + - output valid_b + - output data_out (10 bits) + +The module should implement a serial data accumulation circuit that receives +8-bit input data and outputs the accumulated sum of every 4 input values. +The circuit uses valid-ready handshake mechanisms for both input and output +interfaces to ensure efficient data transfer without performance loss. + +The data accumulation should work as follows: +- Input interface: Uses valid-ready handshake mechanism + - valid_a indicates the validity of input data_in + - ready_a indicates the module is ready to receive upstream data; it is low only + while valid_b is high and ready_b is low +- Output interface: Uses valid-ready handshake mechanism + - valid_b indicates the validity of output data_out + - ready_b indicates downstream is ready to receive data + +Data accumulation process: +- The module receives 8-bit input data_in when valid_a is high and ready_a is high +- After receiving 4 input data values, the module outputs the accumulated sum + as 10-bit data_out with valid_b asserted +- data_out shows the running sum, updated the cycle after each received input. The first input + of a group restarts the sum only if ready_b is high when it is received; otherwise it is added + to the previous output sum +- Both input and output interfaces must support backpressure through ready signals +- The design must ensure no performance loss when both upstream and downstream + are ready for full-speed transmission (no bubbles in data flow) diff --git a/Verilog Gen/dataset_self_contain/Prob007_data_accumulation_output_ref.sv b/Verilog Gen/dataset_self_contain/Prob007_data_accumulation_output_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..64f76b80741305805eb36463a9cd86dcf227cc64 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob007_data_accumulation_output_ref.sv @@ -0,0 +1,44 @@ +`timescale 1ns/1ns + +module RefModule( + input clk , + input rst_n , + input [7:0] data_in , + input valid_a , + input ready_b , + + output ready_a , + output reg valid_b , + output reg [9:0] data_out +); +reg [1:0] data_cnt; + +assign ready_a = !valid_b | ready_b; + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_cnt <= 'd0; + else if(valid_a && ready_a) + data_cnt <= (data_cnt == 2'd3) ? 'd0 : (data_cnt + 1'd1); +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_b <= 'd0; + else if(data_cnt == 2'd3 && valid_a && ready_a) + valid_b <= 1'd1; + else if(valid_b && ready_b) + valid_b <= 1'd0; +end + +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(ready_b && valid_a && ready_a && (data_cnt == 2'd0)) + data_out <= data_in; + else if(valid_a && ready_a) + data_out <= data_out + data_in; + +end + + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob007_data_accumulation_output_test.sv b/Verilog Gen/dataset_self_contain/Prob007_data_accumulation_output_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..99cd1ac83490a6b3f47e9a6f26e024d4a192ddd4 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob007_data_accumulation_output_test.sv @@ -0,0 +1,209 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [7:0] data_in, + output logic valid_a, + output logic ready_b + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + data_in = 0; + valid_a = 0; + ready_b = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test first group of 4 values: 1, 2, 3, 4 (sum = 10) + valid_a <= 1; + data_in <= 8'd1; @(posedge clk); + data_in <= 8'd2; @(posedge clk); + data_in <= 8'd3; @(posedge clk); + data_in <= 8'd4; @(posedge clk); + + data_in <= 8'd5; @(posedge clk); + @(posedge clk); + @(posedge clk); + ready_b <= 1; + + data_in <= 8'd6; @(posedge clk); + data_in <= 8'd7; @(posedge clk); + data_in <= 8'd8; @(posedge clk); + + // Test with valid_a low (should ignore data) + valid_a <= 0; + data_in <= 8'd10; @(posedge clk); + data_in <= 8'd20; @(posedge clk); + + // Test with ready_b low (backpressure) + valid_a <= 1; + ready_b <= 0; + data_in <= 8'd1; @(posedge clk); + data_in <= 8'd2; @(posedge clk); + + // Test third group: 10, 20, 30, 40 (sum = 100) + ready_b <= 1; + data_in <= 8'd10; @(posedge clk); + data_in <= 8'd20; @(posedge clk); + data_in <= 8'd30; @(posedge clk); + data_in <= 8'd40; @(posedge clk); + + // Random testing + repeat(300) @(posedge clk or negedge clk) begin + valid_a <= $random; + ready_b <= $random; + data_in <= $random % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_ready_a; + int errortime_ready_a; + int errors_valid_b; + int errortime_valid_b; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [7:0] data_in; + logic valid_a; + logic ready_b; + logic ready_a_ref; + logic ready_a_dut; + logic valid_b_ref; + logic valid_b_dut; + logic [9:0] data_out_ref; + logic [9:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data_in, valid_a, ready_b, ready_a_ref, ready_a_dut, valid_b_ref, valid_b_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .data_in, + .valid_a, + .ready_b ); + + RefModule good1 ( + .clk, + .rst_n, + .data_in, + .valid_a, + .ready_b, + .ready_a(ready_a_ref), + .valid_b(valid_b_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .data_in, + .valid_a, + .ready_b, + .ready_a(ready_a_dut), + .valid_b(valid_b_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_ready_a) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "ready_a", stats1.errors_ready_a, stats1.errortime_ready_a); + else $display("Hint: Output '%s' has no mismatches.", "ready_a"); + if (stats1.errors_valid_b) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_b", stats1.errors_valid_b, stats1.errortime_valid_b); + else $display("Hint: Output '%s' has no mismatches.", "valid_b"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { ready_a_ref, valid_b_ref, data_out_ref } === ( { ready_a_ref, valid_b_ref, data_out_ref } ^ { ready_a_dut, valid_b_dut, data_out_dut } ^ { ready_a_ref, valid_b_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (ready_a_ref !== ( ready_a_ref ^ ready_a_dut ^ ready_a_ref )) + begin if (stats1.errors_ready_a == 0) stats1.errortime_ready_a = $time; + stats1.errors_ready_a = stats1.errors_ready_a+1'b1; end + if (valid_b_ref !== ( valid_b_ref ^ valid_b_dut ^ valid_b_ref )) + begin if (stats1.errors_valid_b == 0) stats1.errortime_valid_b = $time; + stats1.errors_valid_b = stats1.errors_valid_b+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob008_non-integer_data_width_conversion_24to128_prompt.txt b/Verilog Gen/dataset_self_contain/Prob008_non-integer_data_width_conversion_24to128_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..1c6f0df4958d00578d30d24e949a0e6dc949aa8a --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob008_non-integer_data_width_conversion_24to128_prompt.txt @@ -0,0 +1,34 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_in + - input data_in (24 bits) + - output reg valid_out + - output reg data_out (128 bits) + +The module should implement a data width conversion circuit that converts +24-bit input data into 128-bit output data. The circuit uses valid-only +handshake mechanism for both input and output interfaces. + +The data width conversion should work as follows: +- Input interface: Uses valid-only handshake mechanism + - valid_in indicates the validity of input data_in +- Output interface: Uses valid-only handshake mechanism + - valid_out indicates the validity of output data_out + +Data conversion process: +- The module receives 24-bit input data_in when valid_in is high +- The module accumulates multiple 24-bit inputs to form a 128-bit output +- The first received 24-bit data should be placed in the high bits of data_out +- The last received 24-bit data should be placed in the low bits of data_out +- After accumulating enough data to form a complete 128-bit word, the module + outputs data_out with valid_out asserted +- valid_out and data_out are registered: the word appears the cycle after the edge that samples + the 6th/11th/16th input; valid_out is high for that one cycle and data_out holds the last word +- rst_n is an asynchronous active-low reset that clears valid_out, data_out, the input count and + the accumulated bits + +Clarification: Maintain a running shift register and shift in each full 24-bit input on every valid input. Assert valid_out on the 6th, 11th, and 16th valid inputs (input-counter values 5, 10, 15, counting modulo 16). At those points form the 128-bit output by concatenating the accumulated bits with, respectively, data_in[23:16], data_in[23:8], and data_in[23:0], so that the 128-bit boundaries cut through the 24-bit words. diff --git a/Verilog Gen/dataset_self_contain/Prob008_non-integer_data_width_conversion_24to128_ref.sv b/Verilog Gen/dataset_self_contain/Prob008_non-integer_data_width_conversion_24to128_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..86db2f4a5ed196af303085b2266739c89c840a47 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob008_non-integer_data_width_conversion_24to128_ref.sv @@ -0,0 +1,48 @@ +`timescale 1ns/1ns + +module RefModule( + input clk , + input rst_n , + input valid_in , + input [23:0] data_in , + + output reg valid_out , + output reg [127:0] data_out +); + reg [3:0] cnt; + reg [127:0] data_lock; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + cnt <= 0; + else + cnt <= ~valid_in? cnt:cnt+1; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_out <= 0; + else + valid_out <= (cnt==5 || cnt==10 || cnt==15)&&valid_in; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_lock <= 0; + else + data_lock <= valid_in? {data_lock[103:0], data_in}: data_lock; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else if(cnt==5) + data_out <= valid_in? {data_lock[119:0], data_in[23:16]}: data_out; + else if(cnt==10) + data_out <= valid_in? {data_lock[111:0], data_in[23: 8]}: data_out; + else if(cnt==15) + data_out <= valid_in? {data_lock[103:0], data_in[23: 0]}: data_out; + else + data_out <= data_out; + end +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob008_non-integer_data_width_conversion_24to128_test.sv b/Verilog Gen/dataset_self_contain/Prob008_non-integer_data_width_conversion_24to128_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..7256496acfb63697009cadc94c0c275adccae24e --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob008_non-integer_data_width_conversion_24to128_test.sv @@ -0,0 +1,190 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_in, + output logic [23:0] data_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_in = 0; + data_in = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test first group: 6 x 24-bit inputs to form 128-bit output + // Inputs: 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666 + valid_in <= 1; + data_in <= 24'h111111; @(posedge clk); + data_in <= 24'h222222; @(posedge clk); + data_in <= 24'h333333; @(posedge clk); + data_in <= 24'h444444; @(posedge clk); + data_in <= 24'h555555; @(posedge clk); + data_in <= 24'h666666; @(posedge clk); + + // Test second group: 6 x 24-bit inputs + data_in <= 24'h777777; @(posedge clk); + data_in <= 24'h888888; @(posedge clk); + data_in <= 24'h999999; @(posedge clk); + data_in <= 24'haaaaaa; @(posedge clk); + data_in <= 24'hbbbbbb; @(posedge clk); + data_in <= 24'hcccccc; @(posedge clk); + + // Test with valid_in low (should ignore data) + valid_in <= 0; + data_in <= 24'hdddddd; @(posedge clk); + data_in <= 24'heeeeee; @(posedge clk); + + // Test third group: 6 x 24-bit inputs + valid_in <= 1; + data_in <= 24'hffffff; @(posedge clk); + data_in <= 24'h000000; @(posedge clk); + data_in <= 24'h123456; @(posedge clk); + data_in <= 24'h789abc; @(posedge clk); + data_in <= 24'hdef012; @(posedge clk); + data_in <= 24'h345678; @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + valid_in <= $random; + data_in <= $random % (1<<24); + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_valid_out; + int errortime_valid_out; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_in; + logic [23:0] data_in; + logic valid_out_ref; + logic valid_out_dut; + logic [127:0] data_out_ref; + logic [127:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_in, data_in, valid_out_ref, valid_out_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_in, + .data_in ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_valid_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_out", stats1.errors_valid_out, stats1.errortime_valid_out); + else $display("Hint: Output '%s' has no mismatches.", "valid_out"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { valid_out_ref, data_out_ref } === ( { valid_out_ref, data_out_ref } ^ { valid_out_dut, data_out_dut } ^ { valid_out_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (valid_out_ref !== ( valid_out_ref ^ valid_out_dut ^ valid_out_ref )) + begin if (stats1.errors_valid_out == 0) stats1.errortime_valid_out = $time; + stats1.errors_valid_out = stats1.errors_valid_out+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob009_non-integer_data_width_conversion_8to12_prompt.txt b/Verilog Gen/dataset_self_contain/Prob009_non-integer_data_width_conversion_8to12_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..06289f54903177ad7e1bfca99fcd9c41c0208ba0 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob009_non-integer_data_width_conversion_8to12_prompt.txt @@ -0,0 +1,34 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_in + - input data_in (8 bits) + - output valid_out + - output data_out (12 bits) + +The module should implement a data width conversion circuit that converts +8-bit input data into 12-bit output data. The circuit uses valid-only +handshake mechanism for both input and output interfaces. + +The data width conversion should work as follows: +- Input interface: Uses valid-only handshake mechanism + - valid_in indicates the validity of input data_in +- Output interface: Uses valid-only handshake mechanism + - valid_out indicates the validity of output data_out + +Data conversion process: +- The module receives 8-bit input data_in when valid_in is high +- The module accumulates two 8-bit inputs to form a 12-bit output +- The first received 8-bit data should be placed in the high bits of data_out +- The second received 8-bit data should be placed in the low bits of data_out +- After receiving exactly 2 input data values, the module outputs data_out + with valid_out asserted in the next clock cycle +- If only one data input is received, the module waits for the second input + before producing any output + +Clarification: Process inputs in groups of three 8-bit inputs, producing two 12-bit outputs per group (no bits are dropped). On the 2nd input of each group, output {first_byte, second_byte[7:4]} with valid_out high; on the 3rd input, output {second_byte[3:0], third_byte} with valid_out high. + +rst_n is an active-low asynchronous reset: while it is low, valid_out and data_out are 0 and inputs are ignored; the first input after release starts a new group. valid_out is high for one cycle per output; data_out holds its value between outputs. diff --git a/Verilog Gen/dataset_self_contain/Prob009_non-integer_data_width_conversion_8to12_ref.sv b/Verilog Gen/dataset_self_contain/Prob009_non-integer_data_width_conversion_8to12_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..3ff74355992330de2182dcfcc5d66f51856b39be --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob009_non-integer_data_width_conversion_8to12_ref.sv @@ -0,0 +1,48 @@ +`timescale 1ns/1ns + +module RefModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [11:0] data_out +); +reg [7:0] data_lock; +reg [1:0] valid_cnt ; +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_lock <= 'd0; + else if(valid_in ) + data_lock <= data_in; +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_cnt <= 'd0; + else if(valid_in)begin + if(valid_cnt == 2'd2) + valid_cnt <= 2'd0; + else + valid_cnt <= valid_cnt + 1'd1; + end +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + valid_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + valid_out <= 1'd1; + else if(valid_in && valid_cnt == 2'd2) + valid_out <= 1'd1; + else + valid_out <= 'd0; +end +always @(posedge clk or negedge rst_n ) begin + if(!rst_n) + data_out <= 'd0; + else if(valid_in && valid_cnt == 2'd1) + data_out <= {data_lock, data_in[7:4]}; + else if(valid_in && valid_cnt == 2'd2) + data_out <= {data_lock[3:0], data_in}; +end +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob009_non-integer_data_width_conversion_8to12_test.sv b/Verilog Gen/dataset_self_contain/Prob009_non-integer_data_width_conversion_8to12_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..eb7feeb6a870af7d0f6ae7f10cf400cf0f05562d --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob009_non-integer_data_width_conversion_8to12_test.sv @@ -0,0 +1,181 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_in, + output logic [7:0] data_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_in = 0; + data_in = 0; + repeat(3) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test first pair: 0xAA, 0x55 (should form 0xAA5) + valid_in = 1; + data_in = 8'hAA; @(posedge clk); + data_in = 8'h55; @(posedge clk); + + // Test second pair: 0x12, 0x34 (should form 0x123) + data_in = 8'h12; @(posedge clk); + data_in = 8'h34; @(posedge clk); + + // Test with valid_in low (should ignore data) + valid_in = 0; + data_in = 8'hFF; @(posedge clk); + data_in = 8'hEE; @(posedge clk); + + // Test third pair: 0x78, 0x9A (should form 0x789) + valid_in = 1; + data_in = 8'h78; @(posedge clk); + data_in = 8'h9A; @(posedge clk); + + // Test fourth pair: 0xBC, 0xDE (should form 0xBCD) + data_in = 8'hBC; @(posedge clk); + data_in = 8'hDE; @(posedge clk); + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + valid_in <= $random; + data_in <= $random % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_valid_out; + int errortime_valid_out; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_in; + logic [7:0] data_in; + logic valid_out_ref; + logic valid_out_dut; + logic [11:0] data_out_ref; + logic [11:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_in, data_in, valid_out_ref, valid_out_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_in, + .data_in ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_valid_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_out", stats1.errors_valid_out, stats1.errortime_valid_out); + else $display("Hint: Output '%s' has no mismatches.", "valid_out"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { valid_out_ref, data_out_ref } === ( { valid_out_ref, data_out_ref } ^ { valid_out_dut, data_out_dut } ^ { valid_out_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (valid_out_ref !== ( valid_out_ref ^ valid_out_dut ^ valid_out_ref )) + begin if (stats1.errors_valid_out == 0) stats1.errortime_valid_out = $time; + stats1.errors_valid_out = stats1.errors_valid_out+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob010_integer_multiple_data_bit_width_conversion_8to16_prompt.txt b/Verilog Gen/dataset_self_contain/Prob010_integer_multiple_data_bit_width_conversion_8to16_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..4108f61c07037ba81f865062ee745adebadcc4b0 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob010_integer_multiple_data_bit_width_conversion_8to16_prompt.txt @@ -0,0 +1,37 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input valid_in + - input data_in (8 bits) + - output reg valid_out + - output reg data_out (16 bits) + +The module should implement a data width conversion circuit that converts +8-bit input data into 16-bit output data. The circuit uses valid-only +handshake mechanism for both input and output interfaces. + +The data width conversion should work as follows: +- Input interface: Uses valid-only handshake mechanism + - valid_in indicates the validity of input data_in +- Output interface: Uses valid-only handshake mechanism + - valid_out indicates the validity of output data_out + +Data conversion process: +- The module receives 8-bit input data_in when valid_in is high +- The module accumulates two 8-bit inputs to form a 16-bit output +- The first received 8-bit data should be placed in the high 8 bits of data_out +- The second received 8-bit data should be placed in the low 8 bits of data_out +- After receiving exactly 2 input data values, the module outputs data_out + with valid_out asserted in the next clock cycle +- If only one data input is received, the module waits for the second input + before producing any output +- data_out changes only when a pair is output; otherwise it holds its last + value (0 after reset) + +Clarification: valid_out and data_out are asserted on the clock edge immediately following receipt of the second 8-bit input (exactly one register stage / one cycle of latency), with no additional buffering or pipeline stage. + +Reset: rst_n is an active-low asynchronous reset; while it is low, valid_out and +data_out are 0 and a pending first byte is discarded. diff --git a/Verilog Gen/dataset_self_contain/Prob010_integer_multiple_data_bit_width_conversion_8to16_ref.sv b/Verilog Gen/dataset_self_contain/Prob010_integer_multiple_data_bit_width_conversion_8to16_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..871ba50d8fffed3bb0f14adbfb85fad0277d59a1 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob010_integer_multiple_data_bit_width_conversion_8to16_ref.sv @@ -0,0 +1,40 @@ +module RefModule( + input clk , + input rst_n , + input valid_in , + input [7:0] data_in , + + output reg valid_out, + output reg [15:0] data_out +); + reg [7:0] data_r; + reg flag; + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + flag <= 0; + else + flag <= valid_in? ~flag: flag; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_r <= 0; + else + data_r <= valid_in? data_in: data_r; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + data_out <= 0; + else + data_out <= flag&&valid_in? {data_r, data_in}: data_out; + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + valid_out <= 0; + else + valid_out <= flag&&valid_in; + end +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob010_integer_multiple_data_bit_width_conversion_8to16_test.sv b/Verilog Gen/dataset_self_contain/Prob010_integer_multiple_data_bit_width_conversion_8to16_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..799c6e6a4757f8af250d91797e0be99a8d88a037 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob010_integer_multiple_data_bit_width_conversion_8to16_test.sv @@ -0,0 +1,181 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic valid_in, + output logic [7:0] data_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + valid_in = 0; + data_in = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test first pair: 0xAA, 0x55 (should form 0xAA55) + valid_in = 1; + data_in = 8'hAA; @(posedge clk); + data_in = 8'h55; @(posedge clk); + + // Test second pair: 0x12, 0x34 (should form 0x1234) + data_in = 8'h12; @(posedge clk); + data_in = 8'h34; @(posedge clk); + + // Test with valid_in low (should ignore data) + valid_in = 0; + data_in = 8'hFF; @(posedge clk); + data_in = 8'hEE; @(posedge clk); + + // Test third pair: 0x78, 0x9A (should form 0x789A) + valid_in = 1; + data_in = 8'h78; @(posedge clk); + data_in = 8'h9A; @(posedge clk); + + // Test fourth pair: 0xBC, 0xDE (should form 0xBCDE) + data_in = 8'hBC; @(posedge clk); + data_in = 8'hDE; @(posedge clk); + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + valid_in <= $random; + data_in <= $urandom % 256; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_valid_out; + int errortime_valid_out; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic valid_in; + logic [7:0] data_in; + logic valid_out_ref; + logic valid_out_dut; + logic [15:0] data_out_ref; + logic [15:0] data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, valid_in, data_in, valid_out_ref, valid_out_dut, data_out_ref, data_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .valid_in, + .data_in ); + + RefModule good1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_ref), + .data_out(data_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .valid_in, + .data_in, + .valid_out(valid_out_dut), + .data_out(data_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_valid_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "valid_out", stats1.errors_valid_out, stats1.errortime_valid_out); + else $display("Hint: Output '%s' has no mismatches.", "valid_out"); + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { valid_out_ref, data_out_ref } === ( { valid_out_ref, data_out_ref } ^ { valid_out_dut, data_out_dut } ^ { valid_out_ref, data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (valid_out_ref !== ( valid_out_ref ^ valid_out_dut ^ valid_out_ref )) + begin if (stats1.errors_valid_out == 0) stats1.errortime_valid_out = $time; + stats1.errors_valid_out = stats1.errors_valid_out+1'b1; end + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob011_4-bit_carry_look-ahead_adder_circuit_prompt.txt b/Verilog Gen/dataset_self_contain/Prob011_4-bit_carry_look-ahead_adder_circuit_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..6cee405d2db2d73ad4ec0a437db065ee7e637b84 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob011_4-bit_carry_look-ahead_adder_circuit_prompt.txt @@ -0,0 +1,24 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input A_in (4 bits) + - input B_in (4 bits) + - input C_1 + - output S (4 bits) + - output CO + +Implement a 4-bit carry look-ahead adder using gate-level description in +Verilog. The logical expressions for the carry look-ahead adder are as +follows: + +Intermediate variables: G_i = A_i AND B_i, P_i = A_i XOR B_i + +Sum: S_i = P_i XOR C_(i-1), Carry: C_i = G_i OR (P_i AND C_(i-1)) + +Where C_i and C_(i-1) represent carry signals at positions i and i-1. + +The module should be implemented using gate-level primitives (AND, OR, XOR) +rather than behavioral descriptions. All signals should be declared as wire +type. + diff --git a/Verilog Gen/dataset_self_contain/Prob011_4-bit_carry_look-ahead_adder_circuit_ref.sv b/Verilog Gen/dataset_self_contain/Prob011_4-bit_carry_look-ahead_adder_circuit_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..c104cd362510376fdde777a47447ac151aa3f397 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob011_4-bit_carry_look-ahead_adder_circuit_ref.sv @@ -0,0 +1,18 @@ + +module RefModule( + input [3:0] A_in , + input [3:0] B_in , + input C_1 , + + output wire CO , + output wire [3:0] S +); + wire [3:0] G,P,C; + assign P=A_in^B_in; + assign G=A_in&B_in; + + assign C={G[3]|(P[3]&C[2]), G[2]|(P[2]&C[1]), G[1]|(P[1]&C[0]), G[0]|(P[0]&C_1)}; + assign S={P[3]^C[2], P[2]^C[1], P[1]^C[0], P[0]^C_1}; + assign CO=C[3]; + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob011_4-bit_carry_look-ahead_adder_circuit_test.sv b/Verilog Gen/dataset_self_contain/Prob011_4-bit_carry_look-ahead_adder_circuit_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..01cb3f2bee979edffaa00c96aa755bdf89161b11 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob011_4-bit_carry_look-ahead_adder_circuit_test.sv @@ -0,0 +1,220 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic [3:0] A_in, + output logic [3:0] B_in, + output logic C_1 + // === End your code here === +); + // === Start your code here === + initial begin + A_in <= 0; + B_in <= 0; + C_1 <= 0; + + // Test basic addition cases + @(posedge clk); + A_in <= 4'b0001; + B_in <= 4'b0001; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0010; + B_in <= 4'b0011; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0100; + B_in <= 4'b0100; + C_1 <= 1; + @(posedge clk); + + A_in <= 4'b1000; + B_in <= 4'b1000; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b1111; + B_in <= 4'b0001; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b1111; + B_in <= 4'b1111; + C_1 <= 1; + @(posedge clk); + + A_in <= 4'b1010; + B_in <= 4'b0101; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0110; + B_in <= 4'b1001; + C_1 <= 1; + @(posedge clk); + + // Test edge cases + A_in <= 4'b0000; + B_in <= 4'b0000; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0000; + B_in <= 4'b0000; + C_1 <= 1; + @(posedge clk); + + A_in <= 4'b1111; + B_in <= 4'b0000; + C_1 <= 0; + @(posedge clk); + + A_in <= 4'b0000; + B_in <= 4'b1111; + C_1 <= 1; + @(posedge clk); + + // Random test sequence + repeat(200) @(posedge clk) begin + A_in <= $random & 15; // 4-bit random + B_in <= $random & 15; // 4-bit random + C_1 <= $random & 1; // 1-bit random + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_S; + int errortime_S; + int errors_CO; + int errortime_CO; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic [3:0] A_in; + logic [3:0] B_in; + logic C_1; + logic [3:0] S_ref; + logic [3:0] S_dut; + logic CO_ref; + logic CO_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, A_in, B_in, C_1, S_ref, S_dut, CO_ref, CO_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .A_in, + .B_in, + .C_1 + ); + + RefModule good1 ( + .A_in, + .B_in, + .C_1, + .CO(CO_ref), + .S(S_ref) + ); + + TopModule top_module1 ( + .A_in, + .B_in, + .C_1, + .CO(CO_dut), + .S(S_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_S) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "S", stats1.errors_S, stats1.errortime_S); + else $display("Hint: Output '%s' has no mismatches.", "S"); + if (stats1.errors_CO) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "CO", stats1.errors_CO, stats1.errortime_CO); + else $display("Hint: Output '%s' has no mismatches.", "CO"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { S_ref, CO_ref } === ( { S_ref, CO_ref } ^ { S_dut, CO_dut } ^ { S_ref, CO_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (S_ref !== ( S_ref ^ S_dut ^ S_ref )) + begin if (stats1.errors_S == 0) stats1.errortime_S = $time; + stats1.errors_S = stats1.errors_S+1'b1; end + if (CO_ref !== ( CO_ref ^ CO_dut ^ CO_ref )) + begin if (stats1.errors_CO == 0) stats1.errortime_CO = $time; + stats1.errors_CO = stats1.errors_CO+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob012_state_machine_overlapping_sequence_detection_prompt.txt b/Verilog Gen/dataset_self_contain/Prob012_state_machine_overlapping_sequence_detection_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..fa91e86f578a5e2631346c98464cf0cce350a25a --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob012_state_machine_overlapping_sequence_detection_prompt.txt @@ -0,0 +1,21 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst + - input data + - output reg flag + +The module should implement a state machine that detects the sequence "1011" +with overlapping detection capability. The state machine should detect +multiple occurrences of the sequence even when they overlap. reset is active low. + +The sequence detection should work as follows: +- Monitor the input data stream continuously +- Detect when the sequence "1011" appears in the input +- Support overlapping detection: "10110111" should be detected twice +- Output flag signal should be high for one clock cycle when the sequence + is detected, starting from the next clock cycle after detection + +Clarification: The detector is a Moore machine and flag is a registered output. On the clock edge that completes 1011 the FSM enters the accept state, and flag is asserted on the following clock edge for exactly one cycle (a two-register-deep latency from the completing input bit). Overlapping matches are allowed. diff --git a/Verilog Gen/dataset_self_contain/Prob012_state_machine_overlapping_sequence_detection_ref.sv b/Verilog Gen/dataset_self_contain/Prob012_state_machine_overlapping_sequence_detection_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..9e6792cac0abf39247e8718dc9d59f9e638ff7b8 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob012_state_machine_overlapping_sequence_detection_ref.sv @@ -0,0 +1,40 @@ +module RefModule( + input wire clk , + input wire rst , + input wire data , + output reg flag +); +//*************code***********// + parameter S0=0, S1=1, S2=2, S3=3, S4=4; + reg [2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= S0; + else + state <= nstate; + end + + always@(*) begin + if(~rst) + nstate <= S0; + else + case(state) + S0 : nstate <= data? S1: S0; + S1 : nstate <= data? S1: S2; + S2 : nstate <= data? S3: S0; + S3 : nstate <= data? S4: S2; + S4 : nstate <= data? S1: S2; + default: nstate <= S0; + endcase + end + + always@(posedge clk or negedge rst) begin + if(~rst) + flag <= 0; + else + flag <= state==S4; + end + +//*************code***********// +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob012_state_machine_overlapping_sequence_detection_test.sv b/Verilog Gen/dataset_self_contain/Prob012_state_machine_overlapping_sequence_detection_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..a837d96a98935a96d3a2d8d9b3e47d692a7e56ee --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob012_state_machine_overlapping_sequence_detection_test.sv @@ -0,0 +1,164 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst, + output logic data + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + data = 0; + repeat(10) @(posedge clk); + rst <= 1; + @(posedge clk); + + // Test sequence "1011" - should detect once + data <= 1; @(posedge clk); // First bit + data <= 0; @(posedge clk); // Second bit + data <= 1; @(posedge clk); // Third bit + data <= 1; @(posedge clk); // Fourth bit - should detect here + + // Test overlapping sequences "10110111" - should detect twice + data <= 0; @(posedge clk); // Fifth bit + data <= 1; @(posedge clk); // Sixth bit + data <= 1; @(posedge clk); // Seventh bit - should detect here + data <= 1; @(posedge clk); // Eighth bit + + // Test non-matching sequence + data <= 0; @(posedge clk); + data <= 0; @(posedge clk); + data <= 1; @(posedge clk); + data <= 0; @(posedge clk); + + // Test another "1011" sequence + data <= 1; @(posedge clk); + data <= 0; @(posedge clk); + data <= 1; @(posedge clk); + data <= 1; @(posedge clk); // Should detect here + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + data <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_flag; + int errortime_flag; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic data; + logic flag_ref; + logic flag_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, data, flag_ref, flag_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst, + .data ); + + RefModule good1 ( + .clk, + .rst, + .data, + .flag(flag_ref) ); + + TopModule top_module1 ( + .clk, + .rst, + .data, + .flag(flag_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_flag) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "flag", stats1.errors_flag, stats1.errortime_flag); + else $display("Hint: Output '%s' has no mismatches.", "flag"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { flag_ref } === ( { flag_ref } ^ { flag_dut } ^ { flag_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (flag_ref !== ( flag_ref ^ flag_dut ^ flag_ref )) + begin if (stats1.errors_flag == 0) stats1.errortime_flag = $time; + stats1.errors_flag = stats1.errors_flag+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob013_least_common_multiple_prompt.txt b/Verilog Gen/dataset_self_contain/Prob013_least_common_multiple_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..857305edc9719832dc979c1c5c2d4cba4ad1fdb3 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob013_least_common_multiple_prompt.txt @@ -0,0 +1,30 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input A (8 bits) + - input B (8 bits) + - input vld_in + - output lcm_out (16 bits) + - output mcd_out (8 bits) + - output vld_out + +Design a sequential circuit that takes two unsigned numbers as input and +outputs their least common multiple (LCM) and greatest common divisor (GCD). +The bit width can be determined by parameter DATA_W. For this implementation, +DATA_W is 8, so A, B, and mcd_out are 8 bits while lcm_out is 16 bits. + +The circuit should operate as follows: +- When vld_in is asserted, the input data A and B are valid and should be + processed +- The circuit computes both the LCM and GCD of the two input numbers +- When the computation is complete, vld_out should be asserted to indicate + that the output data is valid +- The rst_n signal is an active-low reset signal + +Assume all sequential logic is triggered on the positive edge of the clock. + +Clarification: After the iterative reduction detects A==B, the FSM spends one additional dedicated Finish cycle in which the result output is latched to the converged value and vld_out is pulsed high for exactly that one cycle - the result and the valid pulse appear one clock cycle after equality is reached, not in the same cycle. + diff --git a/Verilog Gen/dataset_self_contain/Prob013_least_common_multiple_ref.sv b/Verilog Gen/dataset_self_contain/Prob013_least_common_multiple_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..14ae7aecafe6f6b06c7aebafdb173b0e33df4031 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob013_least_common_multiple_ref.sv @@ -0,0 +1,92 @@ + +module RefModule#( +parameter DATA_W = 8) +( +input [DATA_W-1:0] A, +input [DATA_W-1:0] B, +input vld_in, +input rst_n, +input clk, +output wire [DATA_W*2-1:0] lcm_out, +output wire [DATA_W-1:0] mcd_out, +output reg vld_out +); + reg [DATA_W*2-1:0] A_reg; + reg [DATA_W*2-1:0] B_reg; + reg [DATA_W*2-1:0] mcd_out_r1; + reg [DATA_W*2-1:0] mult_reg; + reg [1:0] c_state, n_state; + parameter IDLE = 'd0, lcm1 = 'd1, Finish = 'd2; + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) c_state <= IDLE; + else c_state <= n_state; + end + + always @(*) begin + case(c_state) + IDLE : if(vld_in) n_state = lcm1; + else n_state = IDLE; + lcm1 : if(A_reg == B_reg) n_state = Finish; + else n_state = lcm1; + Finish : n_state = IDLE; + default : n_state = IDLE; + endcase + end + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + else begin + case(c_state) + IDLE : + if(vld_in) begin + A_reg <= A; + B_reg <= B; + mult_reg <= A * B; + mcd_out_r1 <= 0; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + mult_reg <= mult_reg; + mcd_out_r1 <= 0; + end + lcm1 : + if(A_reg > B_reg) begin + A_reg <= A_reg - B_reg; + B_reg <= B_reg; + end + else if(A_reg < B_reg) begin + B_reg <= B_reg - A_reg; + A_reg <= A_reg; + end + else begin + A_reg <= A_reg; + B_reg <= B_reg; + end + Finish : mcd_out_r1 <= A_reg; + + default : begin + A_reg <= 'b0; + B_reg <= 'b0; + mcd_out_r1 <= 'b0; + mult_reg <= 'b0; + end + endcase + end + end + assign mcd_out = mcd_out_r1; + assign lcm_out = (mcd_out_r1 == 'd0) ? 'hz : (mult_reg / mcd_out_r1); // If the GCD register is 0, output high impedance, otherwise the circuit will have issues + + always @(posedge clk , negedge rst_n) begin + if(~rst_n) vld_out <= 1'b0; + else if(c_state == Finish) vld_out <= 1'b1; + else vld_out <= 1'b0; + end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob013_least_common_multiple_test.sv b/Verilog Gen/dataset_self_contain/Prob013_least_common_multiple_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..a66d05200f35a7bc58f27d975fb057bd095514a8 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob013_least_common_multiple_test.sv @@ -0,0 +1,238 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [7:0] A, + output logic [7:0] B, + output logic vld_in + // === End your code here === +); + // === Start your code here === + initial begin + rst_n <= 0; + A <= 0; + B <= 0; + vld_in <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic cases + A <= 8'd12; + B <= 8'd8; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); // Wait for computation + + A <= 8'd15; + B <= 8'd25; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd7; + B <= 8'd11; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd16; + B <= 8'd24; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + + // Test edge cases + A <= 8'd1; + B <= 8'd1; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd255; + B <= 8'd1; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + A <= 8'd0; + B <= 8'd5; + vld_in <= 1; + @(posedge clk); + vld_in <= 0; + repeat(20) @(posedge clk); + + // Random test sequence + repeat(50) @(posedge clk) begin + A <= $random & 255; // 8-bit random + B <= $random & 255; // 8-bit random + vld_in <= $random & 1; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_lcm_out; + int errortime_lcm_out; + int errors_mcd_out; + int errortime_mcd_out; + int errors_vld_out; + int errortime_vld_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [7:0] A; + logic [7:0] B; + logic vld_in; + logic [15:0] lcm_out_ref; + logic [15:0] lcm_out_dut; + logic [7:0] mcd_out_ref; + logic [7:0] mcd_out_dut; + logic vld_out_ref; + logic vld_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, A, B, vld_in, lcm_out_ref, lcm_out_dut, mcd_out_ref, mcd_out_dut, vld_out_ref, vld_out_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .A, + .B, + .vld_in + ); + + RefModule good1 ( + .A, + .B, + .vld_in, + .rst_n, + .clk, + .lcm_out(lcm_out_ref), + .mcd_out(mcd_out_ref), + .vld_out(vld_out_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .A, + .B, + .vld_in, + .lcm_out(lcm_out_dut), + .mcd_out(mcd_out_dut), + .vld_out(vld_out_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_lcm_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "lcm_out", stats1.errors_lcm_out, stats1.errortime_lcm_out); + else $display("Hint: Output '%s' has no mismatches.", "lcm_out"); + if (stats1.errors_mcd_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "mcd_out", stats1.errors_mcd_out, stats1.errortime_mcd_out); + else $display("Hint: Output '%s' has no mismatches.", "mcd_out"); + if (stats1.errors_vld_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "vld_out", stats1.errors_vld_out, stats1.errortime_vld_out); + else $display("Hint: Output '%s' has no mismatches.", "vld_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + // lcm_out/mcd_out are compared only while vld_out_ref is high: the spec defines the result only when vld_out + // is asserted (outside it the reference drives lcm_out='hz, which the X-only tolerance below cannot accept). + wire res_chk = (vld_out_ref === 1'b1); + assign tb_match = ( !res_chk || ( { lcm_out_ref, mcd_out_ref } === ( { lcm_out_ref, mcd_out_ref } ^ { lcm_out_dut, mcd_out_dut } ^ { lcm_out_ref, mcd_out_ref } ) ) ) + && ( vld_out_ref === ( vld_out_ref ^ vld_out_dut ^ vld_out_ref ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (res_chk && lcm_out_ref !== ( lcm_out_ref ^ lcm_out_dut ^ lcm_out_ref )) + begin if (stats1.errors_lcm_out == 0) stats1.errortime_lcm_out = $time; + stats1.errors_lcm_out = stats1.errors_lcm_out+1'b1; end + if (res_chk && mcd_out_ref !== ( mcd_out_ref ^ mcd_out_dut ^ mcd_out_ref )) + begin if (stats1.errors_mcd_out == 0) stats1.errortime_mcd_out = $time; + stats1.errors_mcd_out = stats1.errors_mcd_out+1'b1; end + if (vld_out_ref !== ( vld_out_ref ^ vld_out_dut ^ vld_out_ref )) + begin if (stats1.errors_vld_out == 0) stats1.errortime_vld_out = $time; + stats1.errors_vld_out = stats1.errors_vld_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob014_sequence_generator_prompt.txt b/Verilog Gen/dataset_self_contain/Prob014_sequence_generator_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..785d62936a5547aad0bdbcdfae0c8ad19985a0be --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob014_sequence_generator_prompt.txt @@ -0,0 +1,22 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output data + +Implement a module that continuously outputs the sequence 001011 in a +cyclic manner. + +The circuit should generate the pattern as follows: +- Output '0', then '0', then '1', then '0', then '1', then '1' +- After outputting the last '1', the sequence repeats from the beginning +- The sequence 001011 repeats indefinitely + +The rst_n signal is an active-low asynchronous reset signal; data is 0 +while rst_n is low. When reset is asserted, +the sequence should restart from the beginning. Assume all sequential +logic is triggered on the positive edge of the clock. + +Clarification: data is a registered output equal to bit 5 of a 6-bit register initialized to 001011 that rotates left (MSB-first) by one position each clock; because the output is registered it lags the rotation by one clock cycle. This fixes the post-reset phase and which bit appears in the first cycle after reset. diff --git a/Verilog Gen/dataset_self_contain/Prob014_sequence_generator_ref.sv b/Verilog Gen/dataset_self_contain/Prob014_sequence_generator_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..139d5513975bbb32f359727bfb417bd3d7338b02 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob014_sequence_generator_ref.sv @@ -0,0 +1,21 @@ + +module RefModule( + input clk, + input rst_n, + output reg data + ); + + reg [5:0] q; + + always@(posedge clk or negedge rst_n) + if (!rst_n) + q <= 6'b001011; + else + q <= {q[4:0],q[5]}; + + always@(posedge clk or negedge rst_n) + if (!rst_n) + data <= 1'd0; + else + data <= q[5]; +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob014_sequence_generator_test.sv b/Verilog Gen/dataset_self_contain/Prob014_sequence_generator_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..d674f42565304d0f2bcf1950261451fbe1fe02a7 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob014_sequence_generator_test.sv @@ -0,0 +1,139 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Let it run for multiple cycles + repeat(200) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_data; + int errortime_data; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic data_ref; + logic data_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, data_ref, data_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n + ); + + RefModule good1 ( + .clk, + .rst_n, + .data(data_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .data(data_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_data) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data", stats1.errors_data, stats1.errortime_data); + else $display("Hint: Output '%s' has no mismatches.", "data"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { data_ref } === ( { data_ref } ^ { data_dut } ^ { data_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (data_ref !== ( data_ref ^ data_dut ^ data_ref )) + begin if (stats1.errors_data == 0) stats1.errortime_data = $time; + stats1.errors_data = stats1.errors_data+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob015_vending_machine2_prompt.txt b/Verilog Gen/dataset_self_contain/Prob015_vending_machine2_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..78046d2b4b18901613613cb9f13fb1cf1572049e --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob015_vending_machine2_prompt.txt @@ -0,0 +1,43 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst + - input d1 + - input d2 + - input sel + - output reg out1 + - output reg out2 + - output reg out3 + +The module should implement a vending machine that accepts two types of +currency (0.5 yuan and 1 yuan) and dispenses two types of beverages +(1.5 yuan and 2.5 yuan) with change dispensing capability. +reset is active low: while rst is low the amount and out1, out2 and out3 +are 0 at once, without waiting for a clock edge + +The vending machine should work as follows: +- Input currency: d1 represents 0.5 yuan, d2 represents 1 yuan +- Beverage selection: sel chooses between two beverages +- Beverage prices: Beverage 1 costs 1.5 yuan, Beverage 2 costs 2.5 yuan +- Change dispensing: Only 0.5 yuan coins are dispensed as change + +Currency input behavior: +- Input currency signals (d1, d2) are automatically edge-detected +- Each currency input produces a pulse signal that goes high on clock + rising edge and low on clock falling edge +- The machine accumulates the total amount of money inserted + +Beverage dispensing: +- out1: Dispenses beverage 1 (1.5 yuan) +- out2: Dispenses beverage 2 (2.5 yuan) +- out3: Dispenses change (0.5 yuan coins only) + +sel = 0 selects beverage 1, sel = 1 selects beverage 2. In each cycle the output of the beverage that sel +currently selects is 1 when the amount accumulated at earlier rising edges is +at least its price, and out3 is also 1 when the amount exceeds the price; only +one 0.5 yuan coin is returned, whatever the excess. At the rising edge ending that cycle +the amount returns to zero and coins presented in that cycle are not counted. + +Clarification: Treat d1 and d2 as level signals - every clock cycle d1 is high adds 0.5 yuan and every cycle d2 is high adds 1 yuan; if both are high only d1 counts. Do NOT add your own edge detection. If inputs change within a cycle, each rising edge acts on the latest values seen in the ending cycle that called for a coin or a dispense, so a coin present only in the first half of a cycle still counts. Change (out3) is emitted in the same cycle as the beverage output (out1/out2), not on a following cycle. diff --git a/Verilog Gen/dataset_self_contain/Prob015_vending_machine2_ref.sv b/Verilog Gen/dataset_self_contain/Prob015_vending_machine2_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..a1ac1b88ccb70a8aeabfb77968ec0cc89b517377 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob015_vending_machine2_ref.sv @@ -0,0 +1,64 @@ + +module RefModule( + input wire clk , + input wire rst , + input wire d1 , + input wire d2 , + input wire sel , + + output reg out1, + output reg out2, + output reg out3 +); +//*************code***********// + + parameter S0=0, S0_5=1, S1=2, S1_5=3, S2=4, S2_5=5, S3=6; + reg[2:0] state, nstate; + + always@(posedge clk or negedge rst) begin + if(~rst) + state <= 0; + else + state <= nstate; + end + + always@(*) begin + case(state) + S0 : nstate = d1? S0_5: + d2? S1: + nstate; + S0_5 : nstate = d1? S1: + d2? S1_5: + nstate; + S1 : nstate = d1? S1_5: + d2? S2: + nstate; + S1_5 : nstate = ~sel? S0: + d1? S2: + d2? S2_5: + nstate; + S2 : nstate = ~sel? S0: + d1? S2_5: + d2? S3: + nstate; + default: nstate = S0; + endcase + end + + always@(*) begin + if(~rst) begin + {out1, out2, out3} = 3'b000; + end + else begin + case(state) + S0, S0_5, S1: {out1, out2, out3} = 0; + S1_5 : {out1, out2, out3} = ~sel? 3'b100: 3'b000; + S2 : {out1, out2, out3} = ~sel? 3'b101: 3'b000; + S2_5 : {out1, out2, out3} = ~sel? 3'b101: 3'b010; + S3 : {out1, out2, out3} = ~sel? 3'b101: 3'b011; + default : {out1, out2, out3} = 3'b000; + endcase + end + end +//*************code***********// +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob015_vending_machine2_test.sv b/Verilog Gen/dataset_self_contain/Prob015_vending_machine2_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..17d6dee4540eb7c8bbb9f648823598a0a643812f --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob015_vending_machine2_test.sv @@ -0,0 +1,214 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst, + output logic d1, + output logic d2, + output logic sel + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + d1 = 0; + d2 = 0; + sel = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test case 1: Insert 1.5 yuan and select beverage 1 (sel=0) + d1 = 1; @(posedge clk); // Insert 0.5 yuan + d1 = 0; @(posedge clk); + d1 = 1; @(posedge clk); // Insert another 0.5 yuan + d1 = 0; @(posedge clk); + d1 = 1; @(posedge clk); // Insert third 0.5 yuan + d1 = 0; @(posedge clk); + sel = 0; @(posedge clk); // Select beverage 1 + sel = 0; @(posedge clk); // Should dispense beverage 1 + + // Test case 2: Insert 2.5 yuan and select beverage 2 (sel=1) + d2 = 1; @(posedge clk); // Insert 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert another 1 yuan + d2 = 0; @(posedge clk); + d1 = 1; @(posedge clk); // Insert 0.5 yuan + d1 = 0; @(posedge clk); + sel = 1; @(posedge clk); // Select beverage 2 + sel = 1; @(posedge clk); // Should dispense beverage 2 + + // Test case 3: Insert 2 yuan and select beverage 1 (sel=0) - should get change + d2 = 1; @(posedge clk); // Insert 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert another 1 yuan + d2 = 0; @(posedge clk); + sel = 0; @(posedge clk); // Select beverage 1 + sel = 0; @(posedge clk); // Should dispense beverage 1 + 0.5 yuan change + + // Test case 4: Insert 3 yuan and select beverage 2 (sel=1) - should get change + d2 = 1; @(posedge clk); // Insert 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert another 1 yuan + d2 = 0; @(posedge clk); + d2 = 1; @(posedge clk); // Insert third 1 yuan + d2 = 0; @(posedge clk); + sel = 1; @(posedge clk); // Select beverage 2 + sel = 1; @(posedge clk); // Should dispense beverage 2 + 0.5 yuan change + + // Random testing + repeat(100) @(posedge clk, negedge clk) begin + d1 <= $random; + d2 <= $random; + sel <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_out1; + int errortime_out1; + int errors_out2; + int errortime_out2; + int errors_out3; + int errortime_out3; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic d1; + logic d2; + logic sel; + logic out1_ref; + logic out2_ref; + logic out3_ref; + logic out1_dut; + logic out2_dut; + logic out3_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, d1, d2, sel, out1_ref, out2_ref, out3_ref, out1_dut, out2_dut, out3_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst, + .d1, + .d2, + .sel ); + + RefModule good1 ( + .clk, + .rst, + .d1, + .d2, + .sel, + .out1(out1_ref), + .out2(out2_ref), + .out3(out3_ref) ); + + TopModule top_module1 ( + .clk, + .rst, + .d1, + .d2, + .sel, + .out1(out1_dut), + .out2(out2_dut), + .out3(out3_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_out1) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out1", stats1.errors_out1, stats1.errortime_out1); + else $display("Hint: Output '%s' has no mismatches.", "out1"); + if (stats1.errors_out2) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out2", stats1.errors_out2, stats1.errortime_out2); + else $display("Hint: Output '%s' has no mismatches.", "out2"); + if (stats1.errors_out3) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "out3", stats1.errors_out3, stats1.errortime_out3); + else $display("Hint: Output '%s' has no mismatches.", "out3"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { out1_ref, out2_ref, out3_ref } === ( { out1_ref, out2_ref, out3_ref } ^ { out1_dut, out2_dut, out3_dut } ^ { out1_ref, out2_ref, out3_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (out1_ref !== ( out1_ref ^ out1_dut ^ out1_ref )) + begin if (stats1.errors_out1 == 0) stats1.errortime_out1 = $time; + stats1.errors_out1 = stats1.errors_out1+1'b1; end + if (out2_ref !== ( out2_ref ^ out2_dut ^ out2_ref )) + begin if (stats1.errors_out2 == 0) stats1.errortime_out2 = $time; + stats1.errors_out2 = stats1.errors_out2+1'b1; end + if (out3_ref !== ( out3_ref ^ out3_dut ^ out3_ref )) + begin if (stats1.errors_out3 == 0) stats1.errortime_out3 = $time; + stats1.errors_out3 = stats1.errors_out3+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob016_odd-number_division_with_a_duty_cycle_of_half_prompt.txt b/Verilog Gen/dataset_self_contain/Prob016_odd-number_division_with_a_duty_cycle_of_half_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..0a00457f3f1755f0766bcfb98efb7a02e5d2399c --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob016_odd-number_division_with_a_duty_cycle_of_half_prompt.txt @@ -0,0 +1,26 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk_in + - input rst + - output clk_out7 + +The module should implement a clock divider that produces a 7:1 frequency +division with a 50% duty cycle output. reset is active low. + +The clock divider should work as follows: +- Input clock: clk_in (reference clock signal) +- Output clock: clk_out7 (divided clock signal) +- Division ratio: 7:1 (output frequency is 1/7 of input frequency) +- Duty cycle: 50% (output is high for half the period, low for half the period) + +Clock division behavior: +- The output clk_out7 should toggle at 1/7 the frequency of clk_in +- The output should maintain a 50% duty cycle (equal high and low times) +- The output should be synchronized to the input clock clk_in +- The division should be continuous and stable + +Clarification: Use a mod-7 counter on posedge clk_in. Generate clkp (posedge-clocked): set high when the counter==3 and low when counter==6. Generate clkn identically but clocked on negedge clk_in. The output is clk_out7 = clkp | clkn, which yields an exact 50% duty cycle with the high phase beginning at counter==3. + + diff --git a/Verilog Gen/dataset_self_contain/Prob016_odd-number_division_with_a_duty_cycle_of_half_ref.sv b/Verilog Gen/dataset_self_contain/Prob016_odd-number_division_with_a_duty_cycle_of_half_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..8c5863988ee32c234c730e1f807a42794cb70c74 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob016_odd-number_division_with_a_duty_cycle_of_half_ref.sv @@ -0,0 +1,56 @@ + +module RefModule + #(parameter N = 7) + ( + input wire rst , + input wire clk_in, + output wire clk_out7 + ); + + + + reg [3:0] cnt ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + cnt <= 'b0 ; + end + else if (cnt == N-1) begin + cnt <= 'b0 ; + end + else begin + cnt <= cnt + 1'b1 ; + end + end + + reg clkp ; + always @(posedge clk_in or negedge rst) begin + if (!rst) begin + clkp <= 1'b0 ; + end + else if (cnt == (N>>1)) begin + clkp <= 1 ; + end + else if (cnt == N-1) begin + clkp <= 0 ; + end + end + + + reg clkn ; + always @(negedge clk_in or negedge rst) begin + if (!rst) begin + clkn <= 1'b0 ; + end + else if (cnt == (N>>1) ) begin + clkn <= 1 ; + end + else if (cnt == N-1) begin + clkn <= 0 ; + end + end + + + assign clk_out7 = clkp | clkn ; + + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob016_odd-number_division_with_a_duty_cycle_of_half_test.sv b/Verilog Gen/dataset_self_contain/Prob016_odd-number_division_with_a_duty_cycle_of_half_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..f9718e4354229f62c57df0869666fd678dadda81 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob016_odd-number_division_with_a_duty_cycle_of_half_test.sv @@ -0,0 +1,133 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test clock divider for multiple cycles + repeat(300) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_clk_out7; + int errortime_clk_out7; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic clk_out7_ref; + logic clk_out7_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, clk_out7_ref, clk_out7_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst ); + + RefModule good1 ( + .rst, + .clk_in(clk), + .clk_out7(clk_out7_ref) ); + + TopModule top_module1 ( + .clk_in(clk), + .rst, + .clk_out7(clk_out7_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_clk_out7) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "clk_out7", stats1.errors_clk_out7, stats1.errortime_clk_out7); + else $display("Hint: Output '%s' has no mismatches.", "clk_out7"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { clk_out7_ref } === ( { clk_out7_ref } ^ { clk_out7_dut } ^ { clk_out7_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (clk_out7_ref !== ( clk_out7_ref ^ clk_out7_dut ^ clk_out7_ref )) + begin if (stats1.errors_clk_out7 == 0) stats1.errortime_clk_out7 = $time; + stats1.errors_clk_out7 = stats1.errors_clk_out7+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob017_arbitrary_fractional_frequency_division_prompt.txt b/Verilog Gen/dataset_self_contain/Prob017_arbitrary_fractional_frequency_division_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..7e79d41374eae0f03e050a56a64486a9fb56c801 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob017_arbitrary_fractional_frequency_division_prompt.txt @@ -0,0 +1,36 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk_in + - input rst + - output clk_out + +The module should implement an arbitrary fractional frequency divider that +can achieve non-integer division ratios, such as 8.7x frequency division. + +The fractional frequency divider should work as follows: +- Input clock: clk_in (reference clock signal) +- Output clock: clk_out (divided clock signal) +- Division ratio: 8.7x (output frequency is 1/8.7 of input frequency) +- Reset: rst is active low; clk_out is 0 while rst is low + +Fractional division principle: +Since hardware cannot perform fractional operations (e.g., 2.1 clock cycles +or 3.3 registers), fractional division cannot achieve exact fractional +periods for each output clock cycle. Instead, the implementation uses +the least common multiple approach to achieve the desired average frequency. + +For 8.7x division: +- Over 10 input clock cycles, the output should produce 53 output clock + cycles to achieve an average 8.7x division ratio +- This means: 53 output cycles / 10 input cycles = 5.3x, which when + inverted gives 1/5.3 ≈ 1/8.7x frequency division + +I give u some useful parameter: +parameter M_N = 8'd87; +parameter c89 = 8'd24; // 8/9 clock switching point +parameter div_e = 5'd8; //even period +parameter div_o = 5'd9; //odd period + +Clarification: Within each divided period, clk_out is high while the in-period counter <= (div>>2)+1 (the counter starts at 0, so high for 4 input cycles of the period, low for the rest; clk_out first rises on the first clk_in rising edge after rst goes high), where div is 8 for even periods and 9 for odd periods. Alternate even/odd period lengths using a cycle counter that flips the parity at cyc_cnt = c89-1 (23) and M_N-1 (86). diff --git a/Verilog Gen/dataset_self_contain/Prob017_arbitrary_fractional_frequency_division_ref.sv b/Verilog Gen/dataset_self_contain/Prob017_arbitrary_fractional_frequency_division_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..24421b51278163d6754febe5a7e8b7d194d99911 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob017_arbitrary_fractional_frequency_division_ref.sv @@ -0,0 +1,51 @@ + +module RefModule( + input wire clk_in, + input wire rst, + output wire clk_out +); + parameter M_N = 8'd87; + parameter c89 = 8'd24; // 8/9 clock switching point + parameter div_e = 5'd8; //Even cycle + parameter div_o = 5'd9; //Odd cycle +//*************code***********// + reg [3:0] clk_cnt; + reg [6:0] cyc_cnt; + reg div_flag; + reg clk_out_r; + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_cnt <= 0; + else if(~div_flag) + clk_cnt <= clk_cnt==(div_e-1)? 0: clk_cnt+1; + else + clk_cnt <= clk_cnt==(div_o-1)? 0: clk_cnt+1; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + cyc_cnt <= 0; + else + cyc_cnt <= cyc_cnt==(M_N-1)? 0: cyc_cnt+1; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + div_flag <= 0; + else + div_flag <= cyc_cnt==(M_N-1)||cyc_cnt==(c89-1)? ~div_flag: div_flag; + end + + always@(posedge clk_in or negedge rst) begin + if(~rst) + clk_out_r <= 0; + else if(~div_flag) + clk_out_r <= clk_cnt<=((div_e>>2)+1); + else + clk_out_r <= clk_cnt<=((div_o>>2)+1); + end + + assign clk_out = clk_out_r; +//*************code***********// +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob017_arbitrary_fractional_frequency_division_test.sv b/Verilog Gen/dataset_self_contain/Prob017_arbitrary_fractional_frequency_division_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..999415b04af6fa5676ee8d641f6deb0976eb13e9 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob017_arbitrary_fractional_frequency_division_test.sv @@ -0,0 +1,134 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test fractional frequency divider for multiple cycles + // The divider should achieve 8.7x division ratio + repeat(1000) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_clk_out; + int errortime_clk_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic clk_out_ref; + logic clk_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, clk_out_ref, clk_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst ); + + RefModule good1 ( + .clk_in(clk), + .rst, + .clk_out(clk_out_ref) ); + + TopModule top_module1 ( + .clk_in(clk), + .rst, + .clk_out(clk_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_clk_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "clk_out", stats1.errors_clk_out, stats1.errortime_clk_out); + else $display("Hint: Output '%s' has no mismatches.", "clk_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { clk_out_ref } === ( { clk_out_ref } ^ { clk_out_dut } ^ { clk_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (clk_out_ref !== ( clk_out_ref ^ clk_out_dut ^ clk_out_ref )) + begin if (stats1.errors_clk_out == 0) stats1.errortime_clk_out = $time; + stats1.errors_clk_out = stats1.errors_clk_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob020_write_state_machine_two_stage_prompt.txt b/Verilog Gen/dataset_self_contain/Prob020_write_state_machine_two_stage_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..0afd3eadf679c2bc4b97cecadaa7f585c3b8a6df --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob020_write_state_machine_two_stage_prompt.txt @@ -0,0 +1,33 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst + - input data + - output reg flag + +The module should implement a Moore state machine using a two-stage +description method. The state machine has five states (S0, S1, S2, S3, S4) +and transitions based on the input data signal. + +The state machine should work as follows: +- Input signal: data (wire type) +- Output signal: flag (reg type) +- Reset: rst is active low +- Clock: clk for synchronous operation + +State transition table: + data +current state 1 0 + S0 next state S1 S0 + S1 S2 S1 + S2 S3 S2 + S3 S4 S3 + S4 S1 S0 + +Output logic: +- When current state is S4, the output flag should be 1 +- For all other states (S0, S1, S2, S3), the output flag should be 0 + +Clarification: rst is an asynchronous active-low reset. Code the state register as always @(posedge clk or negedge rst), forcing the state to S0 (and flag to 0) immediately when rst is low, independent of the clock. flag must read 0 whenever rst is low, including from time 0 before any clock edge. diff --git a/Verilog Gen/dataset_self_contain/Prob020_write_state_machine_two_stage_ref.sv b/Verilog Gen/dataset_self_contain/Prob020_write_state_machine_two_stage_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..69b0e0a926b2bc6882fd35c71c9e3ce3967e70ff --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob020_write_state_machine_two_stage_ref.sv @@ -0,0 +1,53 @@ +`timescale 1ns/1ns + + +module RefModule( + input wire clk , + input wire rst , + input wire data , + output reg flag +); + + parameter S0 = 'd0, S1 = 'd1, S2 = 'd2, S3 = 'd3 ,S4 = 'd4 ; + reg [2:0] current_state; + reg [2:0] next_state; + + always@(posedge clk or negedge rst)begin + if(rst == 1'b0)begin + current_state <= S0; + end + else begin + current_state <= next_state; + end + end + + always@(*)begin + case(current_state) + S0:begin + next_state = data ? S1 : S0; + flag = 1'b0; + end + S1:begin + next_state = data ? S2 : S1; + flag = 1'b0; + end + S2:begin + next_state = data ? S3 : S2; + flag = 1'b0; + end + S3:begin + next_state = data ? S4 : S3; + flag = 1'b0; + end + S4:begin + next_state = data ? S1 : S0; + flag = 1'b1; + end + default:begin + next_state = S0; + flag = 1'b0; + end + endcase + end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob020_write_state_machine_two_stage_test.sv b/Verilog Gen/dataset_self_contain/Prob020_write_state_machine_two_stage_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..84ae9288b92210b890dc070ecc6902848b26b92f --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob020_write_state_machine_two_stage_test.sv @@ -0,0 +1,174 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst, + output logic data + // === End your code here === +); + // === Start your code here === + initial begin + rst = 0; + data = 0; + repeat(10) @(posedge clk); + rst = 1; + @(posedge clk); + + // Test state transitions: S0->S1->S2->S3->S4 (flag=1) + data = 1; @(posedge clk); // S0->S1 + data = 1; @(posedge clk); // S1->S2 + data = 1; @(posedge clk); // S2->S3 + data = 1; @(posedge clk); // S3->S4 (flag should be 1) + + // Test S4->S1 transition (data=1) + data = 1; @(posedge clk); // S4->S1 + + // Test S4->S0 transition (data=0) + data = 1; @(posedge clk); // S1->S2 + data = 1; @(posedge clk); // S2->S3 + data = 1; @(posedge clk); // S3->S4 + data = 0; @(posedge clk); // S4->S0 + + // Test staying in S0 (data=0) + data = 0; @(posedge clk); // S0->S0 + data = 0; @(posedge clk); // S0->S0 + + // Test staying in S1 (data=0) + data = 1; @(posedge clk); // S0->S1 + data = 0; @(posedge clk); // S1->S1 + data = 0; @(posedge clk); // S1->S1 + + // Test staying in S2 (data=0) + data = 1; @(posedge clk); // S1->S2 + data = 0; @(posedge clk); // S2->S2 + data = 0; @(posedge clk); // S2->S2 + + // Test staying in S3 (data=0) + data = 1; @(posedge clk); // S2->S3 + data = 0; @(posedge clk); // S3->S3 + data = 0; @(posedge clk); // S3->S3 + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + data <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_flag; + int errortime_flag; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst; + logic data; + logic flag_ref; + logic flag_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst, data, flag_ref, flag_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst, + .data ); + + RefModule good1 ( + .clk, + .rst, + .data, + .flag(flag_ref) ); + + TopModule top_module1 ( + .clk, + .rst, + .data, + .flag(flag_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_flag) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "flag", stats1.errors_flag, stats1.errortime_flag); + else $display("Hint: Output '%s' has no mismatches.", "flag"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { flag_ref } === ( { flag_ref } ^ { flag_dut } ^ { flag_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (flag_ref !== ( flag_ref ^ flag_dut ^ flag_ref )) + begin if (stats1.errors_flag == 0) stats1.errortime_flag = $time; + stats1.errors_flag = stats1.errors_flag+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob023_gray_code_counter_prompt.txt b/Verilog Gen/dataset_self_contain/Prob023_gray_code_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..15d1ac5efd07dd60d695fac3cd0a918b8cafdcdf --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob023_gray_code_counter_prompt.txt @@ -0,0 +1,27 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output reg [3:0] gray_out + +The module should implement a 4-bit Gray code counter. Assume all sequential +logic is triggered on the positive edge of the clock. The reset input is active +low asynchronous, and should reset the counter to 0. + +The Gray code counter should work as follows: +- The counter should generate a 4-bit Gray code sequence +- The counter increments every two clock cycles; after reset, gray_out first + becomes 0001 at the second rising edge of clk with rst_n high +- The implementation should follow this process: + 1. Input a Gray code value (use an internal wire variable initialized to 0) + 2. Convert the Gray code to binary representation + 3. Increment the binary value by 1 + 4. Convert the incremented binary back to Gray code + 5. Output the resulting Gray code +- Gray code is a binary numeral system where two successive values differ + in only one bit position +- The counter should cycle through all 16 possible 4-bit Gray code values + and then repeat +- When rst_n is low, the counter should reset to 0000 diff --git a/Verilog Gen/dataset_self_contain/Prob023_gray_code_counter_ref.sv b/Verilog Gen/dataset_self_contain/Prob023_gray_code_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..894e7d68646d0b1562e7cdf67d0a96597dac0e4f --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob023_gray_code_counter_ref.sv @@ -0,0 +1,44 @@ + +module RefModule( + input clk, + input rst_n, + + output reg [3:0] gray_out +); +//Gray code to binary conversion +reg [3:0] bin_out; +wire [3:0] gray_wire; + +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_out <= 4'b0; + end + else begin + bin_out[3] = gray_wire[3]; + bin_out[2] = gray_wire[2]^bin_out[3]; + bin_out[1] = gray_wire[1]^bin_out[2]; + bin_out[0] = gray_wire[0]^bin_out[1]; + end +end +//Binary increment +reg [3:0] bin_add_wire; +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + bin_add_wire <= 4'b0; + end + else begin + bin_add_wire <= bin_out + 1'b1; + end +end +//Binary to gray code conversion +assign gray_wire = (bin_add_wire >> 1) ^ bin_add_wire; + +always @(posedge clk or negedge rst_n)begin + if(rst_n == 1'b0) begin + gray_out <= 4'b0; + end + else begin + gray_out <= gray_wire; + end +end +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob023_gray_code_counter_test.sv b/Verilog Gen/dataset_self_contain/Prob023_gray_code_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..750db322c6dfc9fdea5ad5bef6a330c4d7f071a8 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob023_gray_code_counter_test.sv @@ -0,0 +1,142 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test Gray code counter for multiple cycles + repeat(200) @(posedge clk); + + // Additional checks: rst_n is active-low asynchronous and resets the counter to 0. + // The original stimulus only resets at t=0 (held from the start). Assert rst_n mid-run, + // between clock edges, while gray_out is non-zero, and hold it low to the end: gray_out + // must go to 0 at once and stay 0. (The reset is not released again: after a mid-run + // release RefModule restarts with 0001 held for one cycle only, which disagrees with the + // spec's "increments every two clock cycles", so the post-reset restart is not checked.) + repeat (5) @(posedge clk); + #2 rst_n = 0; // assert 2 ps after a rising edge + repeat (20) @(posedge clk); + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_gray_out; + int errortime_gray_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [3:0] gray_out_ref; + logic [3:0] gray_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, gray_out_ref, gray_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .gray_out(gray_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .gray_out(gray_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_gray_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "gray_out", stats1.errors_gray_out, stats1.errortime_gray_out); + else $display("Hint: Output '%s' has no mismatches.", "gray_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { gray_out_ref } === ( { gray_out_ref } ^ { gray_out_dut } ^ { gray_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (gray_out_ref !== ( gray_out_ref ^ gray_out_dut ^ gray_out_ref )) + begin if (stats1.errors_gray_out == 0) stats1.errortime_gray_out = $time; + stats1.errors_gray_out = stats1.errors_gray_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob024_multiplication_and_bitwise_operations_prompt.txt b/Verilog Gen/dataset_self_contain/Prob024_multiplication_and_bitwise_operations_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..1c5b227c2167a9e275fe7f25ed4514c3df7586a6 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob024_multiplication_and_bitwise_operations_prompt.txt @@ -0,0 +1,10 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input A (8 bits) + - output B (16 bits) + +Design a computational unit circuit that multiplies A[7:0] by the constant +0xFB (binary 11111011). You should implement this using as few resources +as possible. diff --git a/Verilog Gen/dataset_self_contain/Prob024_multiplication_and_bitwise_operations_ref.sv b/Verilog Gen/dataset_self_contain/Prob024_multiplication_and_bitwise_operations_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..e4f0d68117fad73983a4ce341fc8f75e4dbc3c54 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob024_multiplication_and_bitwise_operations_ref.sv @@ -0,0 +1,12 @@ + +module RefModule( + input [7:0] A, + output [15:0] B + ); + +//*************code***********// + + assign B = (A<<8)-(A<<2)-A; +//*************code***********// + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob024_multiplication_and_bitwise_operations_test.sv b/Verilog Gen/dataset_self_contain/Prob024_multiplication_and_bitwise_operations_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..ed0a3fd059cc6bcba35d77a1f961ec9015cbb5ae --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob024_multiplication_and_bitwise_operations_test.sv @@ -0,0 +1,160 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic [7:0] A + // === End your code here === +); + // === Start your code here === + initial begin + A <= 0; + + // Test basic cases + @(posedge clk); + A <= 8'd0; + @(posedge clk); + + + A <= 8'd200; + @(posedge clk); + + A <= 8'd250; + @(posedge clk); + + A <= 8'd255; + @(posedge clk); + + // Test edge cases + A <= 8'd128; + @(posedge clk); + + A <= 8'd64; + @(posedge clk); + + A <= 8'd32; + @(posedge clk); + + A <= 8'd16; + @(posedge clk); + + // Random test sequence + repeat(200) @(posedge clk) begin + A <= $random & 255; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_B; + int errortime_B; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic [7:0] A; + logic [15:0] B_ref; + logic [15:0] B_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, A, B_ref, B_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .A + ); + + RefModule good1 ( + .A, + .B(B_ref) + ); + + TopModule top_module1 ( + .A, + .B(B_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_B) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "B", stats1.errors_B, stats1.errortime_B); + else $display("Hint: Output '%s' has no mismatches.", "B"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { B_ref } === ( { B_ref } ^ { B_dut } ^ { B_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (B_ref !== ( B_ref ^ B_dut ^ B_ref )) + begin if (stats1.errors_B == 0) stats1.errortime_B = $time; + stats1.errors_B = stats1.errors_B+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob025_pulse_synchronization_circuit_prompt.txt b/Verilog Gen/dataset_self_contain/Prob025_pulse_synchronization_circuit_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..26952e9fb80a840bd054664b3d88d48b67830003 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob025_pulse_synchronization_circuit_prompt.txt @@ -0,0 +1,27 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk_fast + - input clk_slow + - input rst_n + - input data_in + - output data_out + +Extract a single clock cycle width pulse from clock domain A and establish +another single clock width pulse in a new clock domain B. + +The frequency of clock domain A is 10 times that of clock domain B. The +intervals between pulses in clock domain A are large, so there is no need +to consider the problem of pulses being too close together. + +At every rising edge of clk_fast at which data_in is 1 (sampled as a level, +not edge-detected), the fast clock domain will toggle a flag +signal and synchronize it to the slow clock domain. After the slow clock +domain detects the change in this flag signal, it outputs a corresponding +single cycle pulse: data_out is 1 from the second until the third rising edge +of clk_slow after the clk_fast edge that samples data_in high. + +The rst_n signal is an active-low asynchronous reset signal; data_out is 0 +while it is low. Assume all +sequential logic is triggered on the positive edge of the clock. diff --git a/Verilog Gen/dataset_self_contain/Prob025_pulse_synchronization_circuit_ref.sv b/Verilog Gen/dataset_self_contain/Prob025_pulse_synchronization_circuit_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..370add92fcdb39b2b3bfc5db449ea3df65945962 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob025_pulse_synchronization_circuit_ref.sv @@ -0,0 +1,45 @@ + +module RefModule( + input clk_fast , + input clk_slow , + input rst_n , + input data_in , + + output data_out +); +reg Q_fast; +always @(posedge clk_fast or negedge rst_n) begin + if(~rst_n) begin + Q_fast <= 'd0; + end + else if(data_in)begin + Q_fast <= ~Q_fast; + end + else if(~data_in)begin + Q_fast <= Q_fast; + end +end +reg Q_buff0; +reg Q_buff1; +always @(posedge clk_slow or negedge rst_n) begin + if(~rst_n) begin + Q_buff0 <= 'd0; + Q_buff1 <= 'd0; + end + else begin + Q_buff0 <= Q_fast; + Q_buff1 <= Q_buff0; + end +end +reg Q_slow; +always @(posedge clk_slow or negedge rst_n) begin + if(~rst_n) begin + Q_slow <= 'd0; + end + else begin + Q_slow <= Q_buff1; + end +end + +assign data_out = Q_buff1 ^ Q_slow; +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob025_pulse_synchronization_circuit_test.sv b/Verilog Gen/dataset_self_contain/Prob025_pulse_synchronization_circuit_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..83b33a3fd8b9a76aab2fb8cbed79f52b104adafc --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob025_pulse_synchronization_circuit_test.sv @@ -0,0 +1,221 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic clk_fast, + output logic clk_slow, + output logic rst_n, + output logic data_in + // === End your code here === +); + // === Start your code here === + initial begin + clk_fast <= 0; + clk_slow <= 0; + rst_n <= 0; + data_in <= 0; + + // Generate fast clock (10x faster than slow clock) + forever begin + #5 clk_fast <= ~clk_fast; + end + end + + initial begin + // Generate slow clock + forever begin + #50 clk_slow <= ~clk_slow; + end + end + + initial begin + // Reset sequence + @(posedge clk_fast); + @(posedge clk_fast); + rst_n <= 1; + @(posedge clk_fast); + + // Test basic pulse detection + data_in <= 1; + @(posedge clk_fast); + data_in <= 0; + @(posedge clk_fast); + @(posedge clk_fast); + @(posedge clk_fast); + + // Wait for slow clock to process + repeat(20) @(posedge clk_slow); + + // Test multiple pulses + data_in <= 1; + @(posedge clk_fast); + @(posedge clk_fast); + @(posedge clk_fast); + @(posedge clk_fast); + data_in <= 0; + @(posedge clk_fast); + @(posedge clk_fast); + + // Wait for slow clock to process + repeat(20) @(posedge clk_slow); + + // Random test sequence + repeat(100) @(posedge clk_fast) begin + data_in <= $random & 1; + end + + // Additional checks: "The rst_n signal is an active-low asynchronous reset signal; data_out + // is 0 while it is low." The original stimulus only resets at the start. Here rst_n is asserted + // mid-run while data_out is high (between the 2nd and 3rd clk_slow rising edges after an isolated + // one-cycle data_in pulse), between clock edges, and held for three clk_slow periods; then isolated + // pulses resume after the release. + @(posedge clk_fast) data_in <= 0; + repeat (5) @(posedge clk_slow); + for (int k = 0; k < 3; k++) begin + @(posedge clk_slow); + repeat (2 + 3 * k) @(posedge clk_fast); + data_in <= 1; // isolated one-cycle pulse + @(posedge clk_fast) data_in <= 0; + repeat (2) @(posedge clk_slow); // data_out is high from here to the next clk_slow edge + #22 rst_n <= 0; // not on any clock edge + repeat (3) @(posedge clk_slow); + #22 rst_n <= 1; + repeat (3) @(posedge clk_slow); + // isolated pulses after the release + for (int j = 0; j < 4; j++) begin + @(posedge clk_slow); + repeat (1 + 2 * j + k) @(posedge clk_fast); + data_in <= 1; + @(posedge clk_fast) data_in <= 0; + repeat (5) @(posedge clk_slow); + end + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_data_out; + int errortime_data_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic clk_fast; + logic clk_slow; + logic rst_n; + logic data_in; + logic data_out_ref; + logic data_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, clk_fast, clk_slow, rst_n, data_in, data_out_ref, data_out_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .clk_fast, + .clk_slow, + .rst_n, + .data_in + ); + + RefModule good1 ( + .clk_fast, + .clk_slow, + .rst_n, + .data_in, + .data_out(data_out_ref) + ); + + TopModule top_module1 ( + .clk_fast, + .clk_slow, + .rst_n, + .data_in, + .data_out(data_out_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_data_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "data_out", stats1.errors_data_out, stats1.errortime_data_out); + else $display("Hint: Output '%s' has no mismatches.", "data_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { data_out_ref } === ( { data_out_ref } ^ { data_out_dut } ^ { data_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (data_out_ref !== ( data_out_ref ^ data_out_dut ^ data_out_ref )) + begin if (stats1.errors_data_out == 0) stats1.errortime_data_out = $time; + stats1.errors_data_out = stats1.errors_data_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob026_simple_stopwatch_prompt.txt b/Verilog Gen/dataset_self_contain/Prob026_simple_stopwatch_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..b45f6046a74fb47a98a5b7fb1f0b0fc4b3b2f114 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob026_simple_stopwatch_prompt.txt @@ -0,0 +1,21 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output reg [5:0] second + - output reg [5:0] minute + +The module should implement a simple stopwatch functionality. Assume all +sequential logic is triggered on the positive edge of the clock. The reset +input is active low asynchronous, and should reset both counters to 0. + +The simple stopwatch should work as follows: +- The second counter counts from 1 to 60 in a cycle +- When the second counter reaches 60, it resets to 1 and the minute counter + increments by 1 +- The minute counter counts from 0 to 60 +- When the minute counter reaches 60, the stopwatch pauses counting +- Both counters should reset to 0 when rst_n is low + diff --git a/Verilog Gen/dataset_self_contain/Prob026_simple_stopwatch_ref.sv b/Verilog Gen/dataset_self_contain/Prob026_simple_stopwatch_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..bd35c5aaeb9b14ce97cf12d1d4138ffc5c7beb7f --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob026_simple_stopwatch_ref.sv @@ -0,0 +1,38 @@ + +module RefModule( + input clk, + input rst_n, + + output reg [5:0]second, + output reg [5:0]minute + ); + + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + minute <= 6'd0; + end + else if (second == 6'd60) + begin + minute <= minute+1; + end + else + begin + minute <= minute; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + second <= 6'd0; + end + else if(second == 6'd60) + begin + second <= 6'd1; + end + else if (minute == 60) + second <= second; + else + second <= second+1'd1; +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob026_simple_stopwatch_test.sv b/Verilog Gen/dataset_self_contain/Prob026_simple_stopwatch_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..2e9249e83fe18d332a6bc787b4165e264d25c592 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob026_simple_stopwatch_test.sv @@ -0,0 +1,146 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test stopwatch for multiple cycles + // The stopwatch counts seconds from 1-60, then increments minutes + // When minute reaches 60, the stopwatch pauses + repeat(5000) @(posedge clk); + + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_second; + int errortime_second; + int errors_minute; + int errortime_minute; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [5:0] second_ref; + logic [5:0] minute_ref; + logic [5:0] second_dut; + logic [5:0] minute_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, second_ref, minute_ref, second_dut, minute_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .second(second_ref), + .minute(minute_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .second(second_dut), + .minute(minute_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_second) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "second", stats1.errors_second, stats1.errortime_second); + else $display("Hint: Output '%s' has no mismatches.", "second"); + if (stats1.errors_minute) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "minute", stats1.errors_minute, stats1.errortime_minute); + else $display("Hint: Output '%s' has no mismatches.", "minute"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { second_ref, minute_ref } === ( { second_ref, minute_ref } ^ { second_dut, minute_dut } ^ { second_ref, minute_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (second_ref !== ( second_ref ^ second_dut ^ second_ref )) + begin if (stats1.errors_second == 0) stats1.errortime_second = $time; + stats1.errors_second = stats1.errors_second+1'b1; end + if (minute_ref !== ( minute_ref ^ minute_dut ^ minute_ref )) + begin if (stats1.errors_minute == 0) stats1.errortime_minute = $time; + stats1.errors_minute = stats1.errors_minute+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob027_settable_counter_prompt.txt b/Verilog Gen/dataset_self_contain/Prob027_settable_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..b9119e916499b53214687732482bce9305a13c98 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob027_settable_counter_prompt.txt @@ -0,0 +1,35 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input set + - input [3:0] set_num + - output reg zero + - output reg [3:0] number + +The module should implement a hexadecimal settable counter. Assume all +sequential logic is triggered on the positive edge of the clock. The reset +input is active low asynchronous, and should reset the counter to 0. + +The settable counter should work as follows: +- The counter increments on each clock cycle and cycles through hexadecimal + values (0 to F, then back to 0) +- When the counter reaches 0, the zero signal is asserted (goes high) +- When the set signal is active, the counter output is forced to the value + of set_num instead of incrementing +- The zero signal is high only when the number equals 0, and low at all + other times +- When rst_n is low, the counter should reset to 0; number and zero are 0 + at once while rst_n is low, and zero goes high at the first rising clock + edge after rst_n returns high +- the output zero and number will be update on the next rising edge of the clock. + +Counter operation: +- Normal counting: Counter increments each clock cycle (0, 1, 2, ..., F, 0, 1, ...). +- Set operation: When set is active, number is set to set_num value +- Zero detection: The zero signal is high only when number equals 0 +- Reset behavior: Active low reset to 0 + +Clarification: number is a registered copy of the internal counter, delayed by one additional clock cycle. Maintain the count (increment/set/reset) in an internal register, then on the next clock edge drive number (and the zero flag derived from it) from that internal value, so number lags the internally counted value by exactly one cycle. diff --git a/Verilog Gen/dataset_self_contain/Prob027_settable_counter_ref.sv b/Verilog Gen/dataset_self_contain/Prob027_settable_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..f9cf9eb7c182e1264308d1d243de4012ff9a028c --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob027_settable_counter_ref.sv @@ -0,0 +1,50 @@ + +module RefModule( + input clk, + input rst_n, + input set, + input [3:0] set_num, + output reg [3:0]number, + output reg zero + ); + reg [3:0]num; + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + zero <= 1'd0; + end + else if (num == 4'd0) + begin + zero <= 1'b1; + end + else + begin + zero <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + num <= 4'b0; + end + else if(set) + begin + num <= set_num; + end + else + begin + num <= num + 1'd1; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + number <= 1'd0; + end + else + begin + number <= num; + end + +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob027_settable_counter_test.sv b/Verilog Gen/dataset_self_contain/Prob027_settable_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..c6249e9b80c00ef43e9b590baab344971408bed8 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob027_settable_counter_test.sv @@ -0,0 +1,175 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic set, + output logic [3:0] set_num + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + set = 0; + set_num = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test normal counting + set = 0; + repeat(20) @(posedge clk); // Count: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,0,1,2,3 + + // Test set operation + set = 1; + set_num = 4'hA; @(posedge clk); // Set to A + set_num = 4'h5; @(posedge clk); // Set to 5 + set_num = 4'hF; @(posedge clk); // Set to F + set = 0; + @(posedge clk); + // Test set to 0 (should trigger zero signal) + set = 1; + set_num = 4'h0; @(posedge clk); // Set to 0 + set = 0; + @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + set <= $random; + set_num <= $random % 16; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_number; + int errortime_number; + int errors_zero; + int errortime_zero; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic set; + logic [3:0] set_num; + logic [3:0] number_ref; + logic zero_ref; + logic [3:0] number_dut; + logic zero_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, set, set_num, number_ref, zero_ref, number_dut, zero_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .set, + .set_num ); + + RefModule good1 ( + .clk, + .rst_n, + .set, + .set_num, + .number(number_ref), + .zero(zero_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .set, + .set_num, + .number(number_dut), + .zero(zero_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_number) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "number", stats1.errors_number, stats1.errortime_number); + else $display("Hint: Output '%s' has no mismatches.", "number"); + if (stats1.errors_zero) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "zero", stats1.errors_zero, stats1.errortime_zero); + else $display("Hint: Output '%s' has no mismatches.", "zero"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { number_ref, zero_ref } === ( { number_ref, zero_ref } ^ { number_dut, zero_dut } ^ { number_ref, zero_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (number_ref !== ( number_ref ^ number_dut ^ number_ref )) + begin if (stats1.errors_number == 0) stats1.errortime_number = $time; + stats1.errors_number = stats1.errors_number+1'b1; end + if (zero_ref !== ( zero_ref ^ zero_dut ^ zero_ref )) + begin if (stats1.errors_zero == 0) stats1.errortime_zero = $time; + stats1.errors_zero = stats1.errors_zero+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob028_up_and_down_counter_prompt.txt b/Verilog Gen/dataset_self_contain/Prob028_up_and_down_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..68f33d9b506421eddd7eaf7d5163ec5d538c95f2 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob028_up_and_down_counter_prompt.txt @@ -0,0 +1,29 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input mode + - output reg [3:0] number + - output reg zero + +The module should implement a decimal up/down counter. Assume all sequential +logic is triggered on the positive edge of the clock. The reset input is active +low asynchronous, and should reset the counter to 0, and zero is also be 0. + +The up/down counter should work as follows: +- When mode signal is 1, the counter increments (counts up) each clock cycle +- When mode signal is 0, the counter decrements (counts down) each clock cycle +- The counter output (number) is a 4-bit value representing the current count +- The zero signal is asserted (goes high) for the next cycle when the number reaches 0, and is + low at all other times +- When rst_n is low, the counter should reset to 0 + +Counter operation: +- Mode = 1: Counter increments each clock cycle (0, 1, 2, 3, ..., 9, 0, 1, ...), only up to 9 +- Mode = 0: Counter decrements each clock cycle (9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, ...) +- Zero detection: The zero signal is high for next cycle only when number equals 0 +- Reset behavior: Active low reset to 0 + +Clarification: zero is a registered flag asserted in the cycle immediately AFTER number equals 0. Register zero from the present value of number (zero becomes 1 on the clock edge following the one where number is 0), not from the next/predicted counter value. diff --git a/Verilog Gen/dataset_self_contain/Prob028_up_and_down_counter_ref.sv b/Verilog Gen/dataset_self_contain/Prob028_up_and_down_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..3f4a2006e216d481bf02975a552c29d203eb8309 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob028_up_and_down_counter_ref.sv @@ -0,0 +1,44 @@ + +module RefModule( + input clk, + input rst_n, + input mode, + output reg [3:0]number, + output reg zero + ); + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + zero <= 1'd0; + end + else if (number == 4'd0) + begin + zero <= 1'b1; + end + else + begin + zero <= 1'b0; + end + + always @(posedge clk or negedge rst_n) + if (!rst_n) + begin + number <= 4'b0; + end + else if(mode) + begin + if(number == 9) + number <= 0; + else + number <= number + 1'd1; + end + else if(!mode) + begin + if(number == 0) + number <= 9; + else + number <= number - 1'd1; + end + else number <= number; +endmodule \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob028_up_and_down_counter_test.sv b/Verilog Gen/dataset_self_contain/Prob028_up_and_down_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..9c8d87a866b931fe81a5cb15a4f3ba531c1391a7 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob028_up_and_down_counter_test.sv @@ -0,0 +1,165 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic mode + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + mode = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test up counting (mode = 1) + mode <= 1; + repeat(15) @(posedge clk); // Count up: 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4 + + // Test down counting (mode = 0) + mode <= 0; + repeat(15) @(posedge clk); // Count down: 5,4,3,2,1,0,9,8,7,6,5,4,3,2,1 + + // Test mode switching + mode <= 1; @(posedge clk); // Count up + mode <= 0; @(posedge clk); // Count down + mode <= 1; @(posedge clk); // Count up + mode <= 0; @(posedge clk); // Count down + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + mode <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_number; + int errortime_number; + int errors_zero; + int errortime_zero; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic mode; + logic [3:0] number_ref; + logic zero_ref; + logic [3:0] number_dut; + logic zero_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, mode, number_ref, zero_ref, number_dut, zero_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .mode ); + + RefModule good1 ( + .clk, + .rst_n, + .mode, + .number(number_ref), + .zero(zero_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .mode, + .number(number_dut), + .zero(zero_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_number) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "number", stats1.errors_number, stats1.errortime_number); + else $display("Hint: Output '%s' has no mismatches.", "number"); + if (stats1.errors_zero) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "zero", stats1.errors_zero, stats1.errortime_zero); + else $display("Hint: Output '%s' has no mismatches.", "zero"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { number_ref, zero_ref } === ( { number_ref, zero_ref } ^ { number_dut, zero_dut } ^ { number_ref, zero_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (number_ref !== ( number_ref ^ number_dut ^ number_ref )) + begin if (stats1.errors_number == 0) stats1.errortime_number = $time; + stats1.errors_number = stats1.errors_number+1'b1; end + if (zero_ref !== ( zero_ref ^ zero_dut ^ zero_ref )) + begin if (stats1.errors_zero == 0) stats1.errortime_zero = $time; + stats1.errors_zero = stats1.errors_zero+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob030_simple_implementation_RAM_prompt.txt b/Verilog Gen/dataset_self_contain/Prob030_simple_implementation_RAM_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..b3177dece6291f662755db8ef509a877d7ac02d2 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob030_simple_implementation_RAM_prompt.txt @@ -0,0 +1,28 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input read_en + - input write_en + - input read_addr (8 bits) + - input write_addr (8 bits) + - input write_data (4 bits) + - output read_data (4 bits) + +Implement a dual-port RAM with depth of 8 and data width of 4 bits. All +data should be initialized to 0000. The RAM has two separate ports: one +for reading data and one for writing data. Read and write operations can +be performed simultaneously. + +When the read enable signal read_en is asserted, the module should read +data from the location specified by read_addr and output it on read_data. +When the write enable signal write_en is asserted, the module should write +write_data to the location specified by write_addr. + +The rst_n signal is an active-low asynchronous reset signal. Assume all +sequential logic is triggered on the positive edge of the clock. + +Clarification: The read is synchronous (registered). On the rising clock edge, if read_en is asserted, read_data is updated to the contents of read_addr (one-cycle read latency); if read_en is deasserted, read_data holds its previous value. The asynchronous reset clears read_data to 0. + diff --git a/Verilog Gen/dataset_self_contain/Prob030_simple_implementation_RAM_ref.sv b/Verilog Gen/dataset_self_contain/Prob030_simple_implementation_RAM_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..c979fc6d858b9b6871182cfcc04ae6454d16a591 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob030_simple_implementation_RAM_ref.sv @@ -0,0 +1,31 @@ + +module RefModule( + input clk, + input rst_n, + + input write_en, + input [7:0]write_addr, + input [3:0]write_data, + + input read_en, + input [7:0]read_addr, + output reg [3:0]read_data +); + reg [3:0] myRAM [7:0]; + + reg [8:0] i; + // integer i; // Not synthesizable + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + for(i=0;i<256;i=i+1) + myRAM[i] <= 0; + else + myRAM[write_addr] <= write_en? write_data: myRAM[write_addr]; + end + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + read_data <= 0; + else + read_data <= read_en? myRAM[read_addr]: read_data; + end +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob030_simple_implementation_RAM_test.sv b/Verilog Gen/dataset_self_contain/Prob030_simple_implementation_RAM_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..85356a44a0663e3a1f965a5127f30cb959278204 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob030_simple_implementation_RAM_test.sv @@ -0,0 +1,249 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic read_en, + output logic write_en, + output logic [7:0] read_addr, + output logic [7:0] write_addr, + output logic [3:0] write_data + // === End your code here === +); + // === Start your code here === + initial begin + rst_n <= 0; + read_en <= 0; + write_en <= 0; + read_addr <= 0; + write_addr <= 0; + write_data <= 0; + + // Reset sequence + @(posedge clk); + @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic write operations + write_en <= 1; + write_addr <= 0; + write_data <= 4'b1010; + @(posedge clk); + + write_addr <= 1; + write_data <= 4'b0101; + @(posedge clk); + + write_addr <= 2; + write_data <= 4'b1111; + @(posedge clk); + + write_en <= 0; + @(posedge clk); + + // Test basic read operations + read_en <= 1; + read_addr <= 0; + @(posedge clk); // 1010 + + read_addr <= 1; + @(posedge clk); // 0101 + + read_addr <= 2; + @(posedge clk); // 1111 + + read_en <= 0; + @(posedge clk); + + // Test simultaneous read and write + read_en <= 1; + write_en <= 1; + read_addr <= 0; + write_addr <= 3; + write_data <= 4'b1100; + @(posedge clk); + + read_addr <= 1; + write_addr <= 4; + write_data <= 4'b0011; + @(posedge clk); + + read_en <= 0; + write_en <= 0; + @(posedge clk); + + // Random test sequence + repeat(200) @(posedge clk) begin + read_en <= $random & 1; + write_en <= $random & 1; + read_addr <= $random & 7; // Only use addresses 0-7 for depth 8 + write_addr <= $random & 7; + write_data <= $random & 15; // 4-bit data + end + + // Additional checks: "The asynchronous reset clears read_data to 0." The original stimulus + // only resets at the start. Assert rst_n mid-run, between clock edges, while read_data is + // non-zero, hold it two cycles, release between edges. Memory contents after a mid-run reset + // are not stated, so every location is rewritten (read_en low) before reads resume. + for (int r = 0; r < 3; r++) begin + // make read_data non-zero: write a non-zero value, then read it + @(posedge clk) begin + read_en <= 0; write_en <= 1; write_addr <= r; write_data <= 4'h9 + r; + end + @(posedge clk) begin write_en <= 0; read_en <= 1; read_addr <= r; end + @(posedge clk) read_en <= 0; + @(negedge clk) rst_n <= 0; // asynchronous: read_data must clear before the next edge + repeat (2) @(posedge clk); + @(negedge clk) rst_n <= 1; + for (int a = 0; a < 8; a++) @(posedge clk) begin + write_en <= 1; write_addr <= a; write_data <= $random & 15; + end + @(posedge clk) write_en <= 0; + repeat (60) @(posedge clk) begin + read_en <= $random & 1; + write_en <= $random & 1; + read_addr <= $random & 7; + write_addr <= $random & 7; + write_data <= $random & 15; + end + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_read_data; + int errortime_read_data; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic read_en; + logic write_en; + logic [7:0] read_addr; + logic [7:0] write_addr; + logic [3:0] write_data; + logic [3:0] read_data_ref; + logic [3:0] read_data_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, read_en, write_en, read_addr, write_addr, write_data, read_data_ref, read_data_dut); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .read_en, + .write_en, + .read_addr, + .write_addr, + .write_data + ); + + RefModule good1 ( + .clk, + .rst_n, + .write_en, + .write_addr, + .write_data, + .read_en, + .read_addr, + .read_data(read_data_ref) + ); + + TopModule top_module1 ( + .clk, + .rst_n, + .read_en, + .write_en, + .read_addr, + .write_addr, + .write_data, + .read_data(read_data_dut) + ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_read_data) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "read_data", stats1.errors_read_data, stats1.errortime_read_data); + else $display("Hint: Output '%s' has no mismatches.", "read_data"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { read_data_ref } === ( { read_data_ref } ^ { read_data_dut } ^ { read_data_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (read_data_ref !== ( read_data_ref ^ read_data_dut ^ read_data_ref )) + begin if (stats1.errors_read_data == 0) stats1.errortime_read_data = $time; + stats1.errors_read_data = stats1.errors_read_data+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob031_johnson_counter_prompt.txt b/Verilog Gen/dataset_self_contain/Prob031_johnson_counter_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..55b01a8d238ddaa30a5726da49256413f7c9079d --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob031_johnson_counter_prompt.txt @@ -0,0 +1,19 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - output reg [3:0] Q + +The module should implement a 4-bit Johnson counter (also known as a twisted +ring counter). Assume all sequential logic is triggered on the positive edge +of the clock. The reset input is active low asynchronous, and should reset +the counter to 0000. + +The Johnson counter should work as follows: +- The counter follows a specific sequence of states in a circular pattern +- The state sequence is: 0000 -> 1000 -> 1100 -> 1110 -> 1111 -> 0111 -> 0011 -> 0001 -> 0000 +- This creates a cycle of 8 states before repeating +- On each positive clock edge, the counter advances to the next state in the sequence +- When rst_n is low, the counter should reset to 0000 diff --git a/Verilog Gen/dataset_self_contain/Prob031_johnson_counter_ref.sv b/Verilog Gen/dataset_self_contain/Prob031_johnson_counter_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..1f68a1ac386fe58d6d32038dd39d05c6c255c978 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob031_johnson_counter_ref.sv @@ -0,0 +1,12 @@ + +module RefModule( + input clk , + input rst_n, + + output reg [3:0] Q +); + always@(posedge clk or negedge rst_n)begin + if(!rst_n) Q <= 'd0; + else Q <= {~Q[0], Q[3 : 1]}; + end +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob031_johnson_counter_test.sv b/Verilog Gen/dataset_self_contain/Prob031_johnson_counter_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..dc33439851b599a6017ec24dc89ff6eab20774ae --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob031_johnson_counter_test.sv @@ -0,0 +1,133 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test Johnson counter for multiple cycles + // The counter cycles through 8 states: 0000->1000->1100->1110->1111->0111->0011->0001->0000 + repeat(200) @(posedge clk); + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_Q; + int errortime_Q; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [3:0] Q_ref; + logic [3:0] Q_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, Q_ref, Q_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n ); + + RefModule good1 ( + .clk, + .rst_n, + .Q(Q_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .Q(Q_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_Q) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "Q", stats1.errors_Q, stats1.errortime_Q); + else $display("Hint: Output '%s' has no mismatches.", "Q"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { Q_ref } === ( { Q_ref } ^ { Q_dut } ^ { Q_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (Q_ref !== ( Q_ref ^ Q_dut ^ Q_ref )) + begin if (stats1.errors_Q == 0) stats1.errortime_Q = $time; + stats1.errors_Q = stats1.errors_Q+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob032_pipeline_multiplier_prompt.txt b/Verilog Gen/dataset_self_contain/Prob032_pipeline_multiplier_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..c4c66eed1e63b44f7b3b139d178013bd8ca212fe --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob032_pipeline_multiplier_prompt.txt @@ -0,0 +1,24 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input [3:0] mul_a + - input [3:0] mul_b + - output reg [7:0] mul_out + +The module should implement a 4-bit unsigned pipelined multiplier. Assume all +sequential logic is triggered on the positive edge of the clock. The reset +input is active low asynchronous, and should reset the multiplier to 0. + +The pipelined multiplier should work as follows: +- The multiplier performs unsigned multiplication of two 4-bit inputs (mul_a and mul_b) +- The result is an 8-bit output (mul_out) representing the product +- The multiplication is implemented using a pipelined architecture for improved + performance and throughput +- The pipeline allows new inputs to be processed every clock cycle while + previous multiplications are still being completed +- When rst_n is low, the multiplier should reset to 0 + +Clarification: The pipeline has a fixed latency of exactly 2 clock cycles from input to mul_out: stage 1 registers the partial-product/partial-sum terms of the current inputs, and stage 2 registers their final sum into mul_out, so a result for inputs applied on a given cycle appears two cycles later. diff --git a/Verilog Gen/dataset_self_contain/Prob032_pipeline_multiplier_ref.sv b/Verilog Gen/dataset_self_contain/Prob032_pipeline_multiplier_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..4686788d7af68d2fe694cf911d4d8452976defa0 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob032_pipeline_multiplier_ref.sv @@ -0,0 +1,42 @@ + +module RefModule#( + parameter size = 4 +)( + input clk , + input rst_n , + input [size-1:0] mul_a , + input [size-1:0] mul_b , + + output reg [size*2-1:0] mul_out +); + //parameter + parameter N = size * 2; + //defination + wire [N - 1 : 0] temp [0 : 3]; + + reg [N - 1 : 0] adder_0; + reg [N - 1 : 0] adder_1; + + //output + genvar i; + generate + for(i = 0; i < 4; i = i + 1)begin : loop + assign temp[i] = mul_b[i] ? mul_a << i : 'd0; + end + endgenerate + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_0 <= 'd0; + else adder_0 <= temp[0] + temp[1]; + end + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) adder_1 <= 'd0; + else adder_1 <= temp[2] + temp[3]; + end + + always@(posedge clk or negedge rst_n)begin + if(!rst_n) mul_out <= 'd0; + else mul_out <= adder_0 + adder_1; + end +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob032_pipeline_multiplier_test.sv b/Verilog Gen/dataset_self_contain/Prob032_pipeline_multiplier_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..1dce2b7fad20e13c6d4e6b7019b17061acb1401f --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob032_pipeline_multiplier_test.sv @@ -0,0 +1,156 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [3:0] mul_a, + output logic [3:0] mul_b + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + mul_a = 0; + mul_b = 0; + repeat(10) @(posedge clk); + rst_n <= 1; + @(posedge clk); + + // Test basic multiplication cases + mul_a <= 4'd2; mul_b <= 4'd3; @(posedge clk); // 2 * 3 = 6 + mul_a <= 4'd4; mul_b <= 4'd5; @(posedge clk); // 4 * 5 = 20 + mul_a <= 4'd7; mul_b <= 4'd8; @(posedge clk); // 7 * 8 = 56 + mul_a <= 4'd9; mul_b <= 4'd10; @(posedge clk); // 9 * 10 = 90 + mul_a <= 4'd12; mul_b <= 4'd13; @(posedge clk); // 12 * 13 = 156 + mul_a <= 4'd15; mul_b <= 4'd15; @(posedge clk); // 15 * 15 = 225 + + // Test edge cases + mul_a <= 4'd0; mul_b <= 4'd5; @(posedge clk); // 0 * 5 = 0 + mul_a <= 4'd3; mul_b <= 4'd0; @(posedge clk); // 3 * 0 = 0 + mul_a <= 4'd1; mul_b <= 4'd1; @(posedge clk); // 1 * 1 = 1 + mul_a <= 4'd15; mul_b <= 4'd1; @(posedge clk); // 15 * 1 = 15 + mul_a <= 4'd1; mul_b <= 4'd15; @(posedge clk); // 1 * 15 = 15 + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_mul_out; + int errortime_mul_out; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [3:0] mul_a; + logic [3:0] mul_b; + logic [7:0] mul_out_ref; + logic [7:0] mul_out_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, mul_a, mul_b, mul_out_ref, mul_out_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .mul_a, + .mul_b ); + + RefModule good1 ( + .clk, + .rst_n, + .mul_a, + .mul_b, + .mul_out(mul_out_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .mul_a, + .mul_b, + .mul_out(mul_out_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_mul_out) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "mul_out", stats1.errors_mul_out, stats1.errortime_mul_out); + else $display("Hint: Output '%s' has no mismatches.", "mul_out"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { mul_out_ref } === ( { mul_out_ref } ^ { mul_out_dut } ^ { mul_out_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (mul_out_ref !== ( mul_out_ref ^ mul_out_dut ^ mul_out_ref )) + begin if (stats1.errors_mul_out == 0) stats1.errortime_mul_out = $time; + stats1.errors_mul_out = stats1.errors_mul_out+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob033_traffic_lights_prompt.txt b/Verilog Gen/dataset_self_contain/Prob033_traffic_lights_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..e2ebc3c9facb588d08141818c07c90f8d3bfe0b4 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob033_traffic_lights_prompt.txt @@ -0,0 +1,42 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input pass_request + - output reg [7:0] clock + - output reg red + - output reg yellow + - output reg green + +The module should implement a traffic light controller with pedestrian +request functionality. Assume all sequential logic is triggered on the +positive edge of the clock. The reset input is active low asynchronous. +While rst_n is low all lights are off and clock = 10; after release the +lights stay off for two clock edges (clock 9, 8) and the third edge turns +red on with clock = 10. + +The traffic light controller should work as follows: +- Normal operation: Traffic light cycles through red (10 clock cycles), + green (60 clock cycles), and yellow (5 clock cycles) in sequence +- Pedestrian request: When pass_request is active, if remaining green time + is greater than 10 clock cycles, it should be shortened to 10 clock + cycles; if remaining green time is 10 clock cycles or less, it remains + unchanged +- Light sequence: Red → Green → Yellow → Red (repeating) +- Clock counter: The clock output shows the countdown timer for the current + light state +- Light outputs: Only one light should be active at a time (red, yellow, + or green) + +Traffic light operation: +- Red light: 10 clock cycles, clock counts down from 10 to 1 +- Green light: 60 clock cycles normally, or 10 clock cycles if pedestrian + request is active and remaining time > 10 +- Yellow light: 5 clock cycles, clock counts down from 5 to 1 +- Pedestrian request: Shortens green time to 10 cycles if remaining time > 10 +- Reset behavior: Active low reset to all lights off with clock = 10; red + starts at the third clock edge after release + +Clarification: The light outputs are double-registered relative to the counter: the state transition is evaluated when the countdown counter cnt equals 3 (not 1), the light-enable flags register on the next cycle, and the red/yellow/green outputs register one cycle after that, so each light change lags the counter reaching its terminal value by two clock cycles. The countdown reloads to 10 (red), 60 (green), or 5 (yellow) and decrements by 1 each cycle. diff --git a/Verilog Gen/dataset_self_contain/Prob033_traffic_lights_ref.sv b/Verilog Gen/dataset_self_contain/Prob033_traffic_lights_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..9515ad7829b4dd43e3ea700d3343f8d4dbc9fb0f --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob033_traffic_lights_ref.sv @@ -0,0 +1,108 @@ +`timescale 1ns/1ns + +module RefModule + ( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input pass_request, + output wire[7:0]clock, + output reg red, + output reg yellow, + output reg green + ); + + parameter idle = 2'd0, + s1_red = 2'd1, + s2_green = 2'd2, + s3_yellow = 2'd3; + reg [7:0] cnt; + reg [1:0] state; + reg p_red,p_yellow,p_green; //Used to cache the previous value of the traffic lights for rising edge detection + + +always @(posedge clk or negedge rst_n) + begin + if(!rst_n) + begin + state <= idle; + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + end + else case(state) + idle: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b0; + state <= s1_red; + end + s1_red: + begin + p_red <= 1'b1; + p_green <= 1'b0; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s2_green; + else + state <= s1_red; + end + s2_green: + begin + p_red <= 1'b0; + p_green <= 1'b1; + p_yellow <= 1'b0; + if (cnt == 3) + state <= s3_yellow; + else + state <= s2_green; + end + s3_yellow: + begin + p_red <= 1'b0; + p_green <= 1'b0; + p_yellow <= 1'b1; + if (cnt == 3) + state <= s1_red; + else + state <= s3_yellow; + end + endcase + end + +always @(posedge clk or negedge rst_n) + if(!rst_n) + cnt <= 7'd10; + else if (pass_request&&green&&(cnt>10)) + cnt <= 7'd10; + else if (!green&&p_green) + cnt <= 7'd60; + else if (!yellow&&p_yellow) + cnt <= 7'd5; + else if (!red&&p_red) + cnt <= 7'd10; + else cnt <= cnt -1; + assign clock = cnt; + +always @(posedge clk or negedge rst_n) + if(!rst_n) + begin + yellow <= 1'd0; + red <= 1'd0; + green <= 1'd0; + end + else + begin + yellow <= p_yellow; + red <= p_red; + green <= p_green; + end + +endmodule + + +// notice: the why we set if (cnt == 3) for state transition, because at cnt 3, the next cycle +// cnt 2 the state will be updated +// cnt 1 the p_yellow, p_red, p_green will be updated +// cnt 0 the yellow, red, green will be updated +// next cycle, the traffic light will change \ No newline at end of file diff --git a/Verilog Gen/dataset_self_contain/Prob033_traffic_lights_test.sv b/Verilog Gen/dataset_self_contain/Prob033_traffic_lights_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..b3b7eda5538208585526f69ada1c07a7459e47e2 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob033_traffic_lights_test.sv @@ -0,0 +1,195 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic pass_request + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + pass_request = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test normal traffic light cycle + pass_request = 0; + repeat(100) @(posedge clk); // Let it cycle through red->green->yellow->red + + // Test pedestrian request during green phase + pass_request = 1; + repeat(20) @(posedge clk); // Request during green + pass_request = 0; + @(posedge clk); + + // Test pedestrian request during red phase (should not affect) + pass_request = 1; + repeat(15) @(posedge clk); // Request during red + pass_request = 0; + @(posedge clk); + + // Test pedestrian request during yellow phase (should not affect) + pass_request = 1; + repeat(10) @(posedge clk); // Request during yellow + pass_request = 0; + @(posedge clk); + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + pass_request <= $random; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_clock; + int errortime_clock; + int errors_red; + int errortime_red; + int errors_yellow; + int errortime_yellow; + int errors_green; + int errortime_green; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic pass_request; + logic [7:0] clock_ref; + logic red_ref; + logic yellow_ref; + logic green_ref; + logic [7:0] clock_dut; + logic red_dut; + logic yellow_dut; + logic green_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, pass_request, clock_ref, red_ref, yellow_ref, green_ref, clock_dut, red_dut, yellow_dut, green_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .pass_request ); + + RefModule good1 ( + .clk, + .rst_n, + .pass_request, + .clock(clock_ref), + .red(red_ref), + .yellow(yellow_ref), + .green(green_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .pass_request, + .clock(clock_dut), + .red(red_dut), + .yellow(yellow_dut), + .green(green_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_clock) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "clock", stats1.errors_clock, stats1.errortime_clock); + else $display("Hint: Output '%s' has no mismatches.", "clock"); + if (stats1.errors_red) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "red", stats1.errors_red, stats1.errortime_red); + else $display("Hint: Output '%s' has no mismatches.", "red"); + if (stats1.errors_yellow) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "yellow", stats1.errors_yellow, stats1.errortime_yellow); + else $display("Hint: Output '%s' has no mismatches.", "yellow"); + if (stats1.errors_green) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "green", stats1.errors_green, stats1.errortime_green); + else $display("Hint: Output '%s' has no mismatches.", "green"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { clock_ref, red_ref, yellow_ref, green_ref } === ( { clock_ref, red_ref, yellow_ref, green_ref } ^ { clock_dut, red_dut, yellow_dut, green_dut } ^ { clock_ref, red_ref, yellow_ref, green_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (clock_ref !== ( clock_ref ^ clock_dut ^ clock_ref )) + begin if (stats1.errors_clock == 0) stats1.errortime_clock = $time; + stats1.errors_clock = stats1.errors_clock+1'b1; end + if (red_ref !== ( red_ref ^ red_dut ^ red_ref )) + begin if (stats1.errors_red == 0) stats1.errortime_red = $time; + stats1.errors_red = stats1.errors_red+1'b1; end + if (yellow_ref !== ( yellow_ref ^ yellow_dut ^ yellow_ref )) + begin if (stats1.errors_yellow == 0) stats1.errortime_yellow = $time; + stats1.errors_yellow = stats1.errors_yellow+1'b1; end + if (green_ref !== ( green_ref ^ green_dut ^ green_ref )) + begin if (stats1.errors_green == 0) stats1.errortime_green = $time; + stats1.errors_green = stats1.errors_green+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob034_gaming_machine_billing_program_prompt.txt b/Verilog Gen/dataset_self_contain/Prob034_gaming_machine_billing_program_prompt.txt new file mode 100644 index 0000000000000000000000000000000000000000..3c14f39d866eb7e7b41966c9337655db07be8669 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob034_gaming_machine_billing_program_prompt.txt @@ -0,0 +1,42 @@ +I would like you to implement a module named TopModule with the following +interface. All input and output ports are one bit unless otherwise +specified. + + - input clk + - input rst_n + - input [9:0] money + - input set + - input boost + - output reg [9:0] remain + - output reg yellow + - output reg red + +The module should implement a gaming machine billing system with +prepaid charging functionality. Assume all sequential logic is triggered +on the positive edge of the clock. The reset input is active low +asynchronous, and should reset the system to initial state. + +The gaming machine billing system should work as follows: +- Prepaid charging: When set signal is active, the money value is added + to the remaining balance (remain) +- Game modes: Normal mode (1 yuan per minute) and boost mode (2 yuan per + minute) +- Default mode: Normal mode (boost = 0) +- Boost mode: Activated when boost = 1, charges 2 yuan per clock cycle +- Balance deduction: Each clock cycle deducts 1 yuan (normal) or 2 yuan + (boost) from remain; if remain is less than that amount, remain holds + its value +- Warning indicators: Yellow light when remain > 0 and < 10, red light + when remain < 1 in normal mode, when remain < 2 in boost mode. +- Continuous charging: Users can recharge during gameplay via set signal + +Gaming machine operation: +- Normal mode: remain decreases by 1 each clock cycle +- Boost mode: remain decreases by 2 each clock cycle +- Charging: When set = 1, money value is added to remain +- Yellow warning: Active when 0 < remain < 10 +- Red warning: Active when remain < 1 for normal mode, remaon < 2 for boost mode + (insufficient funds) +- Reset behavior: Active low reset to initial state + +Clarification: When set=1, money is added and NO deduction occurs that cycle (charging and deduction are mutually exclusive per cycle; charging takes priority). The yellow and red warning outputs are registered - they update on the clock edge and reflect the previous cycle's remaining balance, not combinational. diff --git a/Verilog Gen/dataset_self_contain/Prob034_gaming_machine_billing_program_ref.sv b/Verilog Gen/dataset_self_contain/Prob034_gaming_machine_billing_program_ref.sv new file mode 100644 index 0000000000000000000000000000000000000000..84d31ece4f078c42fce9b0073d9ff77540935471 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob034_gaming_machine_billing_program_ref.sv @@ -0,0 +1,36 @@ + +module RefModule +( + input rst_n, //Asynchronous reset signal, active low + input clk, //Clock signal + input [9:0]money, + input set, + input boost, + output reg[9:0]remain, + output reg yellow, + output reg red +); + always@(posedge clk or negedge rst_n) begin + if(~rst_n) begin + yellow <= 0; + red <= 0; + end + else begin + yellow <= remain<10&&remain; + red <= boost? remain<2: remain<1; + end + end + + always@(posedge clk or negedge rst_n) begin + if(~rst_n) + remain <= 0; + else if(boost) + remain <= set ? remain+money: + remain<2? remain: + remain-2; + else + remain <= set ? remain+money: + remain<1? remain: + remain-1; + end +endmodule diff --git a/Verilog Gen/dataset_self_contain/Prob034_gaming_machine_billing_program_test.sv b/Verilog Gen/dataset_self_contain/Prob034_gaming_machine_billing_program_test.sv new file mode 100644 index 0000000000000000000000000000000000000000..e447ea3b14ac0f37fb8bc116b201da5c61489f87 --- /dev/null +++ b/Verilog Gen/dataset_self_contain/Prob034_gaming_machine_billing_program_test.sv @@ -0,0 +1,208 @@ +`timescale 1 ps/1 ps +`define OK 12 +`define INCORRECT 13 + + +module stimulus_gen ( + input clk, + // === Start your code here === + output logic rst_n, + output logic [9:0] money, + output logic set, + output logic boost + // === End your code here === +); + // === Start your code here === + initial begin + rst_n = 0; + money = 0; + set = 0; + boost = 0; + repeat(10) @(posedge clk); + rst_n = 1; + @(posedge clk); + + // Test normal mode charging + set = 1; + money = 10'd50; @(posedge clk); // Charge 50 yuan + set = 0; + @(posedge clk); + + // Test normal mode consumption (1 yuan per cycle) + boost = 0; + repeat(20) @(posedge clk); // Consume 20 yuan + + // Test charging during gameplay + boost = 0; + set = 1; + money = 10'd20; @(posedge clk); // Charge while playing + set = 0; + @(posedge clk); + + boost = 1; + repeat(25) @(posedge clk); // Consume 50 yuan + + // Test insufficient funds in normal mode + boost = 0; + repeat(10) @(posedge clk); // Try to consume more than available + + // Test insufficient funds in boost mode + set = 1; + money = 10'd3; @(posedge clk); // Charge small amount + set = 0; + @(posedge clk); + boost = 1; + repeat(5) @(posedge clk); // Try to consume in boost mode + + // Random testing + repeat(200) @(posedge clk, negedge clk) begin + set <= $random; + boost <= $random; + money <= $random % 1024; + end + + #1 $finish; + end + // === End your code here === + +endmodule + +module tb(); + + // === Start your code here === + typedef struct packed { + int errors; + int errortime; + int errors_remain; + int errortime_remain; + int errors_yellow; + int errortime_yellow; + int errors_red; + int errortime_red; + + int clocks; + } stats; + // === End your code here === + + stats stats1; + + + wire[511:0] wavedrom_title; + wire wavedrom_enable; + int wavedrom_hide_after_time; + + reg clk=0; + initial forever + #5 clk = ~clk; + + // === Start your code here === + logic rst_n; + logic [9:0] money; + logic set; + logic boost; + logic [9:0] remain_ref; + logic yellow_ref; + logic red_ref; + logic [9:0] remain_dut; + logic yellow_dut; + logic red_dut; + // === End your code here === + + initial begin + $dumpfile("wave.vcd"); + // === Start your code here === + $dumpvars(1, stim1.clk, tb_mismatch, clk, rst_n, money, set, boost, remain_ref, yellow_ref, red_ref, remain_dut, yellow_dut, red_dut ); + // === End your code here === + end + + + wire tb_match; // Verification + wire tb_mismatch = ~tb_match; + + // === Start your code here === + stimulus_gen stim1 ( + .clk, + .rst_n, + .money, + .set, + .boost ); + + RefModule good1 ( + .clk, + .rst_n, + .money, + .set, + .boost, + .remain(remain_ref), + .yellow(yellow_ref), + .red(red_ref) ); + + TopModule top_module1 ( + .clk, + .rst_n, + .money, + .set, + .boost, + .remain(remain_dut), + .yellow(yellow_dut), + .red(red_dut) ); + // === End your code here === + + + bit strobe = 0; + task wait_for_end_of_timestep; + repeat(5) begin + strobe <= !strobe; // Try to delay until the very end of the time step. + @(strobe); + end + endtask + + + final begin + // === Start your code here === + if (stats1.errors_remain) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "remain", stats1.errors_remain, stats1.errortime_remain); + else $display("Hint: Output '%s' has no mismatches.", "remain"); + if (stats1.errors_yellow) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "yellow", stats1.errors_yellow, stats1.errortime_yellow); + else $display("Hint: Output '%s' has no mismatches.", "yellow"); + if (stats1.errors_red) $display("Hint: Output '%s' has %0d mismatches. First mismatch occurred at time %0d.", "red", stats1.errors_red, stats1.errortime_red); + else $display("Hint: Output '%s' has no mismatches.", "red"); + // === End your code here === + + $display("Hint: Total mismatched samples is %1d out of %1d samples\n", stats1.errors, stats1.clocks); + $display("Simulation finished at %0d ps", $time); + $display("Mismatches: %1d in %1d samples", stats1.errors, stats1.clocks); + end + + // Verification: XORs on the right makes any X in good_vector match anything, but X in dut_vector will only match X. + assign tb_match = ( { remain_ref, yellow_ref, red_ref } === ( { remain_ref, yellow_ref, red_ref } ^ { remain_dut, yellow_dut, red_dut } ^ { remain_ref, yellow_ref, red_ref } ) ); + // Use explicit sensitivity list here. @(*) causes NetProc::nex_input() to be called when trying to compute + // the sensitivity list of the @(strobe) process, which isn't implemented. + always @(posedge clk, negedge clk) begin + + stats1.clocks++; + if (!tb_match) begin + if (stats1.errors == 0) stats1.errortime = $time; + stats1.errors++; + end + // === Start your code here === + if (remain_ref !== ( remain_ref ^ remain_dut ^ remain_ref )) + begin if (stats1.errors_remain == 0) stats1.errortime_remain = $time; + stats1.errors_remain = stats1.errors_remain+1'b1; end + if (yellow_ref !== ( yellow_ref ^ yellow_dut ^ yellow_ref )) + begin if (stats1.errors_yellow == 0) stats1.errortime_yellow = $time; + stats1.errors_yellow = stats1.errors_yellow+1'b1; end + if (red_ref !== ( red_ref ^ red_dut ^ red_ref )) + begin if (stats1.errors_red == 0) stats1.errortime_red = $time; + stats1.errors_red = stats1.errors_red+1'b1; end + // === End your code here === + + end + + // add timeout after 100K cycles + initial begin + #1000000 + $display("TIMEOUT"); + $finish(); + end + +endmodule