Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "4241.h"
- void Chip_4241::read_instruction(){
- // 1 byte instructions
- /// These instructions preform operations on the registers, each made up of four bits.
- switch ( CELL[ pc ].SLOT ) {
- case 0: // HAULT
- HAULT = true;
- break;
- case 1: // ADD
- A.REG = A.REG + B.REG;
- pc++;
- break;
- case 2: // SUBTRACT
- A.REG = A.REG - B.REG;
- pc++;
- break;
- case 3: // INCREMENT REGISTER A
- A.REG = A.REG + 1;
- pc++;
- break;
- case 4: // INCREMENT REGISTER B
- B.REG = B.REG + 1;
- pc++;
- break;
- case 5: // DECREMENT REGISTER A
- A.REG = A.REG - 1;
- pc++;
- break;
- case 6: // DECREMENT REGISTER B
- B.REG = B.REG - 1;
- pc++;
- break;
- case 7: // SWAP REGISTERS
- unsigned char dummy = A.REG;
- A.REG = B.REG;
- B.REG = dummy;
- pc++;
- break;
- }
- /*2 byte instructions
- These instructions preform operations on the memory cell next in line.
- read_instruction() will take a two byte instruction, increase the program counter,
- manipulate this value and then increase the program counter again.
- ex:
- address
- \ _ 0 1 2
- [ 8 ] [ 6 ] [ 0 ]
- PC-^
- executes as follows:
- PC @ MEM 0: read_instruction() executes the content of CELL[0], which is 8 a two byte instruction, or print. PC increases.
- PC @ MEM 1: read_instruction() uses this value to print. PC increases.
- PC @ MEM 2: read_instruction() executes the content of CELL[2], 0. program haults.
- */
- switch ( CELL[ pc ].SLOT ){
- case 8: // PRINT <DATA>
- pc++;
- std::cout << CELL[pc].SLOT << '\n';
- pc++;
- break;
- case 9: // LOAD ADDRESS OF VALUE <DATA> INTO REGISTER A
- pc++;
- A.REG = CELL[pc].SLOT;
- pc++;
- break;
- case 10: // LOAD ADDRESS OF VALUE <DATA> INTO REGISTER B
- pc++;
- B.REG = CELL[pc].SLOT;
- pc++;
- break;
- case 11: // STORE REGISTER A INTO ADDRESS <DATA>
- pc++;
- CELL[pc].SLOT = A.REG;
- pc++;
- break;
- case 12: // STORE REGISTER B INTO ADDRESS <DATA>
- pc++;
- CELL[pc].SLOT = B.REG;
- pc++;
- break;
- case 13: // JUMP TO ADDRESS <DATA>
- pc++;
- pc = CELL[ pc ].SLOT;
- break;
- case 14: // JUMP TO ADDRESS <DATA> IF REGISTER A == 0
- pc++;
- if( A.REG == 0 )
- pc = CELL[ pc ].SLOT;
- else
- pc++;
- break;
- case 15: // JUMP TO ADDRESS <DATA> IF REGISTER A != 0
- pc++;
- if( A.REG != 0 )
- pc = CELL[ pc ].SLOT;
- else
- pc++;
- break;
- }
- }
- void Chip_4241::debug(){
- std::cout << "\nRegister A: " << this->A.REG << '\n'
- << "Register B: " << this->B.REG << '\n'
- << "Program Counter: " << this->pc <<'\n'
- << "Hault Flag: " << this->HAULT << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement