Design a Verilog module called vector_splitter that takes an 8-bit input vector and divides it into:
Requirements
vector_splitterin_vec[7:0]out1[3:0] = bits [7:4]out2[1:0] = bits [3:2]out3 = bit [1]out4 = bit [0]Notes
[msb:lsb]) and bit-select ([i]) operators.out2[1:0] should map to [3:2] in the same order, not reversed.Expected behavior (example)
If in_vec = 8'b1101_0110 (binary = 0xD6):
out1 = 1101 (bits [7:4])out2 = 01 (bits [3:2])out3 = 1 (bit [1])out4 = 0 (bit [0]). notation: top.u1.u2.signal.Syntax:
module <name> #(parameter ... ) (port list);
// declarations
// functionality
endmoduleinput, output, inout)macromodule is an alias for module (rarely used).module β¦ endmodule.Instantiation:
mod_name inst_name (.port1(sig1), .port2(sig2)); // named mapping
mod_name inst_name (sig1, sig2); // ordered mappingBy order: Ports matched in the order declared.
adder u1 (a, b, sum);β οΈ Risky: Errors occur if order changes.
By name: Ports explicitly named.
adder u1 (.x(a), .y(b), .z(sum));β Recommended: Safer against future changes.
input β read-only inside the module.output β driven inside, observed outside.inout β bidirectional (used in buses like IΒ²C, data buses).wire.reg if procedural assignment needed.Example:
module m(input clk, input [7:0] data, output reg q, inout bus);0 β logic zero1 β logic onez/Z β high impedance (tri-stated)x/X β unknown (simulation only!)Hardware reality: Only 0, 1, Z exist. X indicates indeterminate logic (conflict, uninitialized, timing issue) in simulation.
~0 = 1, ~1 = 0~x = x, ~z = xxx1 if odd # of 1βs)x/z β result = xx/z poisons result.x.Syntax:
<type> [msb:lsb] <name>;Example:
wire [7:0] data_bus; // 8-bit wide bus (data_bus[7]..data_bus[0])
reg [15:0] acc; // 16-bit register
[7:0] β descending (bit 7 = MSB, bit 0 = LSB) β most common.[0:7] β ascending (bit 0 = MSB, bit 7 = LSB) β allowed, but less used.β οΈ Gotcha: A vectorβs indexing direction matters in part-selects and concatenations.
Access individual bits:
data_bus[0] // LSB
acc[15] // MSBSyntax:
vector[msb:lsb] // fixed part-select
vector[base +: width] // indexed part-select (2001+)
vector[base -: width]Examples:
data_bus[7:4] // upper nibble of 8-bit bus
acc[11:8] // 4 bits from acc
// Indexed part-select (IEEE 1364-2001 & 2005)
acc[3 +: 4] // acc[6:3] (4 bits starting at index 3, ascending)
acc[7 -: 4] // acc[7:4] (4 bits descending from 7)β Indexed part-select avoids confusion when ranges flip.
{}Syntax:
{expr1, expr2, expr3}Examples:
wire [3:0] a = 4'b1010;
wire [3:0] b = 4'b1100;
wire [7:0] c;
assign c = {a, b}; // c = 1010_1100{N{expr}} repeats an expression N times.Example:
{4{1'b1}} // 1111 (4-bit all ones)
{2{a}} // {a,a} β duplicates a vectorScalars and vectors can mix:
{a[3:0], 2'b11, b[1]} // concatenates 4+2+1 = 7 bitsIf RHS narrower β zero-filled (if unsigned) or sign-extended (if signed).
reg [3:0] x;
x = 8'hAB; // x = 4'hB (truncated)& | ^ ~ ^~ ~^Example:
assign out = a & b; // AND each bit&a, |a, ^a, ~&a, ~|a, ~^aExample:
wire parity = ^data_bus; // XOR all bits β paritya << n, a >> n (logical shift, fill with zeros).>>> (arithmetic right shift, preserves sign bit for signed values).Example:
reg signed [7:0] s = -8'd8; // 1111_1000
$display("%b", s >>> 2); // 1111_1110 = -2Out-of-range index: Returns x (unknown).
wire [3:0] a = 4'b1010;
$display("%b", a[10]); // x<size>'<base><value><size>: number of bits<base>: b (binary), o (octal), d (decimal), h (hex)<value>: digitsExamples:
4'b1010 // 4-bit binary
8'hFF // 8-bit hex
'd25 // 32-bit decimal (unsized default)4'b1010'hF = 32-bit hex F.d/h/o/b) and digits are case-insensitive._ allowed β 16'b1010_1100_1111_0000.Preferred syntax:
-8'd6 // 8-bit signed decimal -6
4'shF // signed 4-bit hex = -18'd-6 is illegal.'sd) are sign-extended when assigned.12.34, 0.11.2e3, 4.5E-2_ can be used for readability: 236.123_763_e-12