The basics of chip simulation on C


The following common code snippets demonstrates, how to simulate any chip design, down to its transistor level.


This is huge structure, holding all internal state of simulated chip.

It has two types of variables: control lines and latches.

Control lines are just current level of some internal wire or external input/output pad. Analog I/O simulated by floating-point variables.

Latches are representing current state of static (floating base capacity) or dynamic (bistable) latches.

Example 8 KB ROM memory device context:

typedef struct ContextROM

В  В  unsigned char ROM[8*1024]; В  В // array of latches

В  В  int В  В  CS; В  В  // Chip Select (Active low)

В  В  int В  В  init; В  // 0 when first run (load ROM from file)

В  В  // Buses
В  В  char В  В ADDR[13], DATA[8];
} ContextROM;


To run simulation you simply need to call Step().

Internal Step() procedures will simulate chip blocks one-by-one, according to their propagation delay and internal context.

Example 8 KB ROM simulation.

void StepROM ( ContextROM * rom )

В  В  unsigned char data = 0;
В  В  unsigned short addr = 0;
В  В  int i;

В  В  if (!rom->init) {
В  В  В  В  LoadROM (rom); В  В // load from file
В  В  В  В  rom->init = 1;
В  В  }

В  В  if (rom->CS == 0) { В // Read data when chip selected (active low)
В  В  В  В  for (i=0; i<13; i++) addr |= (rom->ADDR[i] << i);
В  В  В  В  data = rom->ROM[addr & 0x1fff];
В  В  }

В  В  for (i=0; i<8; i++) { В  // put data onto data bus
В  В  В  В  rom->DATA[i] = (data >> i) & 1;
В  В  }