The first block of the SoC sits right next to the core: the tightly coupled memory, or TCM, which holds the program, its data and its stack, and answers in the same cycle. Next to it sits the smallest decoder in the system, which decides for every load and store whether the address belongs to the TCM or must go out on the bus. This article explains what a TCM is and why SoCs have one, how its two ports and its byte-lane writes work, and why the decode in front of it must look at exactly the right address bits. In exercise S02 you write both.

11 min read | Beginner to Intermediate

What You'll Learn

  • Explain what a TCM is and why a CPU subsystem keeps one next to the core
  • Describe a two-port memory with combinational reads and byte-lane writes
  • Write a local address decode that selects exactly the TCM's 64 KB window
  • Predict what goes wrong when a decode looks at too few address bits

Before You Start

This section needs Verilog: see the Verilog Tutorial. Memories in a System explains the kinds of memory in an SoC and where a TCM fits, and Memory Maps and Address Decoding explains decoding and aliases. Memory, Addresses and Endianness covers byte addresses, words and byte enables. No earlier exercise is needed; S01 is optional.

What a TCM Is

A tightly coupled memory is a block of on-chip memory wired straight to the core, with no bus in between. The core reaches it in a fixed, short time: in cvsoc, in the same cycle. Everything else the core uses is further away, behind the interconnect, and takes several cycles per access. That is why the program, its data and its stack live in the TCM: almost every instruction fetch and most loads and stores stay local, and only the accesses that really need a device or the shared SRAM pay for the bus. Running Programs on an SoC showed the result: in hello, every instruction fetch and 62 data accesses go to the TCM at 1 cycle each.

Real microcontrollers do the same. They call it TCM, scratchpad or closely coupled memory; On-Chip SRAM, Scratchpads and System Caches compares it with a cache. The difference that matters here is that a TCM is simply memory at a fixed address range: no tags, no misses, the same timing every time.

Two Ports, One Array

The core fetches an instruction and makes a data access in the same cycle: the instruction is in F while an older load or store is in W. One memory port could only do one of the two, so the TCM has two: an instruction port that only reads, and a data port that reads and writes. Both look into the same array of 16,384 words of 32 bits, 64 KB:

The TCM: one array of 16,384 words indexed by address bits 15 to 2, an instruction port that reads iaddr, a data port that reads daddr and writes byte lanes, and a hit decode on daddr bits 31 to 16.

These are the ports. The exercise gives them in the empty module, and the tests connect to them by name:

module cvsoc_tcm (
    input  logic        clk,
    input  logic [31:0] iaddr,     // instruction port: address
    output logic [31:0] irdata,    //   the word at iaddr, in the same cycle
    input  logic [31:0] daddr,     // data port: address
    input  logic [31:0] dwdata,    //   write data, already in its byte lanes
    input  logic [3:0]  dwstrb,    //   byte enables
    input  logic        dwe,       //   write enable (written at the clock edge, only when hit is 1)
    output logic [31:0] drdata,    //   the word at daddr, in the same cycle
    output logic        hit        // 1 when daddr is inside the TCM window, 0x0000_0000 to 0x0000_FFFF
);

Both reads are combinational: the word appears in the same cycle as its address, with no clock edge in between. That matches how the core expects its memories to behave when they are ready at once, and it is what makes a TCM access take exactly 1 cycle. A word's position in the array is its address divided by four, address bits [15:2]: byte address 0x0000_0010 is word 4.

A real chip would build the TCM from SRAM macros, which register the address at a clock edge and deliver the data in the next cycle. A core designed for such a TCM spends a stage on the access, or stalls one cycle after each load. The combinational TCM here keeps the core's pipeline unchanged; Memories in a System describes how real macros behave.

Byte-Lane Writes

A 32-bit word holds four bytes, called byte lanes: lane 0 is bits [7:0], lane 3 is bits [31:24]. A store does not always write a whole word: sb writes one byte, sh two. The core's load/store unit has already placed the data in the right lanes of dwdata and set one bit of dwstrb for each lane to be written. The TCM's job is to write exactly those lanes, at the rising clock edge where dwe is 1, and leave the others as they were.

For example, sh to address 0x0000_0012 writes the upper half of word 4. The load/store unit sends dwstrb = 4'b1100 and the halfword in dwdata[31:16]. After the clock edge, lanes 2 and 3 of word 4 hold the new value; lanes 0 and 1 are unchanged.

Quick Check

Q: Word 4 holds 0xA0B0_C0D0. The core stores with dwdata = 0x00FF_0000 and dwstrb = 4'b0100 to address 0x0000_0010. What does word 4 hold afterwards?

Show Answer

0xA0FF_C0D0. Only lane 2, bits [23:16], is written, and it takes the value of the same lane of dwdata, 0xFF. The other three lanes keep 0xA0, 0xC0 and 0xD0.

The Local Decode

Every data address the core produces either belongs to the TCM or does not. The CPU subsystem must know which, in the same cycle, because a TCM access completes at once and anything else must be sent out on the bus and waited for. The TCM tells it with one output, hit: 1 when daddr is inside the TCM's window.

The TCM's window is 0x0000_0000 to 0x0000_FFFF. Every address in it has upper half 0x0000, and every address outside it has a nonzero upper half. So hit depends on exactly the 16 upper address bits, [31:16], being zero. The lower 16 bits select the byte inside the TCM, and they do not take part in the decision.

The same hit guards the write: the TCM writes only when dwe is 1 and hit is 1. That sounds redundant, because the CPU subsystem would not send a store to the TCM if hit were 0. But dwe comes straight from the core, and the core sets it for every store, wherever it goes. A store to 0x2000_0010 in the SRAM has the same low bits as TCM word 4; without the check, the TCM would quietly write word 4 as well, and the program would find its own data changed by a store to somewhere else. This is a kind of alias, described in Memory Maps and Address Decoding.

Where Does This Matter?

Decoders that look at too few bits are a classic integration bug. A block decoded on 15 bits instead of 16 answers for half its window, or answers in a second window nobody planned for; one that ignores the upper bits appears at every multiple of its size. Teams catch these with memory-map tests that write and read every block's first and last word and the addresses just outside it, which is exactly what the S02 tests do. Top-Level Integration and Connectivity Checks describes how.

The Names the Tests Use

One more rule comes from the tests rather than from the hardware. The system tests in S08 and S16 load the program into the TCM before releasing reset, by writing straight into its storage array through the design's hierarchy. They find it as the array mem inside your cvsoc_tcm, declared as logic [31:0] mem [0:16383], word 0 at address 0. Name the array exactly that, and later name the TCM's instance u_tcm.

How It Is Tested

The S02 tests drive the TCM directly and keep a copy of what they wrote:

  • hit at the edges of the window (0x0000_0000, 0x0000_8000, 0x0000_FFFC) and outside it (0x0001_0000, 0x2000_0000, 0x4000_F000, 0xFFFF_0000);
  • word, halfword and byte writes, each read back through both ports;
  • writes to 0x2000_0010 and 0x0001_8004, which share their low bits with TCM words, followed by a check that those TCM words did not change;
  • the two ports reading two different words in the same cycle;
  • 400 random writes with random byte enables, at addresses inside and outside the window (hidden test).

The visible test runs the directed checks and prints ok or FAIL for each.

Quick Check

Q: Someone writes the decode as "address bits [31:15] all zero". Which TCM addresses does that get wrong, and which test catches it?

Show Answer

Every address from 0x0000_8000 to 0x0000_FFFF, the upper half of the TCM, where bit 15 is 1: hit is 0 there, so the CPU subsystem would send those accesses to the bus, and the stack, which starts at the top of the TCM, would break first. The hit check at 0x0000_8000 and 0x0000_FFFC catches it.

Common Mistakes

Mistake #1: Writing whenever dwe is 1

Without the hit condition, every store to the SRAM or a peripheral also lands in the TCM word with the same low address bits. Programs that store to the bus then corrupt their own variables, and the failure shows up far from its cause.

Mistake #2: Writing the whole word

Ignoring dwstrb turns every sb and sh into a word store that overwrites the neighbouring bytes with whatever dwdata carries in the other lanes, usually copies of the same byte.

Mistake #3: Reading the instruction port with the data address

With two addresses in the same cycle it is easy to index both reads with one of them. Most tests that read one address through both ports still pass; the check where the two ports read different words in the same cycle does not.

Your Turn in the Lab

Exercise S02: write cvsoc_tcm with the ports above, the array mem, two combinational reads, byte-lane writes and the hit decode. Save it in your lab project; S08 uses it inside the CPU subsystem.

Open exercise S02 in ChipVerify Lab

Summary

  • A TCM is memory wired straight to the core with fixed, short timing; cvsoc's 64 KB TCM holds the program, data and stack and answers in the same cycle.
  • It has an instruction port that only reads and a data port that reads and writes, both combinational, into one array of 16,384 words indexed by address bits [15:2].
  • A write changes only the byte lanes whose dwstrb bit is 1, at the clock edge where dwe and hit are both 1.
  • hit is 1 exactly when address bits [31:16] are zero; looking at fewer or other bits misplaces half the window or creates aliases.
  • The array must be named mem, because the system tests load programs into it directly.
Did you find what you were looking for?

Quiz

No quiz questions available for this article.