eForth1  v2.4
eforth_vm.h
Go to the documentation of this file.
1 
26 #ifndef __EFORTH_VM_H
27 #define __EFORTH_VM_H
28 #include "eforth_core.h"
29 
30 namespace EfVM {
33 extern PGM_P _rom;
34 extern U8 *_ram;
35 extern Stream *io;
36 #define RAM_FLAG 0xe000
38 #define IDX_MASK 0x07ff
39 #define IRET_FLAG 0x8000
40 #define BOOL(f) ((f) ? TRUE : FALSE)
41 #define RAM(i) &_ram[(i) - FORTH_RAM_ADDR]
42 U8 BGET(U16 d) {
46  return (U8)((d&RAM_FLAG) ? _ram[d&IDX_MASK] : pgm_read_byte(_rom+d));
47 }
51 U16 GET(U16 d) {
52  return (d&RAM_FLAG)
53  ? ((U16)_ram[d&IDX_MASK]<<8) | _ram[(d+1)&IDX_MASK]
54  : ((U16)pgm_read_byte(_rom+d)<<8) | pgm_read_byte(_rom+d+1);
55 }
56 #define BSET(d, c) (_ram[(d)&IDX_MASK]=(U8)(c))
57 void SET(U16 d, U16 v) {
58  BSET(d, v>>8);
59  BSET(d+1, v&0xff);
60 }
61 #define S2D(h, l) (((S32)(h)<<16) | ((l)&0xffff))
62 #define DEPTH() ((DU)((U8*)DS - RAM(FORTH_STACK_ADDR)) >> 1)
66 #define PUSH(v) { *++DS = top; top = (v); }
67 #define RPUSH(v) { *--RS = rtop; rtop = (v); }
68 #define POP() (top = *DS--)
69 #define RPOP() (rtop = *RS++)
70 #define DTOP(d) { *DS = (d)&0xffff; top = (d)>>16; }
71 
75 #if EXE_TRACE
76 int tCNT;
77 int tTAB;
78 int constexpr opEXIT = 1;
83 int constexpr opENTER = 2;
84 int constexpr opDOLIT = 6;
85 int constexpr opEXEC = 8;
86 
87 #define DEBUG(s,v) if (tCNT) printf((s),(v))
88 #define TAB() if (tCNT) { \
89  LOG("\n"); \
90  for (int i=0; i<tTAB; i++) LOG(" "); \
91 }
92 void TRACE(U8 op, U16 ip, U16 w, U16 top, S16 s)
93 {
94  if (!tCNT) return;
95  // indent call depth
96  if (op==opENTER) {
97  TAB();
98  tTAB++;
99  }
100  // display IP:W[opcode]
101  LOG_H(" ", ip - 1);
102  LOG_H(":", w);
103  LOG_H("[", op); LOG("]");
104  // dump stack
105  for (int i=0; i<s; i++) {
106  LOG_H("_", *((DU*)RAM(FORTH_STACK_ADDR) + (i+1)));
107  }
108  LOG_H("_", top);
109  LOG("_");
111  switch (op) {
112  case opDOLIT: LOG_H("$", GET(ip)); LOG(" "); break;
113  case opEXIT: LOG(";"); --tTAB; break;
114  case opEXEC: w = top;
115  case opENTER:
116  for (--w; (BGET(w) & 0x7f)>0x20; w--);
117  int len = BGET(w++) & 0x1f;
118  for (int i=0; i<len; i++, w++) {
119  LOG_C((char)BGET(w));
120  }
121  LOG(" :");
122  break;
123  }
124 }
125 #else
126 
127 #define DEBUG(s,v)
128 #define TAB() /* skip */
129 #define TRACE(op, ip, w, t, s) /* skip */
130 
131 #endif // EXE_TRACE
132 
134 }; // namespace EfVM
135 
136 #endif // __EFORTH_VM_H
EfVM::io
Stream * io
Stream IO interface.
Definition: eforth_vm.cpp:33
EfVM::top
DU top
ALU (i.e. cached top of stack value)
Definition: eforth_vm.cpp:20
RAM
#define RAM(i)
Definition: eforth_vm.h:41
eforth_core.h
eForth prototype and interface
LOG_H
#define LOG_H(s, n)
Definition: eforth_core.h:23
EfVM::BGET
U8 BGET(U16 d)
Definition: eforth_vm.h:45
EfVM::SET
void SET(U16 d, U16 v)
Definition: eforth_vm.h:57
IDX_MASK
#define IDX_MASK
Definition: eforth_vm.h:38
EfVM::GET
U16 GET(U16 d)
Definition: eforth_vm.h:51
LOG_C
#define LOG_C(c)
Definition: eforth_core.h:21
EfVM::_rom
PGM_P _rom
ROM, Forth word stored in Arduino Flash Memory.
Definition: eforth_vm.cpp:26
S16
int16_t S16
16-bit signed integer
Definition: eforth_config.h:25
LOG
#define LOG(s)
Definition: eforth_core.h:20
EfVM::_ram
U8 * _ram
RAM, memory block for user define dictionary.
Definition: eforth_vm.cpp:27
TAB
#define TAB()
Definition: eforth_vm.h:128
RAM_FLAG
#define RAM_FLAG
Definition: eforth_vm.h:37
BSET
#define BSET(d, c)
Definition: eforth_vm.h:56
U16
uint16_t U16
16-bit unsigned integer
Definition: eforth_config.h:21
TRACE
#define TRACE(op, ip, w, t, s)
Definition: eforth_vm.h:129
FORTH_STACK_ADDR
#define FORTH_STACK_ADDR
Definition: eforth_config.h:60
DU
S16 DU
data/cell unit
Definition: eforth_config.h:29
EfVM
Definition: eforth_vm.cpp:12
U8
uint8_t U8
8-bit unsigned integer
Definition: eforth_config.h:22