Question on CPU Simulator

Discussion in 'Embedded Systems and Microcontrollers' started by sciconf, Oct 1, 2006.

  1. sciconf

    Thread Starter New Member

    Oct 1, 2006
    4
    0
    Hi

    I am doing a project of simulating a CPU using my pc and Turbo C.

    I have,

    void execInst(int inst)
    {
    int opCode;
    int operand;

    opCode = inst >> 8;
    operand = inst & 0xff;

    switch (opCode) {
    // default: Handle illegal instruction here.
    case LDA:
    Cpu.a = operand;
    break;

    case STA:
    Mem[operand] = Cpu.a;
    break;
    ----
    }
    ----
    }

    What is the justification for "8" above and also for "0xff" ? Are they correct?
     
  2. sciconf

    Thread Starter New Member

    Oct 1, 2006
    4
    0
    Also when declaring
    #define STA 1

    for the switch statement, the #1" above - can it be arbitary - or should it be value of the OpCode?

    Thanks
     
  3. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    There is no "justification" for these constants. They are what they are. If your question is: "what is the purpose of those constants?", they are the second operands of the binary operators "shift right" and "bitwise AND". The semantics of what they are doing is to break the integer "inst" into two other integers "opcode" and "operand". "opcode" is the most significant byte of "inst" and "operand" is the least significant byte of "inst". If that is how the instruction set is defined then they might be correct, but how should I know that, I don't know what instruction set you are simulating.
     
Loading...