Hardware or Software, and the Cost of a Late Change

Any function of a chip can be a block of logic or a few lines of program. Learn what each choice costs, and why the date of a change matters most.

You Will Be Able To

  • Count the clock cycles of one function built as software and as hardware.
  • Choose between the two from how often the function runs and how settled it is.
  • Say what a change costs before tape-out, and after it.
Lesson
S1.9
Time
22 min
Runs on
Your browser
Check
5 questions

A block counts the 1 bits of a word in one clock cycle, a hundred times faster than the program it replaced. A year later the rule changes, and the slow program is the one still in use.

Before You Start

You need Amdahl's law from S1.5, and the price of cells from S1.8. You need tape-out and firmware from S1.4.

Five questions this page answers
Question
1What does it mean to build a function as hardware, or as software?
2What does each way cost?
3Which functions belong in hardware?
4What does a change cost after the chip is made?
5How can hardware keep some of the freedom of software?

The Idea: The Same Function, Two Places to Put It

An SoC has a processor, so it can compute anything, given time. It also has room for blocks of logic that each do one job quickly. Every function of the product must go to one or the other.

Deciding this for every function is called the hardware and software partition. The architect makes it early, in the architecture stage of S1.4.

The two sides differ in speed, as you would expect. They differ more in something else. Software can be changed after the chip is made. Hardware cannot. So the partition also decides which mistakes can still be repaired.

New Words

New words on this page
WordMeaning
PartitionThe decision, for each function, whether a block of logic does it or the program does it.
AcceleratorA block built to do one function much faster than the processor could in software.
WorkaroundA change in software that avoids a fault in hardware.
RespinMaking a chip again from a corrected physical layout.
MaskA word whose 1 bits choose which bits of another word take part.
Processor timeThe share of its clock cycles that the processor spends on one job.

One Picture: Two Ways

One Function, Two Ways to Build ItStep through both ways. Then see what each one does when the rule changes.
  • Pads
  • Processor
  • Peripheral
  • Memory
  • Power
One Function, Two Ways to Build ItStep through both ways. Then see what each one does when the rule changes. component One function count the bits of aword that are 1 component As software a loop that theprocessor runs, oneinstruction at a time component As hardware a block of logic thatanswers in one cycle component Edit the program a change takes days,on chips already sold component Make a new chip a change takesmonths, and old chipsstay as they are 129 cycles1 cyclethe rule changesthe rule changes
  1. component One functioncount the bits of a word that are 1
    • 129 cycles → As software
    • 1 cycle → As hardware
  2. component As softwarea loop that the processor runs, one instruction at a time
    • the rule changes → Edit the program
  3. component As hardwarea block of logic that answers in one cycle
    • the rule changes → Make a new chip
  4. component Edit the programa change takes days, on chips already sold
  5. component Make a new chipa change takes months, and old chips stay as they are
  1. The function

    The answer must be the same whichever way it is built. Only the cost differs.

  2. As software

    The processor runs a loop of four instructions for each bit. It needs no new silicon, and it takes 129 cycles.

  3. As hardware

    A block adds the 32 bits side by side. It answers in one cycle, and it costs cells on every chip made.

  4. The rule changes: software

    Someone edits one number in the program and loads it again. Chips already sold can take the change.

  5. The rule changes: hardware

    The block is fixed in silicon. A change means a new physical layout, a new tape-out and new chips.

Read the diagram as text
  • One function: count the bits of a word that are 1
  • As software: a loop that the processor runs, one instruction at a time
  • As hardware: a block of logic that answers in one cycle
  • Edit the program: a change takes days, on chips already sold
  • Make a new chip: a change takes months, and old chips stay as they are
  • One function → As software: 129 cycles
  • One function → As hardware: 1 cycle
  • As software → Edit the program: the rule changes
  • As hardware → Make a new chip: the rule changes

The cycle counts are the recorded counts of the lab on this page.

The Smallest Example, Worked by Hand: Count the Ones

The function is small: count the bits of a 32-bit word that are 1. First as software. The loop looks at one bit in each pass.

The software loop, counted
PartInstructionsHow oftenClock cycles at one cycle each
Set up the loop1once1
Take the lowest bit, add it, shift, branch back432 times128
Total129

One clock cycle for each instruction is a simple model. Module S2 measures a real processor.

As hardware, 32 small adders work side by side, and the answer is ready in one cycle. With the 10 ns clock of S1.5, one call takes 10 ns in place of 1290 ns.

Is that worth a block? Amdahl's law of S1.5 says: ask for the share. It depends on how often the function runs.

The software way, at two rates of use
Calls in each secondSumProcessor time usedVerdict
1010 x 1290 ns12,900 ns of each secondNothing. Keep it in software.
100,000100,000 x 1290 ns0.129 second of each secondAbout 13 percent of the processor. A block is worth a look.

Exercise rates.

The Rules

  1. Rule 1: Any function can be built either way.

    The answer is the same. The time, the area and the freedom to change it differ.

  2. Rule 2: Hardware is paid for on every chip.

    An accelerator costs cells and power on each die made, even in products that never use it.

  3. Rule 3: Software is paid for in processor time.

    It needs no new silicon. It is worth replacing only if it takes a large share of the time.

  4. Rule 4: Software can change after tape-out, and hardware cannot.

    A program can be loaded again, even on chips already sold. A block needs a respin.

  5. Rule 5: Put in hardware what must be fast and is settled.

    Keep in software what is rare, or may still change. When both matter, split the function.

Sources for These Rules

These five rules are common practice in architecture work. They are not taken from a library source.

The share argument is Amdahl's law: Patterson and Hennessy, Computer Organization and Design, ARM edition, section 1.10.

That nothing can be edited after tape-out is rule 5 of S1.4.

Now Run It: Then Change the Rule

The lab at the end of this page builds the function both ways and counts cycles. Then it changes the rule, as a customer might a year later. Rule A counts all 32 bits. Rule B counts the low 16 bits only.

Recorded from the lab on Icarus. The word is 00F0_0113 in hexadecimal.
word=00f00113
rule A, all 32 bits.  software: ones=8 in 129 cycles
rule A, all 32 bits.  hardware: ones=8 in 1 cycle
rule B, low 16 bits.  software: ones=4 in 65 cycles (one number changed)
rule B, low 16 bits.  hardware: ones=8 in 1 cycle (the chip still counts 32 bits)

Rows 2 and 3 are rule 1 at work, and the hand count above: both ways find 8 ones. Software needs 129 cycles and hardware 1.

Row 4 is rule 4 at work. For rule B, someone changed one number in the program, from 32 to 16. The loop now finds 4 ones, and it even runs faster.

Row 5 is the other half of rule 4. The block still counts all 32 bits and still says 8. It is not broken. It does exactly what it was built to do, and that is no longer wanted.

The lab leaves things out. Its software is test code that charges one cycle for each instruction. A real call of the block also costs a bus access, as in S1.2.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
A function in softwareA block for itNo silicon, and it can change after the chips are sold. It takes processor time on every call.
A function in hardwareA loop for itA large speed-up of that function. Cells on every die, and no change after tape-out.
Hardware with a setting in a registerHardware with the rule fixedSoftware can adjust the block later. The register and its logic cost a few more cells.

The third row is the common answer. Here it would be a mask register in front of the counter. Software writes which bits to count. Rule B then needs one register write, and no respin.

The Trap: Building the Rule into the Silicon

Return to the opening. The team measured a speed-up of more than a hundred on the function and built the block. Nobody asked how settled the rule was.

The cost of that question grows with the date. Before the RTL is written, a change is a line in a document. Before tape-out, it is new RTL and new tests. After tape-out, it is a respin: months, and a new set of chips.

  1. Ask who can change the rule

    A standard that is still being written, or a customer who has not decided, means the rule is not settled.

  2. Keep the changing part in software

    Let hardware do the heavy, fixed part. Let software choose what to feed it.

  3. Leave a way out

    A register bit that switches a block off lets software take the function back.

At Scale

This page1 function
A phone chiphundreds of functions, several accelerators

This section looks ahead. The lab moved one small function. A phone chip makes this choice for hundreds of functions.

  1. Accelerators for the heavy, settled work

    Video, pictures and radio follow fixed standards and never rest. They get blocks. S10.6 follows a picture through several.

  2. Software for everything else

    Start-up, settings and rare events stay in firmware, where a fault can be repaired by an update.

  3. Workarounds are planned for

    Teams expect some hardware faults after tape-out. They add switch-off bits so that firmware can avoid a faulty block.

Try It

Run the lab. Check the count of ones in 00F0_0113 by hand.

Change the word so that its top 16 bits are 1 and the rest are 0. Predict all four rows before you run it.

Give the hardware a 32-bit mask input, and count only the bits where the mask is 1. Make rule B pass.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: word=00f00113 rule A, all 32 bits. software: ones=8 in 129 cycles rule A, all 32 bits. hardware: ones=8 in 1 cycle rule B, low 16 bits. software: ones=4 in 65 cycles (one number changed) rule B, low 16 bits. hardware: ones=8 in 1 cycle (the chip still counts 32 bits)

Check Yourself

Predict the outputThe word is now 8000_0007 in hexadecimal. What is the last line the lab prints?
// One function, built two ways: count the bits of a word that are 1.
// The software way is a loop. This model charges one clock cycle for each instruction of the loop.
// The hardware way is a block of logic that answers in one clock cycle.
module tb;
  logic clk = 0;
  always #5 clk = ~clk;
  int cycles = 0;
  always @(posedge clk) cycles++;

  // ---- The hardware: fixed when the chip is made. It counts all 32 bits. ----
  logic [31:0] hw_in;
  logic [5:0]  hw_ones;
  always @(posedge clk) begin
    hw_ones = 0;
    for (int i = 0; i < 32; i++) hw_ones = hw_ones + hw_in[i];   // 32 small adders, side by side
  end

  // ---- The software: a loop of four instructions for each bit. BITS is a number in the program. ----
  task software(input logic [31:0] word, input int BITS, output int count);
    logic [31:0] w;
    w = word; count = 0;
    @(posedge clk);                       // one instruction to set up the loop
    for (int i = 0; i < BITS; i++) begin
      @(posedge clk);                     // 1: take the lowest bit
      @(posedge clk); count += w[0];      // 2: add it to the count
      @(posedge clk); w = w >> 1;         // 3: shift the word right
      @(posedge clk);                     // 4: branch back to the top of the loop
    end
  endtask

  int sw_ones, t0;
  initial begin
    hw_in = 32'h8000_0007;
    @(posedge clk); #1;
    $display("word=%h", hw_in);
    // The first rule: count all 32 bits.
    t0 = cycles; software(hw_in, 32, sw_ones);
    #1 $display("rule A, all 32 bits.  software: ones=%0d in %0d cycles", sw_ones, cycles - t0);
    $display("rule A, all 32 bits.  hardware: ones=%0d in 1 cycle", hw_ones);
    // The rule changes after the chip is made: count the low 16 bits only.
    t0 = cycles; software(hw_in, 16, sw_ones);
    #1 $display("rule B, low 16 bits.  software: ones=%0d in %0d cycles (one number changed)", sw_ones, cycles - t0);
    $display("rule B, low 16 bits.  hardware: ones=%0d in 1 cycle (the chip still counts 32 bits)", hw_ones);
    $finish;                      // $finish ends the simulation
  end
endmodule
ConceptWhat does an accelerator cost that a software loop does not?
ConceptA function runs once at start-up and its rule may still change. Where does it belong?
ConceptWhat does a change to a hardware block cost after tape-out?
ConceptHow can a hardware block keep some freedom to change?

Common Mistakes

Wrong Build a block because it is a hundred times faster.

The speed-up of a function matters only by its share of the time.

Fix Work out the share first, with Amdahl's law.

Wrong Fix a rule in hardware while it is still being argued.

After tape-out the block cannot follow a change.

Fix Keep the rule in software, or in a register that software sets.

Wrong Call the block broken when the rule changes.

It does what its specification said.

Fix Treat it as a requirement that moved, and plan the workaround.

Remember

  • Any function can be built as hardware or as software.
  • Hardware costs cells on every die; software costs processor time.
  • Only a function with a large share of the time is worth an accelerator.
  • Software can change after tape-out; hardware needs a respin.
  • A register setting gives a hardware block part of the freedom of software.

Practice

Report a problem with this page