Page 106 - Mechatronic Systems Modelling and Simulation with HDLs
P. 106
5.4 CO-SIMULATION BY SOFTWARE COMPILATION 95
address given in the direct addressing is a port represented in the memory area
or a memory location in the main memory. In the first case the addressed port is
accessed via the routine fetch_io. In the second case the accumulator c1->ac
is set to the value of the memory cell at which the byte points to the opcode. It
should also be mentioned that the pointer c1 points to the current context. A type
declaration of the context is located at the start of the C routine. Then the cycle
counter is incremented by 3 and the programme counter by 2. Finally, the affected
flags are updated and the current time t_cur calculated. The two other commands
shown are processed in a similar manner.
The jsr instruction describes the call of a subroutine, so that the return address
is initially stored on the stack in two bytes. Then the address of the subroutine is
calculated from the two bytes following the opcode and entered into the programme
counter. Then the cycle counter is incremented and the current time calculated.
Finally there is a jump to the label hub at which the large switch instruction
initiates a jump to the correct label. This diversion is necessary because in C it
is not generally possible to jump to a variable destination by means of a goto
command.
Finally, the bra instruction includes the calculation of a relative jump, which can
also be in a backwards direction. The second byte of this instruction — the width
of the jump — should thus be regarded as a signed number, which is expressed in
the appropriate C instruction. After the normal incrementation of the cycle counter
and the calculation of the time the actual jump again takes place via the hub.
typedef struct context {
/* Programme counter (pc), Accumulator (ac), Index register
(ix), Stack pointer (sp), Flag register (cc), Cycle
counter (cyc), Main memory (m) ...*/
unsigned int ac, ix, sp, pc, cc, cyc, ...;
unsigned int m[MEMORYSIZE];
...
} CONTEXT;
static CONTEXT con1, con2, *c1=&con1, *c2=&con2;
software_sim(t_start, t_stop ... ) ... ; {
if (t_start > 0.0) {
if (t_start ss< t_cur_old) {
/* t_cur_old = Time when routine was last left,
Replay! ... */
exchange_context(&c1,&c2);
...
}
copy_context(c1,c2);
...
goto hub;
}
else {