Advertisement
nelson33

Untitled

Sep 23rd, 2023
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VeriLog 5.19 KB | Source Code | 0 0
  1. module Encoder (
  2.     input clk,
  3.     input rst_n,
  4.     input [3:0] max,
  5.     input [3:0] min,
  6.     input in_valid,
  7.     input [1:0] mode,
  8.     input [7:0] in_data,
  9.     output reg [11:0] out_data,
  10.     output reg [2:0] state,
  11.     output wire [3:0] counter_out, /* use wire to connect two module*/
  12.     output wire direction,
  13.     //output reg [7:0] e_value,
  14.     output reg [3:0] next_offset_cnt_value
  15.     //output wire [7:0] output_tmp_value,
  16.     //output wire [7:0] next_output_tmp_value
  17. );
  18.  
  19. reg [2:0] next_state;
  20. reg [3:0] offset_cnt;
  21. reg [3:0] next_offset_cnt;
  22. reg [7:0] output_tmp [7:0], next_output_tmp [7:0];
  23. reg [7:0] e;
  24. reg r1, r2, r3, r4;
  25. reg [11:0] encrypted_data[0:7];
  26. reg [2:0] pre_state;
  27. reg [7:0] in_data_temp;
  28.  
  29. parameter INIT = 3'd0;
  30. parameter GET_DATA = 3'd1;
  31. parameter ENCRYPT_DATA = 3'd2;
  32. parameter OUTPUT_DATA = 3'd3;
  33.  
  34. /* using mode signal to process the flip and enable signal */
  35. wire flip, enable;
  36.  
  37. /* fill in the following blanks (e.g.: a = (b == 2'b01) ? 1'b1 : 1'b0) */
  38. assign flip = (mode == 2'b00)? 0: 1;
  39. assign enable = (mode == 2'b10)? 0: 1;
  40.  
  41. /* instantiate the Parameterized_Ping_Pong_Counter module */
  42. Parameterized_Ping_Pong_Counter pppc(
  43.     .clk(clk),
  44.     .rst_n(rst_n),
  45.     .enable(enable),
  46.     .max(max),
  47.     .min(min),
  48.     .flip(flip),
  49.     .direction(direction),
  50.     .out(counter_out)
  51. );
  52.  
  53. /* state transition */
  54. always@(posedge clk)begin
  55.     if(!rst_n) begin
  56.         state <= INIT;
  57.     end
  58.     else begin
  59.         state <= next_state;
  60.     end
  61. end
  62. always@(*) begin
  63.     case(state)
  64.     INIT: begin
  65.         pre_state = INIT;
  66.         if(in_valid) next_state = GET_DATA;
  67.         else next_state = INIT;
  68.     end
  69.     GET_DATA: begin
  70.         /* determine the next state */
  71.         pre_state = GET_DATA;
  72.         if(!in_valid && mode == 2'b10) next_state = ENCRYPT_DATA;
  73.         else next_state = GET_DATA;
  74.     end
  75.     ENCRYPT_DATA: begin
  76.         pre_state = ENCRYPT_DATA;
  77.         if(offset_cnt == 4'd7) next_state = OUTPUT_DATA;
  78.         else next_state = ENCRYPT_DATA;
  79.     end
  80.     OUTPUT_DATA: begin
  81.         pre_state = OUTPUT_DATA;
  82.         /* determine the next state */
  83.         if(offset_cnt == 4'd7) next_state = INIT;
  84.         else next_state = OUTPUT_DATA;
  85.     end
  86.     default: begin
  87.         next_state = state;
  88.     end
  89.     endcase
  90. end
  91. /* counter (this is the offset_cnt in the Practice_2) */
  92. always@(posedge clk) begin
  93.     if (!rst_n) begin
  94.         offset_cnt <= 4'd0;
  95.     end
  96.     else begin
  97.         offset_cnt <= next_offset_cnt;
  98.     end
  99. end
  100. //add
  101. /* determine the next_offset_cnt value base on the current state */
  102. always@(*) begin
  103.     case(state)
  104.     INIT: begin
  105.         next_offset_cnt = offset_cnt;
  106.         out_data = 0;
  107.     end
  108.     GET_DATA: begin
  109.         if (next_state == GET_DATA) begin
  110.             next_offset_cnt = offset_cnt + 1;
  111.         end
  112.         else begin
  113.             next_offset_cnt = 0;
  114.         end
  115.     end
  116.     ENCRYPT_DATA: begin
  117.         if (next_state == ENCRYPT_DATA) begin
  118.             next_offset_cnt = offset_cnt + 1;
  119.         end
  120.         else begin
  121.             next_offset_cnt = 0;
  122.         end
  123.     end
  124.     OUTPUT_DATA: begin
  125.         if (next_state == OUTPUT_DATA) begin
  126.             next_offset_cnt = offset_cnt + 1;
  127.         end
  128.         else begin
  129.             next_offset_cnt = 0;
  130.         end
  131.     end
  132.     default: begin
  133.         next_offset_cnt = offset_cnt;
  134.     end
  135.     endcase
  136. end
  137.  
  138. /* data processing  */
  139. //add
  140. /* output_tmp */
  141. always@(posedge clk) begin
  142.     if(!rst_n) begin
  143.         output_tmp[0] <= 8'b0;
  144.     end
  145.     else begin
  146.         output_tmp[next_offset_cnt] <= next_output_tmp[next_offset_cnt];
  147.     end
  148. end
  149. //add
  150. /* determine the next_output_tmp value base on the current state */
  151. /* You can store the in_data in the next_output_tmp (by using the value of offset_cnt reg)
  152.     and then process these data in the PROCESS_DATA state */
  153. always@(*) begin
  154.     case(state)
  155.     INIT:begin
  156.         in_data_temp = in_data;
  157.     end
  158.     GET_DATA: begin
  159.         next_output_tmp[0] = in_data_temp;
  160.         if (next_offset_cnt != 0) begin
  161.         next_output_tmp[next_offset_cnt] = in_data;
  162.         end
  163.     end
  164.     ENCRYPT_DATA: begin
  165.             e = (output_tmp[next_offset_cnt] + counter_out) % 256;
  166.             r1 = e[0] ^ e[1] ^ e[3] ^ e[4] ^ e[6];
  167.             r2 = e[0] ^ e[2] ^ e[3] ^ e[5] ^ e[6];
  168.             r3 = e[1] ^ e[2] ^ e[3] ^ e[7];
  169.             r4 = e[4] ^ e[5] ^ e[6] ^ e[7];
  170.             encrypted_data[next_offset_cnt] = {e[7], e[6], e[5], e[4], r4, e[3], e[2], e[1], r3, e[0], r2, r1};
  171.     end
  172.     default: begin
  173.         next_output_tmp[next_offset_cnt] = output_tmp[next_offset_cnt];
  174.     end
  175.     endcase
  176. end
  177.  
  178. /* output data */
  179. always @(posedge clk) begin
  180.     if (!rst_n) begin
  181.         out_data <= 12'b0;
  182.     end
  183.     else begin
  184.         /* determine the value of out_data under different circumstances */
  185.         if (next_state == OUTPUT_DATA)  begin
  186.             out_data = encrypted_data[next_offset_cnt];
  187.             //next_offset_cnt_value = next_offset_cnt;
  188.         end
  189.     end
  190. end
  191.  
  192. /*always @(negedge clk) begin
  193.     assign output_tmp_value = output_tmp[offset_cnt];
  194.     assign next_output_tmp_value = next_output_tmp[offset_cnt];
  195. end*/
  196. endmodule
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement