cs 150 - spring 2008 – lec #3: combinational logic - 1 example: prime generation zproblem: “in...

47
CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation Problem: “in practice” efficiently generate all primes for a function Solution: Use recursive-evaluation method on “cofactors”

Post on 19-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1

Example: Prime Generation

Problem: “in practice” efficiently generate all primes for a function

Solution: Use recursive-evaluation method on “cofactors”

Page 2: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 2

Cofactors

Partial evaluation of function Fa = F evaluated at a=1

Fa’ = F evaluated at a=0

Example: F=a’b’d’ + abd’ +a’b Fa = bd’

Fa’ = b’d’ + b

For any function F, variable a:

F = a Fa + a’ Fa’ (“Shannon Expansion”)

Example: F = a bd’ + a’(b’d’ + b) [Check it!]

Page 3: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 3

Cofactors and Primes

Theorem: Any Function F, variable a, Primes are maximal implicants of set:

{a Primes(Fa), a’ Primes(Fa’), Primes(Fa) Primes(Fa’)}

In other words Find the primes of Fa, multiply each by a

Find the primes of Fa’, multiply each by a’

Multiply each prime of Fa by each prime of Fa’

Maximal members of these sets are primes of F

Page 4: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 4

Example

0 0

1 1

X 0

X 1D

A

1 1

0 X

0 0

0 0

B

C

F =

0 0

1 1

1 1

0 X

Fa’ =

Primes(Fa’) = d, bc

X 0

X 1

A

0 0

0 0

Fa

=

Primes(Fa) = bc’,c’d’

Primes(F) = a’d, a’bc, abc’,ac’d’, bc’d

Page 5: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 5

Similar Fast Techniques to Generate Rows

Recurse down cofactor tree, throwing out primes when they disagree

Stop when: Cofactor of on-set is empty (no rows) All the remaining primes cover the whole space (1 row, with

those primes)

Still one very fast technique, “Espresso-Signature” (to follow approximate algorithm)

Page 6: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 6

Approximate Algorithm

Don’t try to generate minimum cover

Given a cover (sum-of-products for on-set and don’t-care set) Generate a prime cover smaller than given cover Keep generating smaller prime covers until you can’t anymore

Program – Espresso-II

Page 7: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 7

Espresso-II algorithm (approx)

Given Cover C = c1 + c2 +…+cn

While (we keep making improvement) PrimeCover = 0 Foreach cube c_i of the cover, C

Find a prime p_i containing c_i that contains as many c_j’s as possible

PrimeCover = PrimeCover + p_i We now have a prime cover p1+…+pn, Make the cover

“irredundant” (example next slide). Result is a smaller prime cover

“Reduce” each cube away from a prime while still retaining a cover (this permits a different expansion in next iteration)

Page 8: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 8

Espresso-II example

F=a’b’d’ + abc’d’ + abc+ a’b, DC = abcd

Expand: a’b’d’ => a’d’ abc’d’ => bd’ abc => bc a’b => a’b

Prime Cover a’d’ + bd’ + bc + a’b

Prime bc is redundant: covered by bd’ + a’b + don’t-care

Irredundant cover is:

a’d’ + bd’ + a’b Reduce to a’b’d’ + bd’ + a’bd

for next iteration

1 0 1 11 0 1 10 0 X 10 0 0 1

a’b’ ab’ab a’b

c’d’

cd

cd’

c’d

Page 9: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 9

Espresso-II presented here very simplified

See text for fuller description

What I left out Details of EXPAND (how to pick the primes) Details of IRREDUNDANT Details of REDUCE ESSENTIAL_PRIMES (identified early and tossed into don’t-care

set for efficiency) LAST_GASP (radical reduction to get new starting point) MAKESPARSE (final pruning of literals in the output plane)

Page 10: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 10

Return to Exact Methods

Realization (mid-nineties): The only purpose of generating primes was to get to covering table!

Specifically: to get to “non-dominated” rows of covering table

Realization (beyond the scope of this class) Can identify non-dominated

rows without generating primes or minterms!

“Espresso Signature”: first radical revision of exact two-level synthesis algorithm

a’d’

0XX0

bd’

X1X0

a’b

01XX

bc

X11X

0 (0000)

1

2 (0010)

1

5 (0101)

1

12 (1100)

1

Page 11: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 11

Espresso-Signature

Espresso-Signature Algorithm Generate non-dominated rows of covering table

This forms a cover, the “canonical cover” of a logic function Use modified Espresso EXPAND to generate non-dominated

columns of covering table Solve the covering problem

Much more efficient than standard algorithm

Page 12: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 12

Two-Level Synthesis Summary

Two exact methods: Quine-McCluskey and Espresso-Signature In practice, both highly efficient Theoretically: exponential complexity Algorithms work well because they fit circuits people actually

design (though this set is not characterized well)

One approximate method shown, Espresso Others (McBoole, e.g.) similar Also theoretically exponential complexity! In practice, fast, gives results within a few percent of optimum.

Page 13: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 13

Implementations of Two-level Logic

Sum-of-products AND gates to form product terms

(minterms) OR gate to form sum

Product-of-sums OR gates to form sum terms

(maxterms) AND gates to form product

Page 14: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 14

Two-level Logic Using NAND Gates

Replace minterm AND gates with NAND gates

Place compensating inversion at inputs of OR gate

Page 15: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 15

Two-level Logic Using NAND Gates (cont’d)

OR gate with inverted inputs is a NAND gate de Morgan's: A' + B' = (A • B)'

Two-level NAND-NAND network Inverted inputs are not counted In a typical circuit, inversion is done once and signal distributed

Page 16: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 16

Two-level Logic Using NOR Gates

Replace maxterm OR gates with NOR gates

Place compensating inversion at inputs of AND gate

Page 17: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 17

Two-level Logic Using NOR Gates (cont’d)

AND gate with inverted inputs is a NOR gate de Morgan's: A' • B' = (A + B)'

Two-level NOR-NOR network Inverted inputs are not counted In a typical circuit, inversion is done once and signal distributed

Page 18: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 23

Combinational Logic Summary Logic functions, truth tables, and switches

NOT, AND, OR, NAND, NOR, XOR, . . ., minimal set

Axioms and theorems of Boolean algebra Proofs by re-writing and perfect induction

Gate logic Networks of Boolean functions and their time behavior

Canonical forms Two-level and incompletely specified functions

Simplification Two-level simplification

Page 19: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 24

Hardware Description Languages: Verilog

Verilog Structural Models (Combinational) Behavioral Models Syntax Examples

Page 20: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 25

Quick History of HDLs ISP (circa 1977) - research project at CMU

Simulation, but no synthesis

Abel (circa 1983) - developed by Data-I/O Targeted to programmable logic devices Not good for much more than state machines

Verilog (circa 1985) - developed by Gateway (now Cadence) Similar to Pascal and C Delays is only interaction with simulator Fairly efficient and easy to write IEEE standard

VHDL (circa 1987) - DoD sponsored standard Similar to Ada (emphasis on re-use and maintainability) Simulation semantics visible Very general but verbose IEEE standard

“Synchronous/Reactive Languages” (1980’s-1990’s) Esterel, Lustre, Signal, SMV, V++ Developed for embedded software and formal verification V++ Explicit “Hardwware Design Language” Semantics much closer to hardware (but no tool support )

Page 21: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 26

Design Methodology

HDLSpecification

Structure and Function(Behavior) of a Design

Simulation

Verification: DesignBehave as Required?

Functional: I/O BehaviorRegister-Level (Architectural)

Logic-Level (Gates)Transistor-Level (Electrical)Timing: Waveform Behavior

Synthesis

Generation: MapSpecification toImplementation

Page 22: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 27

Verilog/VHDL

The “standard” languages

Very similar Many tools provide front-ends to both Verilog is simpler

Less syntax, fewer constructs VHDL supports large, complex systems

Better support for modularization More grungy details “Hello world” is much bigger in VHDL

Page 23: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 28

Verilog

Supports structural and behavioral descriptions Structural

Explicit structure of the circuit How a module is composed as an interconnection of more

primitive modules/components E.g., each logic gate instantiated and connected to others

Behavioral Program describes input/output behavior of circuit Many structural implementations could have same behavior E.g., different implementations of one Boolean function

Page 24: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 29

Key Problem (Both Verilog AND VHDL)

Original use as modeling and simulation tools Goal was to let designers simulate circuits

Synthesis came later

Semantics defined in terms of discrete-event simulation, not hardware

Leads to peculiar behavior, particularly concerning latches Latches don’t necessarily appear when you think they should (“reg”

declaration doesn’t necessarily mean a latch) Latches appear when you aren’t expecting them (when Verilog decides

there is undefined state)

US Patent 6,725,187: “Compiling Verilog into Timed Finite State Machines” Oy, vey…

Key lesson: be careful!

Page 25: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 30

Verilog Introduction

the module describes a component in the circuit

Two ways to describe: Structural Verilog

List of components and how they are connected Just like schematics, but using text Hard to write, hard to decode Useful if you don’t have integrated design tools

Behavioral Verilog Describe what a component does, not how it does it Synthesized into a circuit that has this behavior

Page 26: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 31

module xor_gate (out, a, b); input a, b; output out; wire abar, bbar, t1, t2;

inverter invA (abar, a); inverter invB (bbar, b); and_gate and1 (t1, a, bbar); and_gate and2 (t2, b, abar); or_gate or1 (out, t1, t2);

endmodule

Structural Model

Composition of primitive gates to form more complex module

Note use of wire declaration!

By default, identifiersare wires

A B

OutT2

T1

Page 27: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 32

Structural Model

Example of full-adder

module full_addr (A, B, Cin, S, Cout); input A, B, Cin; output S, Cout; assign {Cout, S} = A + B + Cin;endmodule

module adder4 (A, B, Cin, S, Cout); input [3:0] A, B; input Cin; output [3:0] S; output Cout; wire C1, C2, C3; full_addr fa0 (A[0], B[0], Cin, S[0], C1); full_addr fa1 (A[1], B[1], C1, S[1], C2); full_addr fa2 (A[2], B[2], C2, S[2], C3); full_addr fa3 (A[3], B[3], C3, S[3], Cout);endmodule

Behavior

Structural

Page 28: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 33

module and_gate (out, in1, in2); input in1, in2; output out;

assign out = in1 & in2;

endmodule

Simple Behavioral Model

Combinational logic Describe output as a function of inputs Note use of assign keyword: continuous assignment

Output port of a primitive mustbe first in the list of ports

Restriction does not apply tomodules

Page 29: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 38

Verilog Module

Corresponds to a circuit component “Parameter list” is the list of external connections, aka “ports” Ports are declared “input”, “output” or “inout”

inout ports used on tri-state buses Port declarations imply that the variables are wires

module full_addr (A, B, Cin, S, Cout); input A, B, Cin; output S, Cout; assign {Cout, S} = A + B + Cin;endmodule

module name

inputs/outputs

ports

Page 30: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 39

assign A = X | (Y & ~Z);

assign B[3:0] = 4'b01XX;

assign C[15:0] = 16'h00ff;

assign #3 {Cout, S[3:0]} = A[3:0] + B[3:0] + Cin;

use of arithmetic operator

multiple assignment (concatenation)

delay of performing computation, only used by simulator, not synthesis

use of Boolean operators(~ for bit-wise, ! for logical negation)

bits can take on four values(0, 1, X, Z)

variables can be n-bits wide(MSB:LSB)

Verilog Continuous Assignment Assignment is continuously evaluated assign corresponds to a connection or a simple component

with the described function Target is NEVER a reg variable

Page 31: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 40

module Compare1 (A, B, Equal, Alarger, Blarger); input A, B; output Equal, Alarger, Blarger;

assign Equal = (A & B) | (~A & ~B); assign Alarger = (A & ~B); assign Blarger = (~A & B);endmodule

Comparator Example

A B

Equal

AlargerBlarger

Page 32: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 41

// Make a 4-bit comparator from 4 x 1-bit comparators

module Compare4(A4, B4, Equal, Alarger, Blarger); input [3:0] A4, B4; output Equal, Alarger, Blarger; wire e0, e1, e2, e3, Al0, Al1, Al2, Al3, B10, Bl1, Bl2, Bl3;

Compare1 cp0(A4[0], B4[0], e0, Al0, Bl0); Compare1 cp1(A4[1], B4[1], e1, Al1, Bl1); Compare1 cp2(A4[2], B4[2], e2, Al2, Bl2); Compare1 cp3(A4[3], B4[3], e3, Al3, Bl3);

assign Equal = (e0 & e1 & e2 & e3); assign Alarger = (Al3 | (Al2 & e3) | (Al1 & e3 & e2) | (Al0 & e3 & e2 & e1)); assign Blarger = (~Alarger & ~Equal);endmodule

Comparator Example

Page 33: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 42

module and_gate (out, in1, in2); input in1, in2; output out; reg out;

always @(in1 or in2) begin out = in1 & in2; endendmodule

Simple Behavioral Model: the always block

always block Always waiting for a change to a trigger signal Then executes the body

Not a real register!!A Verilog registerNeeded because of assignment in always block

Specifies when block is executed I.e., triggered by which signals

Page 34: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 43

always Block

Procedure that describes the function of a circuit Can contain many statements including if, for, while, case Statements in the always block are executed sequentially

(Non-blocking assignments <= are executed in parallel) Blocking assignments = are executed sequentially

Entire block is executed at once Final result describes the function of the circuit for current set

of inputs intermediate assignments don’t matter, only the final result

begin/end used to group statements

Page 35: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 44

“Complete” Assignments

If an always block executes, and a variable is not assigned Variable keeps its old value (think implicit state!) NOT combinational logic latch is inserted (implied memory)This is usually not what you want: dangerous for the novice!

Any variable assigned in an always block should be assigned for any (and every!) execution of the block Check particularly: case statements (missing default, and how

can the compiler tell?)

Page 36: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 45

module and_gate (out, in1, in2); input in1, in2; output out; reg out;

always @(in1) begin out = in1 & in2; end

endmodule

Incomplete Triggers

Leaving out an input trigger usually results in a sequential circuit

module and_gate (out, in1, in2); input in1, in2; output out; reg out;

always @(in1 or in2) begin out = in1 & in2; end

endmodule

Generates a Latch Doesn’t Generate a Latch

Page 37: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 46

One Latch or Two?

module d_latch (clk, a, c); input clk, a; output c; reg c; reg b; always @(posedge clk) begin b = a; c = b; end

endmodule

Q

QSET

CLR

D

C

BA

Clk

Page 38: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 47

One Latch or Two?

module d_latch (clk, a, c); input clk, a; output c; reg c; reg b; always @(posedge clk) begin b <= a; c <= b; end

endmodule

Q

QSET

CLR

D CA

Clk Q

QSET

CLR

D

B

Why?Non-blocking assignments in parallelc gets previous value of b

Page 39: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 48

Verilog if

Same as C if statement

// Simple 4:1 muxmodule mux4 (sel, A, B, C, D, Y);input [1:0] sel; // 2-bit control signalinput A, B, C, D;output Y;reg Y; // target of assignment

always @(sel or A or B or C or D) if (sel == 2’b00) Y = A; else if (sel == 2’b01) Y = B; else if (sel == 2’b10) Y = C; else if (sel == 2’b11) Y = D;

endmodule

Page 40: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 49

Verilog if

Another way

// Simple 4:1 muxmodule mux4 (sel, A, B, C, D, Y);input [1:0] sel; // 2-bit control signalinput A, B, C, D;output Y;reg Y; // target of assignment

always @(sel or A or B or C or D) if (sel[0] == 0) if (sel[1] == 0) Y = A; else Y = B; else if (sel[1] == 0) Y = C; else Y = D;endmodule

Page 41: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 50

Verilog case

Sequential execution of cases Only first case that matches is executed (implicit break) Default case can be used

// Simple 4-1 muxmodule mux4 (sel, A, B, C, D, Y);input [1:0] sel; // 2-bit control signalinput A, B, C, D;output Y;reg Y; // target of assignment

always @(sel or A or B or C or D) case (sel) 2’b00: Y = A; 2’b01: Y = B; 2’b10: Y = C; 2’b11: Y = D; endcaseendmodule

Conditions tested intop to bottom order

Page 42: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 51

Verilog case

Note:case (case_expression)

case_item1 : case_item_statement1; case_item2 : case_item_statement2; case_item3 : case_item_statement3; case_item4 : case_item_statement4; default : case_item_statement5;

endcase … is the same as …

if(case_expression == case_item1) case_item_statement1;

else if (case_expression == case_item2) case_item_statement2;

else if (case_expression == case_item3) case_item_statement3;

else if (case_expression == case_item4)case_item_statement4;

else case_item_statement5;

Page 43: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 52

Verilog case vs C switch

Note difference from C:switch (case_expression) {

case_item1 : case_item_statement1; case_item2 : case_item_statement2; case_item3 : case_item_statement3; case_item4 : case_item_statement4; default : case_item_statement5; }

… is the same as …if(case_expression == case_item1)

case_item_statement1; if (case_expression == case_item2)

case_item_statement2; if (case_expression == case_item3)

case_item_statement3; if (case_expression == case_item4)

case_item_statement4; case_item_statement5;

This is why we need break statements in C!

Page 44: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 53

Verilog case Without the default case, this example would create a latch

for Y Assigning X to a variable means synthesis is free to assign

any value// Simple binary encoder (input is 1-hot)module encode (A, Y);input [7:0] A; // 8-bit input vectoroutput [2:0] Y; // 3-bit encoded outputreg [2:0] Y; // target of assignment

always @(A) case (A) 8’b00000001: Y = 0; 8’b00000010: Y = 1; 8’b00000100: Y = 2; 8’b00001000: Y = 3; 8’b00010000: Y = 4; 8’b00100000: Y = 5; 8’b01000000: Y = 6; 8’b10000000: Y = 7; default: Y = 3’bXXX; // Don’t care when input is not 1-hot endcaseendmodule

Page 45: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 54

Verilog case (cont)

Cases are executed sequentially Following implements a priority encoder

// Priority encodermodule encode (A, Y);input [7:0] A; // 8-bit input vectoroutput [2:0] Y; // 3-bit encoded outputreg [2:0] Y; // target of assignment

always @(A) case (1’b1) A[0]: Y = 0; A[1]: Y = 1; A[2]: Y = 2; A[3]: Y = 3; A[4]: Y = 4; A[5]: Y = 5; A[6]: Y = 6; A[7]: Y = 7; default: Y = 3’bXXX;// Don’t care when input is all 0’s endcaseendmodule

Page 46: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 55

Parallel Case

A priority encoder is more expensive than a simple encoder If we know the input is 1-hot, we can tell the synthesis tools “parallel-case” pragma says the order of cases does not matter

// simple encodermodule encode (A, Y);input [7:0] A; // 8-bit input vectoroutput [2:0] Y; // 3-bit encoded outputreg [2:0] Y; // target of assignment

always @(A) case (1’b1) // synthesis parallel-case A[0]: Y = 0; A[1]: Y = 1; A[2]: Y = 2; A[3]: Y = 3; A[4]: Y = 4; A[5]: Y = 5; A[6]: Y = 6; A[7]: Y = 7; default: Y = 3’bX; // Don’t care when input is all 0’s endcaseendmodule

Page 47: CS 150 - Spring 2008 – Lec #3: Combinational Logic - 1 Example: Prime Generation zProblem: “in practice” efficiently generate all primes for a function

CS 150 - Spring 2008 – Lec #3: Combinational Logic - 57

casex Example

// Priority encodermodule encode (A, valid, Y);input [7:0] A; // 8-bit input vectoroutput [2:0] Y; // 3-bit encoded outputoutput valid; // Asserted when an input is not all 0’sreg [2:0] Y; // target of assignmentreg valid;

always @(A) begin valid = 1; casex (A) 8’bXXXXXXX1: Y = 0; 8’bXXXXXX10: Y = 1; 8’bXXXXX100: Y = 2; 8’bXXXX1000: Y = 3; 8’bXXX10000: Y = 4; 8’bXX100000: Y = 5; 8’bX1000000: Y = 6; 8’b10000000: Y = 7; default: begin valid = 0; Y = 3’bX; // Don’t care when input is all 0’s end endcase endendmodule