Beruflich Dokumente
Kultur Dokumente
represent a file that has been opened. As files are opened, bit 1
Example:
//***********************************
case(expression)
item1: statement1;
item2: begin
statement2;
statement3;
end
// More codes...
default: default_statement;
endcase
//###########################################
// 4-to-1 multiplexer.
module mux4to1
(output out,
input ip_data3, ip_data2, ip_data1, ip_data0,
input ip_s1, ip_s0);
//***********************************
// Recompute the signal out if any input signal changes.
// All input signals that cause a recomputation of op_out to
// occur must go into the always @(...)
endmodule
casex and casez Statements
Example:
Write the code for a 16-bit loadable up/down counter. The
counter has a load signal and a reset signal, both active on the
rising edge of the clock. The reset signal has priority over the
load signal. If the load signal is not active, then the counter
counts up or down depending on the state of the up signal.
//#############################################
module counter
(output reg [15:0] op_count, // Count.
input [15:0] ip_data, // Data.
input ip_load, // Parallel load.
input ip_updown, // Count up.
input ip_clock, // Clock signal.
input ip_reset); // Active-high synchronous
// reset signal.
//***********************************
always @(posedge ip_clock)
casex({ip_reset, ip_load, ip_updown})
// casex means that x and z are treated as don’t-care.
endcase
endmodule
else if(ip_load)
op_count <= ip_data;
else if(ip_updown)
op_count <= op_count + 16'h0001;
else
op_count <= op_count - 16'h0001;
Loops
Example:
// Increment and display count from 0 to 127 using for loop.
module counter_for;
//***********************************
integer i_count;
//***********************************
initial
for(i_count = 0; i_count < 128; i_count = i_count + 1)
$display("Count = %d", i_count);
endmodule
Example:
// Increment and display count from 0 to 127 using repeat loop.
module counter_repeat;
//***********************************
integer i_count;
//***********************************
initial begin
i_count = 0;
repeat(128) begin
$display("Count = %d", i_count);
i_count = i_count + 1;
end
end
endmodule
Example:
// Increment and display count from 0 to 127 using while loop.
module counter_while;
//***********************************
integer i_count;
//***********************************
initial begin
i_count = 0;
while (i_count < 128) begin
$display("Count = %d", i_count);
i_count = i_count + 1;
end
end
endmodule
generate Statement
Example:
Given the following 8-bit adder module definition, develop a
24-bit adder using the generate statement.
Suppose the 8-bit adder has the following port declaration:
module add8
(output [7:0] op8_sum,
output op8_cout,
input [7:0] ip8_a, ip8_b,
input ip8_cin);
//***********************************
// Interconnects.
wire [3:0] w_carry;
//***********************************
assign w_carry[0] = ip24_cin;
assign op24_cout = w_carry[3];
//***********************************
// Generate the required number of instance.
generate
genvar k; // k is the adder stage number: stage 0, stage 1...