Skip to content
Browse files

Clifford Wolf:

	Fixed return from interrupt



git-svn-id: http://svn.clifford.at/embedvm/trunk@40 9d21f824-f436-4d2e-9c88-bb158a3a422f
  • Loading branch information...
1 parent 74aa3c0 commit 2a56ad242700583ac45c3763f91cde825367383d clifford committed
Showing with 24 additions and 6 deletions.
  1. +3 −0 README.VM
  2. +19 −6 vmsrc/embedvm.c
  3. +2 −0 vmsrc/evmdemo.c
View
3 README.VM
@@ -54,6 +54,9 @@ pops its operands from the stack and pushes the result).
The return address for the current function is stored in the 16-bit word
on SFP + 2 and the parent stack frame pointer at SFP + 4.
+All variables on the stack must be aligned on even addresses. So the stack
+pointer must be initialized to an even address (LSB not set).
+
Instruction Encoding
--------------------
View
25 vmsrc/embedvm.c
@@ -109,7 +109,10 @@ extern void embedvm_exec(struct embedvm_s *vm)
vm->sp = vm->sfp;
vm->ip = embedvm_pop(vm);
vm->sfp = embedvm_pop(vm);
- embedvm_push(vm, a);
+ if ((vm->sfp & 1) != 0)
+ vm->sfp &= ~1;
+ else
+ embedvm_push(vm, a);
break;
case 0x9d:
embedvm_pop(vm);
@@ -117,8 +120,13 @@ extern void embedvm_exec(struct embedvm_s *vm)
break;
case 0x9e:
addr = embedvm_pop(vm);
- embedvm_push(vm, vm->sfp);
- embedvm_push(vm, vm->ip + 1);
+ if (vm->mem_read(vm->ip+1, false, vm->user_ctx) == 0x9d) {
+ embedvm_push(vm, vm->sfp | 1);
+ embedvm_push(vm, vm->ip + 2);
+ } else {
+ embedvm_push(vm, vm->sfp);
+ embedvm_push(vm, vm->ip + 1);
+ }
vm->sfp = vm->sp;
vm->ip = addr;
break;
@@ -141,8 +149,13 @@ extern void embedvm_exec(struct embedvm_s *vm)
break;
case 0xa2:
case 0xa3:
- embedvm_push(vm, vm->sfp);
- embedvm_push(vm, vm->ip);
+ if (vm->mem_read(vm->ip, false, vm->user_ctx) == 0x9d) {
+ embedvm_push(vm, vm->sfp | 1);
+ embedvm_push(vm, vm->ip + 1);
+ } else {
+ embedvm_push(vm, vm->sfp);
+ embedvm_push(vm, vm->ip);
+ }
vm->sfp = vm->sp;
vm->ip = addr;
break;
@@ -265,7 +278,7 @@ extern void embedvm_exec(struct embedvm_s *vm)
void embedvm_interrupt(struct embedvm_s *vm, uint16_t addr)
{
- embedvm_push(vm, vm->sfp);
+ embedvm_push(vm, vm->sfp | 1);
embedvm_push(vm, vm->ip);
vm->sfp = vm->sp;
vm->ip = addr;
View
2 vmsrc/evmdemo.c
@@ -105,6 +105,8 @@ int main(int argc, char **argv)
while (!stop) {
if (vm.ip == 0xffff) {
printf("Main function returned => Terminating.\n");
+ if (vm.sp != 0 || vm.sfp != 0)
+ printf("Unexpected stack configuration on program exit: SP=%04x, SFP=%04x\n", vm.sp, vm.sfp);
fflush(stdout);
break;
}

0 comments on commit 2a56ad2

Please sign in to comment.
Something went wrong with that request. Please try again.