| 1 |
4 |
ja_rd |
/**
|
| 2 |
|
|
@file b51_cpu.h
|
| 3 |
|
|
@brief CPU model.
|
| 4 |
|
|
|
| 5 |
|
|
This file includes the CPU simulation model. Excludes the behavior of the
|
| 6 |
|
|
CPU peripherals, which is done in b51_mcu.c.
|
| 7 |
|
|
|
| 8 |
|
|
When implementing different CPU core models, you should modify this file
|
| 9 |
|
|
by adding conditional code and new functions. Differences between CPU
|
| 10 |
|
|
cores are going to be small enough -- cycle counts, implementation of
|
| 11 |
|
|
certain instructions, etc.
|
| 12 |
|
|
|
| 13 |
|
|
Different cores will have a different set of peripherals, though. That must
|
| 14 |
|
|
be modelled with a function pointer table against different b51_mcu
|
| 15 |
|
|
implementations -- meaning one different source file per different core.
|
| 16 |
|
|
|
| 17 |
|
|
TODO CPU & MCU polymorphism to be done.
|
| 18 |
|
|
*/
|
| 19 |
|
|
|
| 20 |
|
|
#ifndef S51_H_INCLUDED
|
| 21 |
|
|
#define S51_H_INCLUDED
|
| 22 |
|
|
|
| 23 |
|
|
#include <stdint.h>
|
| 24 |
|
|
#include <stdbool.h>
|
| 25 |
|
|
|
| 26 |
|
|
#include "b51_mcu.h"
|
| 27 |
|
|
#include "b51_log.h"
|
| 28 |
|
|
|
| 29 |
|
|
|
| 30 |
|
|
/*-- Configuration macros ----------------------------------------------------*/
|
| 31 |
|
|
|
| 32 |
|
|
/** Size of IRAM in bytes */
|
| 33 |
|
|
#define MAX_IDATA_SIZE (256)
|
| 34 |
|
|
|
| 35 |
|
|
|
| 36 |
|
|
/*-- Public data types & macros ----------------------------------------------*/
|
| 37 |
|
|
|
| 38 |
|
|
|
| 39 |
|
|
/**
|
| 40 |
|
|
MCS51 CPU SFRs.
|
| 41 |
|
|
FIXME should only include the CPU SFRs and not the peripherals.
|
| 42 |
|
|
Note that ACC, while accessible as an SFR, is not in this struct and is
|
| 43 |
|
|
handled separately.
|
| 44 |
|
|
*/
|
| 45 |
|
|
typedef struct cpu51_sfr_s {
|
| 46 |
|
|
uint8_t b;
|
| 47 |
|
|
uint8_t dph;
|
| 48 |
|
|
uint8_t dpl;
|
| 49 |
|
|
uint8_t ie;
|
| 50 |
|
|
uint8_t ip;
|
| 51 |
|
|
uint8_t p0;
|
| 52 |
|
|
uint8_t p1;
|
| 53 |
|
|
uint8_t p2;
|
| 54 |
|
|
uint8_t p3;
|
| 55 |
|
|
uint8_t pcon;
|
| 56 |
|
|
uint8_t psw;
|
| 57 |
|
|
uint8_t sbuf;
|
| 58 |
|
|
uint8_t scon;
|
| 59 |
|
|
uint8_t sp;
|
| 60 |
|
|
uint8_t tcon;
|
| 61 |
|
|
uint8_t th0;
|
| 62 |
|
|
uint8_t th1;
|
| 63 |
|
|
uint8_t tl0;
|
| 64 |
|
|
uint8_t tl1;
|
| 65 |
|
|
uint8_t tmod;
|
| 66 |
|
|
} cpu51_sfr_t;
|
| 67 |
|
|
|
| 68 |
|
|
|
| 69 |
|
|
typedef struct cpu51_options_s {
|
| 70 |
|
|
bool bcd;
|
| 71 |
|
|
} cpu51_options_t;
|
| 72 |
|
|
|
| 73 |
|
|
|
| 74 |
|
|
/**
|
| 75 |
|
|
CPU object. This is the CPU model, which includes the peripherals block
|
| 76 |
|
|
and the XDATA and XCODE memory blocks as a member object (struct field).
|
| 77 |
|
|
*/
|
| 78 |
|
|
typedef struct cpu51_s {
|
| 79 |
|
|
uint8_t idata[MAX_IDATA_SIZE]; /**< IDATA RAM */
|
| 80 |
|
|
uint16_t pc; /**< PC -- addr of instruction being run */
|
| 81 |
|
|
uint8_t a; /**< ACC */
|
| 82 |
|
|
|
| 83 |
|
|
cpu51_sfr_t sfr; /**< CPU core (non-peripheral) SFRs */
|
| 84 |
|
|
mcu51_t mcu; /**< MCU peripherals model */
|
| 85 |
|
|
uint32_t cycles; /**< Clock cycles since last reset */
|
| 86 |
|
|
bool max_cycle_count; /**< Last instr. used the max # of cycles */
|
| 87 |
|
|
bool implemented_as_nop; /**< Last instr. was decoded as NOP */
|
| 88 |
|
|
|
| 89 |
|
|
log51_t log; /**< Logger data */
|
| 90 |
|
|
|
| 91 |
|
|
uint16_t breakpoint; /**< Address of breakpoint */
|
| 92 |
|
|
|
| 93 |
|
|
cpu51_options_t options; /**< Core implementation options */
|
| 94 |
|
|
} cpu51_t;
|
| 95 |
|
|
|
| 96 |
|
|
|
| 97 |
|
|
/*-- Public functions --------------------------------------------------------*/
|
| 98 |
|
|
|
| 99 |
|
|
/**
|
| 100 |
|
|
Initialize CPU model, including peripheral models.
|
| 101 |
|
|
This does not reset the CPU or the peripherals.
|
| 102 |
|
|
Use at least once before calling any other function in this API.
|
| 103 |
|
|
|
| 104 |
|
|
@arg cpu CPU model.
|
| 105 |
|
|
*/
|
| 106 |
|
|
extern void cpu_init(cpu51_t *cpu);
|
| 107 |
|
|
|
| 108 |
|
|
/**
|
| 109 |
|
|
Simulate a CPU reset, which includes the peripherals too.
|
| 110 |
|
|
|
| 111 |
|
|
@arg cpu CPU model.
|
| 112 |
|
|
*/
|
| 113 |
|
|
extern void cpu_reset(cpu51_t *cpu);
|
| 114 |
|
|
|
| 115 |
|
|
/**
|
| 116 |
|
|
Run a number of CPU instructions.
|
| 117 |
|
|
Execution will stop after running num_inst instructions, or if a
|
| 118 |
|
|
single-instruction infinite loop is detected, or if a breakpoint is hit.
|
| 119 |
|
|
|
| 120 |
|
|
@arg cpu CPU model.
|
| 121 |
|
|
@arg num_inst Number of instructions to be run.
|
| 122 |
|
|
@return 0 if execution timed out,
|
| 123 |
|
|
1 if it was interrupted,
|
| 124 |
|
|
2 for breakpoints.
|
| 125 |
|
|
*/
|
| 126 |
|
|
extern uint32_t cpu_exec(cpu51_t *cpu, uint32_t num_inst);
|
| 127 |
|
|
|
| 128 |
|
|
/**
|
| 129 |
|
|
Load object code onto XCODE memory.
|
| 130 |
|
|
Every object byte that fits the XCODE memory will be loaded onto it.
|
| 131 |
|
|
Note that bytes out of memory bounds will be dropped silently.
|
| 132 |
|
|
|
| 133 |
|
|
@arg cpu CPU model.
|
| 134 |
|
|
@arg hex_filename Nam of Intel-HEX file to be loaded.
|
| 135 |
|
|
@return Number of bytes read from HEX file -- some may have been dropped!.
|
| 136 |
|
|
*/
|
| 137 |
|
|
extern uint16_t cpu_load_code(cpu51_t *cpu, const char *hex_filename);
|
| 138 |
|
|
|
| 139 |
|
|
/**
|
| 140 |
|
|
Add a new breakpoint at given address.
|
| 141 |
|
|
The only reasons this function might fail (returning false) are:
|
| 142 |
|
|
-# Breakpoint address out of XCODE bounds.
|
| 143 |
|
|
-# Too many breakpoints.
|
| 144 |
|
|
|
| 145 |
|
|
There's no way to delete breakpoints, and the logic is still weak so this
|
| 146 |
|
|
must be considered a stub.
|
| 147 |
|
|
|
| 148 |
|
|
@arg cpu CPU model.
|
| 149 |
|
|
@arg address Breakpoint address.
|
| 150 |
|
|
@return True if the breakpoint could be added.
|
| 151 |
|
|
*/
|
| 152 |
|
|
extern bool cpu_add_breakpoint(cpu51_t *cpu, uint16_t address);
|
| 153 |
|
|
|
| 154 |
|
|
|
| 155 |
|
|
#endif // S51_H_INCLUDED
|