Practice
Chocolate Box Splitter
A chocolate shop runs a small packing bench. After each batch, a counter shows how many chocolates came out, and the packer types in how many boxes the order needs. Until now the packers split each batch by eye. Boxes went out uneven, and a hotel client counted 11 chocolates in a box sold as 12.
Your task: build the divider block for the new panel. Sample total and boxes on the rising edge where start is 1 and busy is 0. Hold busy high while you divide: 8 clock cycles, one quotient bit per clock cycle. Then pulse done for 1 cycle. Put the chocolates per box on quotient. Put the leftover count on remainder. Raise dbz together with done when the sampled boxes was 0.
Interface
| Port | Direction | Type | Description |
|---|---|---|---|
clk | in | 1 bit | Clock |
rst | in | 1 bit | Synchronous reset, active high |
start | in | 1 bit | Request a division |
total | in | 8-bit unsigned | Chocolates in the batch (the dividend) |
boxes | in | 8-bit unsigned | Boxes to fill (the divisor) |
busy | out | 1 bit | High while a division is running |
done | out | 1 bit | High for 1 cycle when the result is ready |
dbz | out | 1 bit | High with done when the sampled boxes was 0 |
quotient | out | 8-bit unsigned | Chocolates per box |
remainder | out | 8-bit unsigned | Chocolates left over |
Behavior
- A rising edge where
startis 1 andbusyis 0 accepts a job.totalandboxesare sampled on that edge. busyrises on the accepting edge and stays high for exactly 8 cycles. Each of those cycles produces one quotient bit.busyfalls anddonerises on the same edge, 8 cycles after the accepting edge.donestays high for exactly 1 cycle.- While
doneis high:quotientholdstotaldivided byboxes, rounded down.remainderholds the rest, soquotient * boxes + remainder = total. - If the sampled
boxesis 0:quotientis 255,remainderequals the sampledtotal, anddbzrises withdone, with the same timing. dbzis 0 withdonefor every job whose sampledboxeswas not 0.dbzis 0 whilebusyis high: it rises, if at all, on the same edge asdone.- After the
donecycle,quotient,remainder, anddbzhold their values until the next acceptedstartor a reset. startis ignored whilebusyis high. The running job keeps its sampled operands.- A new
startis accepted on the edge that ends thedonecycle, so jobs can run back to back. rsthigh at a rising edge clearsbusy,done,dbz,quotient, andremainderon that same edge.
What the bench checks
busyanddoneare sampled every cycle of every job: 8 busy cycles, then a 1-cycledonepulse, never early, never late.quotient,remainder, anddbzare checked whiledoneis high, and checked as cleared to 0 after each reset.- One cycle after
done,quotient,remainder, anddbzmust still hold their checked values. - During every job's 8 busy cycles,
dbzmust stay low; it may rise only withdone. - Results are compared against the true split for exact jobs, jobs with leftovers,
totalsmaller thanboxes,boxesof 1, and 255 at both ports. - A job where the compare lands exactly equal mid-run must still divide correctly.
- Jobs with
boxesof 0 must raisedbzwithquotient255 andremainderequal tototal. - A
startpulse with new operands in the middle of a run must not change the running job's result. - A second job started on the
donecycle of the first must produce a correct result. - A 2-cycle mid-run reset is checked right after its first edge: a synchronous reset clears every output in 1 cycle.
Constraints
Build the division serially: shift, compare, subtract, one quotient bit per clock cycle. Do not use /, mod, rem, or % on the data path.
everything is synchronous to the rising edge of clk, and every output comes from a register. The bench reads outputs shortly after each edge.
this block only divides. The counter feeding total, the keypad behind boxes, and the panel display are all out of scope.
Do not add ports.
Click Run to execute your code. Output will appear here.