Advertisement
regzarr

LSIC - all labs so far in Verilog

Nov 25th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // FrequencyDivider
  2. module FrequencyDivider(
  3.     input clk_in,
  4.     input rst,
  5.     output reg clk_out
  6. );
  7.  
  8.     reg [3:0] counter;
  9.  
  10.     always @(posedge clk_in or posedge rst) begin
  11.         if (rst) begin
  12.             counter <= 0;
  13.             clk_out <= 0;
  14.         end else begin
  15.             if (counter == 9) begin
  16.                 counter <= 0;
  17.                 clk_out <= ~clk_out;
  18.             end else begin
  19.                 counter <= counter + 1;
  20.             end
  21.         end
  22.     end
  23.  
  24. endmodule
  25.  
  26. // TimeMeasurement
  27. module TimeMeasurement(
  28.     input clk,
  29.     input rst,
  30.     input [1:0] opcode,
  31.     output [31:0] ms_count
  32. );
  33.  
  34.     reg [22:0] clk_counter_reg, clk_counter_nxt;
  35.     reg [31:0] ms_count_reg, ms_count_nxt;
  36.     reg running;
  37.  
  38.     always @(posedge clk or posedge rst) begin
  39.         if (rst) begin
  40.             clk_counter_reg <= 0;
  41.             ms_count_reg <= 0;
  42.         end else begin
  43.             clk_counter_reg <= clk_counter_nxt;
  44.             ms_count_reg <= ms_count_nxt;
  45.         end
  46.     end
  47.  
  48.     always @(*) begin
  49.         clk_counter_nxt = clk_counter_reg;
  50.         ms_count_nxt = ms_count_reg;
  51.  
  52.         if (running) begin
  53.             if (clk_counter_reg == 49999) begin // Verilog constant syntax unchanged
  54.                 clk_counter_nxt = 0;
  55.                 ms_count_nxt = ms_count_reg + 1;
  56.             end else begin
  57.                 clk_counter_nxt = clk_counter_reg + 1;
  58.             end
  59.         end
  60.  
  61.         case (opcode)
  62.             2'b01: running = 1;   // Start
  63.             2'b11: running = 0;   // Pause
  64.             2'b10: begin          // Stop
  65.                 running = 0;
  66.                 ms_count_nxt = 0;
  67.             end
  68.             default: running = 0; // Default state
  69.         endcase
  70.     end
  71.  
  72.     assign ms_count = ms_count_reg;
  73.  
  74. endmodule
  75.  
  76. // SevenSegmentController
  77. module SevenSegmentController(
  78.     input [31:0] ms_count,
  79.     output reg [6:0] hex0, hex1, hex2, hex3, hex4, hex5
  80. );
  81.  
  82.     reg [3:0] digit[5:0];
  83.  
  84.     // Function definition changed for Verilog
  85.     function [6:0] encode;
  86.         input [3:0] value;
  87.         case (value)
  88.             4'd0: encode = 7'b1000000;
  89.             4'd1: encode = 7'b1111001;
  90.             4'd2: encode = 7'b0100100;
  91.             4'd3: encode = 7'b0110000;
  92.             4'd4: encode = 7'b0011001;
  93.             4'd5: encode = 7'b0010010;
  94.             4'd6: encode = 7'b0000010;
  95.             4'd7: encode = 7'b1111000;
  96.             4'd8: encode = 7'b0000000;
  97.             4'd9: encode = 7'b0010000;
  98.             default: encode = 7'b1111111;
  99.         endcase
  100.     endfunction
  101.  
  102.     always @(*) begin
  103.         digit[0] = ms_count % 10;
  104.         digit[1] = (ms_count / 10) % 10;
  105.         digit[2] = (ms_count / 100) % 10;
  106.         digit[3] = (ms_count / 1000) % 10;
  107.         digit[4] = (ms_count / 10000) % 10;
  108.         digit[5] = (ms_count / 100000) % 10;
  109.  
  110.         hex0 = encode(digit[0]);
  111.         hex1 = encode(digit[1]);
  112.         hex2 = encode(digit[2]);
  113.         hex3 = encode(digit[3]);
  114.         hex4 = encode(digit[4]);
  115.         hex5 = encode(digit[5]);
  116.     end
  117.  
  118. endmodule
  119.  
  120. // MainSystem
  121. module MainSystem(
  122.     input rst,
  123.     input clk,
  124.     input switch_pause,
  125.     input btn_start,
  126.     output [6:0] hex0, hex1, hex2, hex3, hex4, hex5
  127. );
  128.  
  129.     wire clk_2_5MHz;
  130.     wire [31:0] ms_count;
  131.     reg [1:0] opcode;
  132.  
  133.     FrequencyDivider freq_div (
  134.         .clk_in(clk),
  135.         .rst(rst),
  136.         .clk_out(clk_2_5MHz)
  137.     );
  138.  
  139.     TimeMeasurement timer (
  140.         .clk(clk_2_5MHz),
  141.         .rst(rst),
  142.         .opcode(opcode),
  143.         .ms_count(ms_count)
  144.     );
  145.  
  146.     SevenSegmentController display (
  147.         .ms_count(ms_count),
  148.         .hex0(hex0),
  149.         .hex1(hex1),
  150.         .hex2(hex2),
  151.         .hex3(hex3),
  152.         .hex4(hex4),
  153.         .hex5(hex5)
  154.     );
  155.  
  156.     always @(*) begin
  157.         opcode = 2'b00;
  158.         if (btn_start) opcode = 2'b01;
  159.         else if (switch_pause) opcode = 2'b11;
  160.     end
  161.  
  162. endmodule
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement