Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Encoder (
- input clk,
- input rst_n,
- input [3:0] max,
- input [3:0] min,
- input in_valid,
- input [1:0] mode,
- input [7:0] in_data,
- output reg [11:0] out_data,
- output reg [2:0] state,
- output wire [3:0] counter_out, /* use wire to connect two module*/
- output wire direction,
- //output reg [7:0] e_value,
- output reg [3:0] next_offset_cnt_value
- //output wire [7:0] output_tmp_value,
- //output wire [7:0] next_output_tmp_value
- );
- reg [2:0] next_state;
- reg [3:0] offset_cnt;
- reg [3:0] next_offset_cnt;
- reg [7:0] output_tmp [7:0], next_output_tmp [7:0];
- reg [7:0] e;
- reg r1, r2, r3, r4;
- reg [11:0] encrypted_data[0:7];
- reg [2:0] pre_state;
- reg [7:0] in_data_temp;
- parameter INIT = 3'd0;
- parameter GET_DATA = 3'd1;
- parameter ENCRYPT_DATA = 3'd2;
- parameter OUTPUT_DATA = 3'd3;
- /* using mode signal to process the flip and enable signal */
- wire flip, enable;
- /* fill in the following blanks (e.g.: a = (b == 2'b01) ? 1'b1 : 1'b0) */
- assign flip = (mode == 2'b00)? 0: 1;
- assign enable = (mode == 2'b10)? 0: 1;
- /* instantiate the Parameterized_Ping_Pong_Counter module */
- Parameterized_Ping_Pong_Counter pppc(
- .clk(clk),
- .rst_n(rst_n),
- .enable(enable),
- .max(max),
- .min(min),
- .flip(flip),
- .direction(direction),
- .out(counter_out)
- );
- /* state transition */
- always@(posedge clk)begin
- if(!rst_n) begin
- state <= INIT;
- end
- else begin
- state <= next_state;
- end
- end
- always@(*) begin
- case(state)
- INIT: begin
- pre_state = INIT;
- if(in_valid) next_state = GET_DATA;
- else next_state = INIT;
- end
- GET_DATA: begin
- /* determine the next state */
- pre_state = GET_DATA;
- if(!in_valid && mode == 2'b10) next_state = ENCRYPT_DATA;
- else next_state = GET_DATA;
- end
- ENCRYPT_DATA: begin
- pre_state = ENCRYPT_DATA;
- if(offset_cnt == 4'd7) next_state = OUTPUT_DATA;
- else next_state = ENCRYPT_DATA;
- end
- OUTPUT_DATA: begin
- pre_state = OUTPUT_DATA;
- /* determine the next state */
- if(offset_cnt == 4'd7) next_state = INIT;
- else next_state = OUTPUT_DATA;
- end
- default: begin
- next_state = state;
- end
- endcase
- end
- /* counter (this is the offset_cnt in the Practice_2) */
- always@(posedge clk) begin
- if (!rst_n) begin
- offset_cnt <= 4'd0;
- end
- else begin
- offset_cnt <= next_offset_cnt;
- end
- end
- //add
- /* determine the next_offset_cnt value base on the current state */
- always@(*) begin
- case(state)
- INIT: begin
- next_offset_cnt = offset_cnt;
- out_data = 0;
- end
- GET_DATA: begin
- if (next_state == GET_DATA) begin
- next_offset_cnt = offset_cnt + 1;
- end
- else begin
- next_offset_cnt = 0;
- end
- end
- ENCRYPT_DATA: begin
- if (next_state == ENCRYPT_DATA) begin
- next_offset_cnt = offset_cnt + 1;
- end
- else begin
- next_offset_cnt = 0;
- end
- end
- OUTPUT_DATA: begin
- if (next_state == OUTPUT_DATA) begin
- next_offset_cnt = offset_cnt + 1;
- end
- else begin
- next_offset_cnt = 0;
- end
- end
- default: begin
- next_offset_cnt = offset_cnt;
- end
- endcase
- end
- /* data processing */
- //add
- /* output_tmp */
- always@(posedge clk) begin
- if(!rst_n) begin
- output_tmp[0] <= 8'b0;
- end
- else begin
- output_tmp[next_offset_cnt] <= next_output_tmp[next_offset_cnt];
- end
- end
- //add
- /* determine the next_output_tmp value base on the current state */
- /* You can store the in_data in the next_output_tmp (by using the value of offset_cnt reg)
- and then process these data in the PROCESS_DATA state */
- always@(*) begin
- case(state)
- INIT:begin
- in_data_temp = in_data;
- end
- GET_DATA: begin
- next_output_tmp[0] = in_data_temp;
- if (next_offset_cnt != 0) begin
- next_output_tmp[next_offset_cnt] = in_data;
- end
- end
- ENCRYPT_DATA: begin
- e = (output_tmp[next_offset_cnt] + counter_out) % 256;
- r1 = e[0] ^ e[1] ^ e[3] ^ e[4] ^ e[6];
- r2 = e[0] ^ e[2] ^ e[3] ^ e[5] ^ e[6];
- r3 = e[1] ^ e[2] ^ e[3] ^ e[7];
- r4 = e[4] ^ e[5] ^ e[6] ^ e[7];
- 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};
- end
- default: begin
- next_output_tmp[next_offset_cnt] = output_tmp[next_offset_cnt];
- end
- endcase
- end
- /* output data */
- always @(posedge clk) begin
- if (!rst_n) begin
- out_data <= 12'b0;
- end
- else begin
- /* determine the value of out_data under different circumstances */
- if (next_state == OUTPUT_DATA) begin
- out_data = encrypted_data[next_offset_cnt];
- //next_offset_cnt_value = next_offset_cnt;
- end
- end
- end
- /*always @(negedge clk) begin
- assign output_tmp_value = output_tmp[offset_cnt];
- assign next_output_tmp_value = next_output_tmp[offset_cnt];
- end*/
- endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement