Hardware does not store “integers” the way Python does. It stores bits — wires that are 0 or 1 — grouped into buses. Signed vs unsigned is a interpretation of those bits. Overflow is what happens when the math does not fit the width.
Width is part of the type. An 8-bit unsigned bus holds 0…255. Add 1 to 255 and you wrap to 0 unless you widen the result.
A single wire is 1 bit. A vector (bus) is [MSB:LSB] — in Verilog usually [7:0] means bit 7 is the most significant.
[7:0] → 8 wires (7 down to 0 inclusive).assign or inside always blocks.MSB = most significant bit (highest weight). LSB = least significant bit (weight 1).
| Syntax | Meaning |
|---|---|
wire a; | 1-bit net (default) |
wire [7:0] data; | 8-bit bus, bit 7 down to 0 |
reg [3:0] nibble; | 4-bit storage (in always blocks) |
8'b1010_1100 | Sized binary literal (underscores optional) |
8'hAC | Same value in hex |
8'd172 | Same value in decimal |
module bus_declare;
wire flag; // 1 bit
wire [7:0] data; // 8-bit bus
wire [15:0] addr; // 16-bit address
// Part-select and concatenation
wire [3:0] high_nibble = data[7:4];
wire [3:0] low_nibble = data[3:0];
wire [15:0] word = {data, data}; // replicate/concat
endmodule
Syntax tip: {a,b} concatenates. {4{1'b1}} replicates. Width of the result is the sum of widths — mismatches cause silent padding/truncation bugs.
The pattern 1111_1111 is 255 unsigned or −1 in 8-bit two’s complement signed. Hardware stores bits; signed tells tools how to interpret arithmetic and comparisons.
Two’s complement: negate by invert-all-bits then add 1.
module signed_demo ( input wire signed [7:0] a, input wire signed [7:0] b, output wire signed [7:0] sum_wrap, // may overflow output wire signed [8:0] sum_wide // safe width ); assign sum_wrap = a + b; // result truncated to 8 bits assign sum_wide = a + b; // Verilog extends for wider LHS endmodule
Mixing signed and unsigned in one expression often causes unexpected zero/sign extension. Keep types consistent, or cast explicitly with $signed() / $unsigned() in SV.
Follow the bit pattern step by step. Goal: compute −5 from +5.
4'b0101 (MSB=0 → positive).4'b1010.4'b1010 + 4'b0001 = 4'b1011 → that is −5 signed.4'sb1011 + 4'sb0101 = 4'b0000 (wraps to 0 in 4 bits) ✓Negation recipe: ~x + 1. MSB flips from 0→1 when going positive to negative.
“What is 8'hFF as signed?” → −1. “As unsigned?” → 255. Same bit pattern — always state the width and signedness.
When you assign a wide bus to a narrow one, truncation drops MSBs silently. When you widen, Verilog zero-extends unsigned values and sign-extends signed values — unless you concatenate explicitly.
| Operation | Verilog pattern | Effect |
|---|---|---|
| Truncate | wire [3:0] lo = wide[3:0]; | Keep LSBs, discard MSBs |
| Zero-extend | {4'b0000, byte} or 8'h00, byte | Pad MSBs with 0 (unsigned widen) |
| Sign-extend | {{4{byte[7]}}, byte} | Replicate MSB (signed widen) |
| Explicit cast (SV) | $signed(wide) / $unsigned(s) | Force interpretation before math |
module trunc_extend (
input wire signed [7:0] s8, // e.g. 8'sb1111_1111 = -1
input wire [7:0] u8, // e.g. 8'hFF = 255
output wire signed [3:0] s_trunc,
output wire signed [15:0] s_sign_ext,
output wire [15:0] u_zero_ext
);
assign s_trunc = s8; // truncates to [3:0] → 4'b1111 (-1)
assign s_sign_ext = s8; // sign-extends to 16 bits → 16'hFFFF
assign u_zero_ext = u8; // zero-extends → 16'h00FF
// Explicit (portable):
// assign s_sign_ext = {{8{s8[7]}}, s8};
// assign u_zero_ext = {8'h00, u8};
endmodule
Comparing 8'hFF (unsigned 255) to 8'sd-1 can surprise you — tools may widen unsigned with zeros while sign-extending the signed value. Cast before compare.
Unsigned wrap: 8'd255 + 8'd1 = 8'd0. Signed wrap: 8'sd127 + 8'sd1 = −128. Both are “correct” for modular arithmetic — and disastrous if you expected math like a calculator.
Step-by-step unsigned wrap: (1) Add binary: 1111_1111 + 0000_0001 = 1_0000_0000 (9 bits). (2) Keep only 8 LSBs → 0000_0000. (3) Carry-out bit 8 = 1 → that is your overflow flag.
Unsigned wrap (overflow) at 255 → 0. Two's complement wrap at +127 → -128.
// Detect unsigned overflow on 8-bit add
module overflow_flag (
input wire [7:0] a, b,
output wire [7:0] sum,
output wire overflow
);
wire [8:0] full = {1'b0, a} + {1'b0, b};
assign sum = full[7:0];
assign overflow = full[8]; // carry-out = unsigned overflow
endmodule
| Operation | Safe pattern |
|---|---|
| Add N-bit + N-bit | Result width N+1 (or keep N and track carry/overflow) |
| Multiply N×N | Result width 2N |
| Shift left | Bits fall off MSB — that is intentional loss unless you widen first |
| Operator | Role | Note |
|---|---|---|
+ - * / % | Arithmetic | / % costly in hardware — prefer shifts when power-of-2 |
& | ^ ~ | Bitwise | Bit-by-bit logic |
&& || ! | Logical | Reduce to true/false (1-bit sense) |
== != | Equality | Use === in TB for X/Z awareness |
<< >> | Shift | Logical; arithmetic >>> in SV for signed |
{ } {n{}} | Concat / replicate | Build buses from pieces |
A bus is a multi-lane highway. Overflow is cars spilling onto a road that only has N lanes — they wrap around unless you built an exit ramp (wider result).
[7:0] and name MSB/LSB