[159] | 1 | module amplitude
|
---|
| 2 | #(
|
---|
| 3 | parameter width = 12 // bit width of the input data
|
---|
| 4 | )
|
---|
| 5 | (
|
---|
| 6 | input wire clock, frame, reset,
|
---|
[173] | 7 | input wire [width-1:0] min_data,
|
---|
| 8 | input wire [width-1:0] max_data,
|
---|
[159] | 9 | input wire [width-1:0] inp_data,
|
---|
| 10 | output wire [width-1:0] out_data,
|
---|
| 11 | output wire [1:0] out_flag
|
---|
| 12 | );
|
---|
| 13 |
|
---|
| 14 | reg int_case_reg, int_case_next;
|
---|
| 15 | reg out_flag_reg, out_flag_next;
|
---|
| 16 | reg int_flag_reg, int_flag_next;
|
---|
| 17 | reg [width-1:0] int_mini_reg, int_mini_next;
|
---|
| 18 | reg [width-1:0] out_data_reg, out_data_next;
|
---|
| 19 | reg [width-1:0] inp_data_reg [1:0], inp_data_next [1:0];
|
---|
| 20 |
|
---|
| 21 | wire int_comp_wire;
|
---|
| 22 | reg int_comp_reg, int_comp_next;
|
---|
| 23 |
|
---|
| 24 | reg [5:0] int_cntr_reg, int_cntr_next;
|
---|
| 25 |
|
---|
| 26 | assign int_comp_wire = (inp_data_reg[1] < inp_data);
|
---|
| 27 |
|
---|
| 28 | always @(posedge clock)
|
---|
| 29 | begin
|
---|
| 30 | if (reset)
|
---|
| 31 | begin
|
---|
| 32 | int_case_reg <= 1'b0;
|
---|
| 33 | int_mini_reg <= {(width){1'b0}};
|
---|
| 34 | inp_data_reg[0] <= {(width){1'b0}};
|
---|
| 35 | inp_data_reg[1] <= {(width){1'b0}};
|
---|
| 36 | out_data_reg <= {(width){1'b0}};
|
---|
| 37 | out_flag_reg <= 1'b0;
|
---|
| 38 | int_flag_reg <= 1'b0;
|
---|
| 39 | int_comp_reg <= 1'b0;
|
---|
| 40 | int_cntr_reg <= 6'd0;
|
---|
| 41 | end
|
---|
| 42 | else
|
---|
| 43 | begin
|
---|
| 44 | int_case_reg <= int_case_next;
|
---|
| 45 | int_mini_reg <= int_mini_next;
|
---|
| 46 | inp_data_reg[0] <= inp_data_next[0];
|
---|
| 47 | inp_data_reg[1] <= inp_data_next[1];
|
---|
| 48 | out_data_reg <= out_data_next;
|
---|
| 49 | out_flag_reg <= out_flag_next;
|
---|
| 50 | int_flag_reg <= int_flag_next;
|
---|
| 51 | int_comp_reg <= int_comp_next;
|
---|
| 52 | int_cntr_reg <= int_cntr_next;
|
---|
| 53 | end
|
---|
| 54 | end
|
---|
| 55 |
|
---|
| 56 | always @*
|
---|
| 57 | begin
|
---|
| 58 | int_case_next = int_case_reg;
|
---|
| 59 | int_mini_next = int_mini_reg;
|
---|
| 60 | inp_data_next[0] = inp_data_reg[0];
|
---|
| 61 | inp_data_next[1] = inp_data_reg[1];
|
---|
| 62 | out_data_next = out_data_reg;
|
---|
| 63 | out_flag_next = out_flag_reg;
|
---|
| 64 | int_flag_next = int_flag_reg;
|
---|
| 65 | int_comp_next = int_comp_reg;
|
---|
| 66 | int_cntr_next = int_cntr_reg;
|
---|
| 67 |
|
---|
| 68 | case (int_case_reg)
|
---|
| 69 | 0:
|
---|
| 70 | begin
|
---|
| 71 | if (frame)
|
---|
| 72 | begin
|
---|
| 73 | inp_data_next[0] = inp_data;
|
---|
| 74 | inp_data_next[1] = inp_data_reg[0];
|
---|
| 75 | int_comp_next = int_comp_wire;
|
---|
| 76 | out_data_next = {(width){1'b0}};
|
---|
| 77 | out_flag_next = 1'b0;
|
---|
| 78 | // minimum
|
---|
| 79 | if ((~int_comp_reg) & (int_comp_wire) & int_cntr_reg[5])
|
---|
| 80 | begin
|
---|
| 81 | int_mini_next = inp_data_reg[0];
|
---|
| 82 | int_flag_next = 1'b1;
|
---|
| 83 | end
|
---|
| 84 | // maximum after minimum
|
---|
| 85 | else if ((int_comp_reg) & (~int_comp_wire) & (int_flag_reg))
|
---|
| 86 | begin
|
---|
| 87 | out_data_next = inp_data_reg[0] - int_mini_reg;
|
---|
| 88 | int_flag_next = 1'b0;
|
---|
| 89 | int_case_next = 1'b1;
|
---|
| 90 | end
|
---|
| 91 | else if (~int_cntr_reg[5])
|
---|
| 92 | begin
|
---|
| 93 | int_cntr_next = int_cntr_reg + 6'd1;
|
---|
| 94 | end
|
---|
| 95 | end
|
---|
| 96 | end
|
---|
| 97 |
|
---|
| 98 | 1:
|
---|
| 99 | begin
|
---|
[173] | 100 | if (out_data_reg > min_data)
|
---|
[159] | 101 | begin
|
---|
| 102 | int_cntr_next = 6'b0;
|
---|
[173] | 103 | // out_flag_next = 1'b1;
|
---|
| 104 | out_flag_next = (inp_data_reg[1] < max_data);
|
---|
[159] | 105 | end
|
---|
| 106 | int_case_next = 1'b0;
|
---|
| 107 | end
|
---|
| 108 |
|
---|
| 109 | endcase
|
---|
| 110 | end
|
---|
| 111 |
|
---|
| 112 | assign out_data = out_data_reg;
|
---|
| 113 | assign out_flag = {~int_cntr_reg[5], out_flag_reg};
|
---|
| 114 |
|
---|
| 115 | endmodule
|
---|