G8.8.1 Two Banks, Two Requests

MediumImplementIcarus · browser samples / server checksReview the topic

Serve two ports from two single-port banks, grant both when they want different banks, and alternate the winner of a conflict.

Hardware context: A two-port memory for a small processor built from two single-port banks, interleaved on the low address bit. Build a banked memory that serves two requests per cycle and stalls the loser of a bank conflict fairly.

A small processor fetches and stores through two ports in the same cycle. Its memory has two single-port banks of 16 words. Each bank can do one read or one write per cycle.

This is an Implement problem. Write two_port_banked. Even words live in bank b0 and odd words in bank b1. Serve both ports when they want different banks.

When both want the same bank, only one is served. The other port keeps asking. To stay fair, the port that lost the last conflict wins the next one.

The test checks both grants in every cycle, the word of every read, and finally every word inside the two banks.

Signals in the Timeline

SignalWidthRole
gnts64input of each check
Full specification
The module you write (keep the name, the ports and the bank instances b0 and b1)
module two_port_banked (
  input  logic       clk,
  input  logic       rst,                // synchronous, active high
  input  logic       req_a, we_a,        // port A: a request, and 1 for a write
  input  logic [4:0] addr_a,
  input  logic [7:0] wdata_a,
  output logic       gnt_a,              // 1: port A is served at the next rising edge
  output logic [7:0] rdata_a,            // the word of a read, in the cycle after the edge that served it
  input  logic       req_b, we_b,        // port B: the same
  input  logic [4:0] addr_b,
  input  logic [7:0] wdata_b,
  output logic       gnt_b,
  output logic [7:0] rdata_b
);
  // two bank16x8 instances named b0 (even words) and b1 (odd words), and your logic
endmodule
The bank, given (do not copy it into your answer)
// bank16x8: one single-port bank of 16 words of 8 bits (a model). At a rising edge with en = 1 it
// writes wdata (we = 1) or reads the word into rdata (we = 0). rdata holds until the next read.
module bank16x8 (input logic clk, en, we, input logic [3:0] row, input logic [7:0] wdata, output logic [7:0] rdata);
  logic [7:0] mem [16];
  always @(posedge clk) if (en) begin if (we) mem[row] <= wdata; else rdata <= mem[row]; end
endmodule
RuleDefinition
PlaceWord w is in bank w[0], at row w[4:1].
Grantgnt is 1 in the cycle before the edge that serves the request. A port with req = 0 gets gnt = 0.
ConflictBoth ports ask and want the same bank. Exactly one is granted.
WinnerPort A wins the first conflict after reset. After that, the loser of each conflict wins the next one. A grant without a conflict does not change this.
Read datardata of a port is the word in the cycle after the edge that served its read.
ResetAt a rising edge with rst = 1, port A becomes the winner of the next conflict.

Each test fills the banks, resets your module, and gives each port 8 operations. A port asks for its next operation in the cycle after it was granted, or after one or two idle cycles. The test runs 32 cycles. The result has three fields.

FieldMeaning
wrong_grantsHow many cycles had a grant pair different from the rule.
wrong_readsHow many reads returned a wrong word.
wrong_wordsHow many of the 32 words in the banks are wrong at the end.

Examples

Example 1

Neighbouring Words
Input
A: r0 r2 r4 r6 r8 r10 r12 r14 | B: r1 r3 r5 r7 r9 r11 r13 r15
Output
wrong_grants = 0, wrong_reads = 0, wrong_words = 0, cycles = 32

Port A reads even words and port B odd words. They never want the same bank, so both are granted in every cycle and finish in 8 cycles.

Example 2

The Same Bank Every Time
Input
A: r0 r2 r4 r6 r8 r10 r12 r14 | B: r16 r18 r20 r22 r24 r26 r28 r30
Output
wrong_grants = 0, wrong_reads = 0, wrong_words = 0, cycles = 32

Both ports read even words. Every cycle is a conflict. A fair memory alternates: A, B, A, B. Both finish after 16 cycles.

Example 3

Writes, Then Reads of the Other Port
Input
A: w4=44 w5=55 w6=66 w7=77 r9 r10 r11 r12 | B: r4 r5 r6 r7 r20 r21 r22 r23
Output
wrong_grants = 0, wrong_reads = 0, wrong_words = 0, cycles = 32

Port A writes words 4 to 7 while port B reads them. Whether B sees the old or the new word depends on which edge served each one.

Constraints and Contract

  • The inputs are known 0/1 values. They change while the clock is low. A port holds its request until it is granted.
  • Keep the module name, the ports, and the two bank instances named b0 and b1. The test reads their arrays.

Hints

Hint 1

Start with the grants: compare the low address bits, then let a one-bit register decide who wins a conflict. Flip it only in a cycle with a conflict.

Hint 2

Drive each bank from the granted port that wants it. A port that is not granted must not touch any bank.

Hint 3

A read word appears one edge later. Remember which bank each port used at that edge, and pick rdata from that bank.

What the grader checks: Six directed and eight seeded scripts of 8 operations per port, with idle cycles between some operations, run for 32 cycles. Grants and read words are compared with a JavaScript model in every cycle. At the end, all 32 words are read from the banks. More than two ports, more banks and write-to-read forwarding are outside this problem.

14 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 1 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Neighbouring Words

Input
A: r0 r2 r4 r6 r8 r10 r12 r14 | B: r1 r3 r5 r7 r9 r11 r13 r15
Expected output
wrong_grants = 0, wrong_reads = 0, wrong_words = 0, cycles = 32

Port A reads even words and port B odd words. They never want the same bank, so both are granted in every cycle and finish in 8 cycles.

Run samples executes all 3 public checks. Check all tests runs 14 checks on the server.

Icarus · browser samples / server checks