Your code simulates two multiplexers. These are actually asynchronous components. The fact that Verilog requires data1_temp
and data2_temp
to be declared as reg
's is a quirk of Verilog syntax and your choice of coding style, and doesn't mean these signals would be the outputs of storage elements in a physical implementation.
If you want to capture these values in actual registers, you need to add those explicitly:
reg [7:0] data1, data2;
always @(posedge someclock) begin
data1 <= data1_tmp;
data2 <= data2_tmp;
end
But I would like to know what this mini register file would be made of in hardware. Particularly, the 4x8 bit array consisting of k0,k1,k2,k3.
You haven't shown how these variables are assigned, so it's not possible to say how they are implemented. As your code showed, just declaring them as reg
doesn't guarantee they are implemented with actual storage elements. If you assign them inside a block that begins always @(posedge clk)
then very likely they are flip-flops, but there are ways you could code them that would make them synthesize differently.
I thought when it came to registers and arrays like this, you need a clock to read out data, like RAM?
You need a clock to update a (physical) register. You can read it out at any time. For example:
wire [8:0] sum;
assign sum = k0 + k1;
is perfectly valid code. sum
will change whenever any of its inputs changes. If k0
and k1
are the outputs of flip-flops, their values will only change when there is a clock edge.
For another example, you could equally well describe your multiplexers with code like this:
reg [7:0] k0, k1, k2, k3;
wire [7:0] data1_tmp;
reg [1:0] reg1;
// k<n> and reg1 are assigned elsewhere.
assign data1_tmp = (reg1 == 0) ? k0 :
(reg1 == 1) ? k1 :
(reg1 == 2) ? k2 : k3;
how do I read from this tag_array and do the comparison all within the same clock cycle?
Let me repeat a key point for emphasis: You need to use a clock to assign a new value to a register (an actual hardware register or group of flip-flops). It's output is available at any time.
RAMs are different and how you access the contents of a RAM will depend on details of the type of RAM you use.
I got confused because frankly I don't know enough about memory hardware and how that's possible.
Another key strategy: When you are learning digital logic, I recommend you learn about the physical hardware first, and then work out or study how to simulate it in HDL second. So first, learn what a physical flip-flop is, then learn the standard Verilog methods of describing a flip-flop. Especially if you are trying to write HDL for synthesis, trying to write good code before you learn the capabilities of the underlying hardware will lead you down a lot of dead-end paths.
If I understood your question correctly, I think the following module does what you want. You can very simply specify the gate by its operator (^
,&
,|
, etc) and nothing else. Plus only the desired assignments are generated (which is I think what you are looking for).
`timescale 1 ns / 1 ps
module selectable_gate(input [7:0] in, output out);
parameter [7:0] SELECT = 8'b10010100;
function integer count_ones;
input [7:0] v;
integer ret;
integer i;
begin
ret = 0;
for(i=0;i<8;i=i+1) begin
if (v[i]) begin
ret = ret+1;
end
end
count_ones = ret;
end
endfunction
function integer nth_one;
input integer n;
input [7:0] v;
integer i,ret,cnt;
begin
ret = -1;
cnt = 0;
for(i=0;i<8;i=i+1) begin
if (v[i]) begin
if (cnt == n) begin
ret = i;
end
cnt = cnt+1;
end
end
nth_one = ret;
end
endfunction
localparam integer w = count_ones(SELECT);
wire [w-1:0] y;
generate
genvar i;
for(i=0 ; i<w ; i=i+1) begin
assign y[i] = in[nth_one(i,SELECT)];
end
endgenerate
assign out = ^y; // specify gate here (e.g. ^y, &y, |y, etc)
endmodule;
The above module for the default SELECT = 8'b10010100
should end up generated as:
assign y[0] = in[2];
assign y[1] = in[4];
assign y[2] = in[7];
assign out = ^y;
Which is the same as xor(in[2],in[4],in[7])
.
Since genvars are always unrolled as constants inside the generates, the trick was to find constants that allow the proper conditions for the generate, and use functions for any temporary variable manipulation necessary to calculate them.
Best Answer
You can do parameterised functions:
But this is only useful if you are using the function for one data width in the module. Basically you can use the module parameters to parameterise the width of the function, but you couldn't use the same function with two different widths in the same module.