Foreword:
This column aims to record high-frequency pen interview hand-torn code questions for digital front-end autumn recruits. All articles in this column provide principle analysis, codes and waveforms, and all codes have been verified by myself.
The directory is as follows:
1. Digital IC hand-tear code-frequency divider (any even number frequency division)
2. Digital IC hand-tear code-frequency divider (any odd frequency division)
3. Digital IC hand-tear code-frequency divider (any decimal frequency division)
4. Digital IC hand tearing code - asynchronous reset and synchronous release
5. Digital IC hand tear code - edge detection (rising edge, falling edge, double edge)
6. Digital IC hand tearing code-sequence detection (state machine writing method)
7. Digital IC hand tearing code-sequence detection (shift register writing method)
8. Digital IC tearing code - half adder, full adder
9. Digital IC hand tearing code - serial to parallel, parallel to serial
10. Digital IC hand tearing code-data bit width converter (width-narrow, narrow-width conversion)
11. Digital IC hand tearing code - finite state machine FSM - beverage machine
12. Digital IC hand tear code - handshake signal (READY-VALID)
14. Digital IC hand tearing code - Telink micro written test questions
15. Digital IC hand tearing code - Pingtouge technology final face hand tearing real question
16. Digital IC manual tearing code-Zhaoyi innovation written test real questions
18. Digital IC tearing code - dual-port RAM (dual-port-RAM)
...Continually updated
For more hand-tearing code questions, you can go to the digital IC hand-tearing code--question bank
Table of contents
topic description
It is known that a section of data stream data is input, and when datain_ena is high, please select the largest and second largest value of this data stream, and give the corresponding result max, submax and result enable dataout_ena after a section of datain_ena ends.
Input clk;
Input [4:0] datain;
Input datain_ena;
Output [4:0] max;
Output [4:0] submax;
Output dataout_ena;
problem solving ideas
This question is relatively simple and relatively large. There are only two points to pay attention to:
① When the input datain does not exceed two, dataout_ena cannot be pulled high. Only when at least two numbers are input, both max and submax are meaningful.
② Considering all possible situations, when the input data is larger than max, replace max with datain and submax with max; when the input data is equal to max, replace submax data with datain, so that max=submax (see the meaning of the question) Understand, you can not do this); when the input data is less than max but greater than or equal to submax, replace submax with datain; when the input data is less than submax, do nothing.
the code
module find_max(
input clk ,
input [4:0] datain ,
input datain_ena ,
input rstn ,
output reg [4:0] max ,
output reg [4:0] submax ,
output dataout_ena
);
always @(posedge clk)begin
if(!rstn)begin
max <= 5'd0;
submax <= 5'd0;
end
else if(datain_ena == 1'b1)begin
if(datain > max)begin
max <= datain;
submax <= max;
end
else if(datain == max)begin
submax <= datain;
end
else if(datain >=submax && datain < max)begin
submax <= datain;
end
end
end
reg [1:0] count;
always @(posedge clk)begin
if(!rstn)begin
count <= 2'd0;
end
else if(datain_ena && count < 2'd3)begin
count <= count + 1'b1;
end
end
assign dataout_ena = (count >= 2) ? 1:0; //if input number >= 2 , dataout_ena = 1
endmodule
testbench
module find_max_tb();
reg clk,rstn;
reg [4:0] datain;
reg datain_ena;
wire [4:0] max,submax;
wire dataout_ena;
always #5 clk <= ~clk;
initial begin
clk <= 1'b0;
rstn <= 1'b0;
datain_ena <= 1'b0;
#16
rstn <= 1'b1;
#20
datain_ena <= 1'b1;
datain <= 5'd2;
#10
datain <= 5'd3;
#10
datain <= 5'd7;
#10
datain <= 5'd4;
#10
datain <= 5'd9;
#20
datain_ena <= 1'b0;
#200
$finish();
end
//dump fsdb
initial begin
$fsdbDumpfile("find_max.fsdb");
$fsdbDumpvars(0);
end
find_max u_find_max(
.clk (clk) ,
.rstn (rstn) ,
.datain (datain) ,
.datain_ena (datain_ena) ,
.max (max) ,
.submax (submax) ,
.dataout_ena(dataout_ena)
);
endmodule
waveform
As can be seen from the waveform, dataout_ena will be pulled high after inputting two datain. All considerations are met.
For more hand-tearing code questions, you can go to the digital IC hand-tearing code--question bank