All pastes #940931 Raw Edit

Mine

public text v1 · immutable
·published 2008-03-13 12:11 UTC
rendered paste body
[quote=CarlChatfield,Thur 13 Mar. 22:02]Heres mine. EDIT: hehehe ive discovered a bug. O well, only occurs when all 16 bytes are used. Not of concern for me :p, might fix it later.
[code]#include <cstdlib>
#include <iostream>

using namespace std;

int program[16] = {  0,  0,  0,  0,
                     0,  0,  0,  0,
                     0,  0,  0,  0,
                     0,  0,  0,  0 };
...
[/quote][/quote]
Carl, your programming style could be improved a lot, not only by removing the switch completely, but also by a few things such as :

 * All instructions will increment IP, and load into IS etc. So might as well do this once, not separately for each instruction.

 * You  have a lot of '& 0x0F' a lot. Now what if it suddenly became a 5 bit machine! This makes your code harder to maintain. You should use a function to do this.

 * Notice how you kept using program[IP]. It would be nice to have a variable (perhaps... IS as defined in the machine!)

Here is a snippet from my emulator which I wrote last year. This follows the style guidelines, and you will notice that without switch, it is a lot neater.

[code]
      IS = memory[IP];

      if(IS<8){
         IP++;
      } else {
         ISS = lowerValue(IP + 1);
         ISS = memory[ISS];
         IP = IP + 2;
      }
      IP = lowerValue(IP);

      //now actually execute IS
      if(IS == 1){
         R0 = lowerValue(R0 + R1);
      } else if (IS == 2){
         R0 = lowerValue(R0 - R1);
      } else if (IS == 3){
         R0++;
         R0 = lowerValue(R0);
      } else if (IS == 4){
         R1++;
         R1 = lowerValue(R1);
      } else if (IS == 5){
         R0--;
         R0 = lowerValue(R0);

...etc
[/code]