New instructions PEEK and POKE to call outside routines.

To use PEEK and POKE initialize uBASIC from your program with
ubasic_init_peek_poke(program, &peek, &poke) where "peek" is a
function peek(VARIABLE_TYPE addr) that returns VARIABLE_TYPE
and "poke" is a procedure void poke(VARIABLE_TYPE addr,
VARIABLE_TYPE value).

Use "PEEK addr, var" to set the value of the variable var to the
result of calling peek(addr). Use "POKE addr, value" to call
poke(addr, value);

The argument name "addr" is arbitrary; it doesn't mean you should
necessarily use it to pass your routines a memory address.
This commit is contained in:
Danyil Bohdan 2013-12-23 18:46:25 +02:00
parent f9ea3c6e6b
commit 93bb242eb0
6 changed files with 85 additions and 2 deletions

22
tests.c
View file

@ -68,6 +68,22 @@ rem 120 print a, b\n\
140 next i\n\
160 end\n";
static const char program_peek_poke[] =
"10 peek 100 + 20 + 3, a\n\
20 peek 123, z\n\
30 poke 100 - 1, 99\n\
40 poke 0, 0\n\
50 end\n";
/*---------------------------------------------------------------------------*/
VARIABLE_TYPE peek(VARIABLE_TYPE arg) {
return arg;
}
/*---------------------------------------------------------------------------*/
void poke(VARIABLE_TYPE arg, VARIABLE_TYPE value) {
assert(arg == value);
}
/*---------------------------------------------------------------------------*/
void run(const char program[]) {
@ -81,7 +97,7 @@ void run(const char program[]) {
start_t = clock();
ubasic_init(program);
ubasic_init_peek_poke(program, &peek, &poke);
do {
ubasic_run();
@ -109,6 +125,10 @@ main(void)
run(program_fibs);
assert(ubasic_get_variable(1) == 89);
run(program_peek_poke);
assert(ubasic_get_variable(0) == 123);
assert(ubasic_get_variable(25) == 123);
return 0;
}
/*---------------------------------------------------------------------------*/