V13.3.1 The Model Side of a Transactor

MediumImplement35 minVerilator + your C over DPI · serverReview the topic

Write the C half of a hybrid's bus link. The hardware side owns every clock edge and drives the real transactor; your code never waits. It hands over one request at a time and is told each reply.

Hardware context: The hybrid: ap_iss with RTL peripherals behind a synthesizable AHB transactor. The model side of a transactor.

In the hybrid, the model core's accesses to devices cross a transactor into RTL. On this site's DPI engine, as across an emulator's link, the C side may not wait for clock edges. So the link is turned inside out. The hardware side asks the model for its next request, runs it through the transactor, and hands back the reply.

Write model.c with the four functions of model.h. The model runs a list of register steps from programs.c: writes, reads, polls and read-modify-writes. model_next hands over the next bus request; model_reply takes its result. Keep count of the requests and fold every value an OP_READ returns into an XOR. Stop at the first ERROR, or at a poll that runs out.

The hardware side in main.sv is the hybrid's own transactor (tools/soc/fast-platforms/transactor.sv), unchanged, in front of a small AHB target. Each case runs one program on a freshly reset target and grades your two results.

Full specification
model.h (given)
/* model.h: the model side of the transactor. The hardware side (main.sv) calls these four functions. */
#ifndef MODEL_H
#define MODEL_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
enum { OP_END = 0, OP_WRITE = 1, OP_READ = 2, OP_POLL = 3, OP_SET = 4 };
/* OP_WRITE addr, a = data, b = size in bytes (1, 2 or 4)      OP_READ addr, b = size in bytes
   OP_POLL  addr (a word), a = mask, b = value: read until (data & mask) == value, at most MAX_POLLS reads
   OP_SET   addr (a word), a = bits to set, b = bits to clear: read the word, then write (old & ~b) | a      */
typedef struct { uint32_t op, addr, a, b; } step_t;
#define MAX_POLLS 20
const step_t *program(int which);              /* the server's steps; the last one is OP_END */

/* What you write. Each runs in zero time: the hardware side owns every clock edge. */
void model_start(int which);                   /* a fresh run of program(which) */
/* The next request: 1 and the request, or 0 when the program is over (or stopped).
   size is the AHB HSIZE code: 0 a byte, 1 a half, 2 a word. value: the write data, right-justified. */
int model_next(uint32_t *addr, uint32_t *size, uint32_t *write, uint32_t *value);
void model_reply(uint32_t data, uint32_t error);  /* the reply to the last request: read data, right-justified */
/* 0: the XOR of every value the OP_READ steps returned. 1: (requests << 8) | stop, where stop is 0 when the
   program ran to OP_END, else the number (from 1) of the step that met an ERROR or ran out of polls. */
uint32_t model_result(int which);
#ifdef __cplusplus
}
#endif
#endif

StepRequests it makesWhat the model keeps
OP_WRITE addr, data, bytesone write; size code 0, 1 or 2 for 1, 2 or 4 bytesnothing
OP_READ addr, bytesone read of that sizethe value, XORed into result 0
OP_POLL addr, mask, valueword reads until (data & mask) == value; at most MAX_POLLSnothing; out of polls stops the program
OP_SET addr, set, clearone word read, then one word write of (old & ~clear) | setnothing
Target addressBehaviour
0x00 to 0x3Cregisters with byte lanes; 0x20 to 0x3C add one wait state
0x40 STATUSbit 0 reads 1 from the K-th read after a write of K to GO, and stays 1
0x44 GOa write of K arms STATUS
anything else, a misaligned half or wordERROR
The hardware side of one case (in main.sv)
  import "DPI-C" function void model_start(input int which);
  import "DPI-C" function int model_next(output int unsigned a, output int unsigned s, output int unsigned w, output int unsigned v);
  import "DPI-C" function void model_reply(input int unsigned data, input int unsigned error);
  import "DPI-C" function int unsigned model_result(input int which);
  task automatic run_program(input int which);
    resetn = 0; repeat (2) @(negedge clk); resetn = 1;
    model_start(which);
    while (requests < 400 && model_next(a, s, w, v) != 0) begin
      @(negedge clk); addr = a; size = s[2:0]; write_req = w[0]; value = v; start = 1;
      @(negedge clk); start = 0;
      while (!done) @(negedge clk);
      model_reply(result, {31'b0, error});
    end
  endtask

Examples

Example 1

Words Written and Read Back
Input
6 steps
Output
xor = 0xbf2a66ec, requests = 6, stop = 0

Three writes and three reads: six requests, and the XOR of the three words read back.

Example 2

Bytes and Halves
Input
7 steps
Output
xor = 0x1234b98d, requests = 7, stop = 0

A byte and a half land in their lanes; the reads of a half and two bytes come back right-justified.

Example 3

Wait for Ready
Input
5 steps
Output
xor = 0xcaff, requests = 7, stop = 0

GO arms STATUS for the third read: the poll takes three requests, then a write and two reads follow.

Constraints and Contract

  • One file, model.c, with the four functions of model.h. No waiting, no threads, no output.
  • A request that meets ERROR ends the program: return 0 from model_next from then on.
  • Count every request you hand over, including the one that met ERROR.

Hints

Hint 1

An OP_SET is two requests. Which one comes first, and where do you keep the old value?

Hint 2

The size you hand over is the AHB code, not a number of bytes.

Hint 3

A poll is one step but many requests. Where do you count the tries?

What the grader checks: Each case runs one program through the real transactor and target and checks your XOR, your request count and your stop number. It cannot see how many clock edges each request took; the hardware side counts those and the page explains them.

8 checks on Verilator + your C over DPI · server. 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 3 problems on this path

Report a problem with this page

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

Words Written and Read Back

Input
6 steps
Expected output
xor = 0xbf2a66ec, requests = 6, stop = 0

Three writes and three reads: six requests, and the XOR of the three words read back.

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

Verilator + your C over DPI · server