Forum Discussion
26 Replies
- Altera_Forum
Honored Contributor
to get max of stream:
in a clocked process: if din > max then max <= din; addr_of_max <= addr; end if; - Altera_Forum
Honored Contributor
--- Quote Start --- to get max of stream: in a clocked process: if din > max then max <= din; addr_of_max <= addr; end if; --- Quote End --- Hi Kaz, thanks for your reply... i know the concept... but I have no idea how to write it in Verilog code... though I have tried for several times, the output is wrong... - Altera_Forum
Honored Contributor
I have written the code in vhdl, which should work provided you look after type issues(signed...etc).
You can write verilog on same basis. - Altera_Forum
Honored Contributor
--- Quote Start --- I have written the code in vhdl, which should work provided you look after type issues(signed...etc). You can write verilog on same basis. --- Quote End --- Hi Kaz, here is my code... but it's wrong... can you figure out my mistake? module absolute(clock,Enable, d_in, max_value) ; input wire clock; input wire Enable; input wire [9:0] d_in; output reg [9:0] max_value; reg [9:0] max_val_tmp; reg [2:0] Q; reg [9:0] mem_array [0:7]; initial begin max_val_tmp = mem_array[0]; end always @ (posedge clock) begin if (Enable) begin mem_array[Q] <= d_in; if (mem_array[Q] > max_val_tmp) begin max_value <= mem_array[Q]; end else begin max_value <= max_val_tmp; end Q <= Q + 1; end end endmodule - Altera_Forum
Honored Contributor
I suggested:
you need to take care of verilog syntax and rules as I am not good at it. You also need to take care of address latency. I also thought you are reading from memory but your code suggest are just writing to an array from din?? so which is'it? Anyway the code for din above should work in principle. what you do with din before/after is your focus.always @ (posedge clock) begin if (Enable) begin if (din > max_val) begin max_val <= din; Q_val <= Q; end Q <= Q + 1; end end - Altera_Forum
Honored Contributor
--- Quote Start --- I suggested:
you need to take care of verilog syntax and rules as I am not good at it. You also need to take care of address latency. I also thought you are reading from memory but your code suggest are just writing to an array from din?? so which is'it? Anyway the code for din above should work in principle. what you do with din before/after is your focus. --- Quote End --- Thank you Kaz for your time :) I have tried the codes below, both with mem_array and without mem_array gave the same results which are wrong... (as picture attachment) module absolute(clock,Enable, d_in, max_value, max_location) ; input wire clock; input wire Enable; input wire [9:0] d_in; output reg [9:0] max_value; output reg [2:0] max_location; reg [9:0] max_val_tmp; reg [2:0] Q; //reg [9:0] mem_array [0:7]; initial begin max_val_tmp = 0; end always @ (posedge clock) begin if (Enable) begin //mem_array[Q] <= d_in; if (d_in > max_val_tmp) begin max_value <= d_in; max_location <= Q; end Q <= Q + 1; end end endmodule **p.s. I need to write d_in into mem_array[Q] so that i can keep track on its location right? **another way i found something: "develop a verilog model for a peak detector that finds the maximum value in a sequence of 10-bit unsigned integers. a new number arrives at the input during a clock cycle when the data_en input is 1. if the new number is greater than the previously stored maximum value, the maximum value is updated with the new number; otherwise, it is unchanged. the stored maximum value is cleared to zero when the reset control input is 1. both data_en and reset are synchronous control inputs."always @ (posedge clock) begin if (Enable) begin if (din > max_val) begin max_val <= din; Q_val <= Q; end Q <= Q + 1; end end - Altera_Forum
Honored Contributor
--- Quote Start --- Thank you Kaz for your time :) **p.s. I need to write d_in into mem_array[Q] so that i can keep track on its location right? **another way i found something: "develop a verilog model for a peak detector that finds the maximum value in a sequence of 10-bit unsigned integers. a new number arrives at the input during a clock cycle when the data_en input is 1. if the new number is greater than the previously stored maximum value, the maximum value is updated with the new number; otherwise, it is unchanged. the stored maximum value is cleared to zero when the reset control input is 1. both data_en and reset are synchronous control inputs." --- Quote End --- What another way? It is same one I am telling you since last year!! and your code : why do you keep using the wrong signal name: why do you use max_val_tmp, I did not say that. - Altera_Forum
Honored Contributor
--- Quote Start --- What another way? It is same one I am telling you since last year!! and your code : why do you keep using the wrong signal name: why do you use max_val_tmp, I did not say that. --- Quote End --- last year?? i just joined today... if i didn't create a temparory reg "max_val_tmp" how can keep the number for comparing with the next number?? if you don't mind to edit my whole codes, so i don't make any mistakes again won't ask you anymore?? - Altera_Forum
Honored Contributor
now i change to these codes and the output as shown in picture attachment...
input wire Enable; input wire [9:0] d_in; output reg [9:0] max_value; output reg [2:0] max_location; reg [2:0] Q; initial begin max_value = 0; end always @ (posedge clock) begin if (Enable) begin if (d_in > max_value) begin max_value <= d_in; max_location <= Q; end Q <= Q + 1; end end endmodule - Altera_Forum
Honored Contributor
Just use my above code. The max_val updates and stores din by itself until it is updated (if at all).
I am sorry I have not done verilog for years so can't help directly at code level. Yourlast code and waveforms looks ok to me