Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MOVE 0
- #define ADDA 1
- #define SUB 2
- #define BRA 3
- #define CMP 4
- #define BEQ 5
- #define BNE 6
- #define EXG 7
- #define ADD 9
- #define STOP 15
- void main(void) {
- unsigned short int PC = 0; // program counter
- unsigned short int D0 = 1; // data register 0
- unsigned short int D1 = 2; // data register 1
- unsigned short int A0 = 5; // adress register
- unsigned short int CCR = 0; // condition code register
- unsigned short int MAR; // memory address register
- unsigned short int MBR; // memory buffer register
- unsigned short int IR; // Instruction register
- unsigned short int operand; // the 8-bit operand from the IR
- unsigned short int source; // source operand
- unsigned short int destination; // destination value
- unsigned short int opcode; // the 4-bit op-code from the IR
- unsigned short int amode; // the 2-bit addressing mode
- unsigned short int direction; // the 1-bit direction flag
- unsigned short int memory[256]={0}; // the memory
- unsigned short int run = 1; // excution of program
- unsigned short int x=0;
- unsigned short int l=0;
- unsigned short int z=0;
- /* Instruction format:
- /* 7 6 5 4 3 2 1 0
- /* Bit 1 and 0 : 2-bit address mode
- 00 address mode = absolute
- 01 address mode = literal
- 10 address mode = indexed
- 11 address mode = relative
- /* Bit 2 : 1-bit direction (source/operand)
- /* Bit 3 : not used
- /* Bit 4 to 7 : 4-bit instruction code
- /* main loop */
- while (run)
- {
- memory[0]=0b00010001;
- memory[1]=2;
- memory[2]=10;
- //memory[3]=2;
- memory[5]=6;
- memory[8]=16;
- MAR = PC; // PC to MAR
- PC = PC + 1; // Increment PC
- MBR = memory[MAR]; // get next instruction
- IR = MBR; // copy MBR to IR
- opcode = IR; // store the op-code bits
- MAR = PC; // PC to MAR
- PC = PC + 1; // Increment PC
- MBR = memory[MAR]; // get the operand
- IR = MBR; // copy MBR to IR
- operand = IR; // store the operand bits
- amode = opcode & 0x03; // extract the address mode bits
- direction = (opcode & 0x04) >> 2;
- /* get data direction
- 0 = register to memory
- 1 = memory to register */
- opcode = opcode >> 4 ;
- run = 0;
- }// behery touch
- // get the 4-bit instruction code
- x=memory[A0];
- l=memory[D0];
- z=memory[x+l];
- printf("Opcode = %d\n",opcode);
- printf("amode = %d\n",amode);
- printf("Direction = %d\n",direction);
- /* use the address mode to get the source operand */
- printf("z before overwrite =%d\n",z);
- //printf("Memory[D1] before overwrite = %d\n",memory[D1]);
- switch (amode) {
- case 0: {
- source = memory[operand];
- break;
- } // Absolute
- case 1: {
- source = operand;
- break;
- } // Literal
- case 2: {
- source = memory[A0 + operand];
- break;
- } // Indexed
- case 3: {
- source = memory[PC + operand];
- break;
- } // PC relative
- }
- /* now execute the instruction */
- switch (opcode) {
- case MOVE: {
- if (direction == 0)
- {z=x+4;
- printf("The Result of Move = %d\n",destination);}
- else
- D0 = source;
- if (D0 == 0)
- CCR = 1;
- else
- CCR = 0;
- printf("The CCR = %d\n",CCR);
- break;
- }
- case ADDA: {
- if (direction == 0) {
- z=x+4 ;
- printf("The Result of z after overwrite = %d\n",z);
- if (destination == 0)
- CCR = 1;
- else
- CCR = 0;
- } else {
- D0 = l + source;
- printf("The Result of Add of D0 = %d\n",D0);
- if (D0 == 0)
- CCR = 1;
- else
- CCR = 0;
- }
- printf("The CCR = %d\n",CCR);
- break;
- }
- case SUB: {
- if (direction == 0) {
- destination = D0 - source;
- printf("The Result of Sub = %d\n",destination);
- if (destination == 0)
- CCR = 1;
- else
- CCR = 0;
- } else {
- D0 = D0 - source;
- printf("The Result of Sub of D0 = %d\n",D0);
- if (D0 == 0)
- CCR = 1;
- else
- CCR = 0;
- }
- printf("The CCR = %d\n",CCR);
- break;
- }
- case BRA: {
- if (amode == 0)
- {PC = operand;
- printf("The Result of BRA = %d\n",PC);}
- if (amode == 1)
- {PC = PC + operand;
- printf("The Result of BRA = %d\n",PC);}
- break;
- }
- case CMP: {
- MBR = D0 - source;
- printf("The result of CMP = %d\n",MBR);
- if (MBR == 0)
- CCR = 1;
- else
- CCR = 0;
- printf("The CCR = %d\n",CCR);
- break;
- }
- case BEQ: {
- if (CCR == 1) {
- printf("There is CCR\n");
- if (amode == 0)
- {PC = operand;
- printf("PC = %d",PC);}
- if (amode == 1)
- {PC = PC + operand;
- printf("PC = %d",PC);}
- }
- break;
- }
- case BNE: {
- if (CCR != 1) {
- printf("There is no CCR\n");
- if (amode == 0)
- {PC = operand;
- printf("PC = %d",PC);}
- if (amode == 1)
- {PC = PC + operand;
- printf("PC = %d",PC);}
- }
- break;
- }
- case EXG: {
- MBR = D0;
- D0 = A0;
- A0 = MBR;
- printf("A0 = %d\n",A0);
- break;
- }
- case ADD: {
- if (direction == 0) {
- memory[D1] = memory[D1] + memory[l] ;
- printf("The Result of Add of memory[D1] after overwrite = %d\n",memory[D1]);
- if (memory[D1] == 0)
- CCR = 1;
- else
- CCR = 0;
- } else {
- memory[l] = memory[l] + source;
- printf("The Result of Add of D0 = %d\n",D0);
- if (D0 == 0)
- CCR = 1;
- else
- CCR = 0;
- }
- printf("The CCR = %d\n",CCR);
- break;
- }
- case STOP: {
- printf("STOPPED");
- run = 0;
- break;
- }
- }
- /* save result in memory if register to memory */
- if (direction == 0)
- switch (amode) {
- case 0: {
- memory[operand] = destination;
- //printf("%d",destination);
- break;
- } // Absolute
- case 1: {
- //printf("%d",destination);
- break;
- } // Literal
- case 2: {
- memory[A0 + operand] = destination;
- break;
- } // Indexed
- case 3: {
- memory[PC + operand] = destination;
- break;
- } // PC relative
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement