Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define TO_HEX(i) (i <= 9 ? '0' + i : 'a' - 10 + i)
- //char * toHex(int val);
- //void print_format(int * registers[], int regcc);
- //int set_cc(int value);
- int main(int argc, char**argv) {
- short *buffer;
- unsigned long fileLength;
- int regs[11] = {0};
- int rego;
- int opcode;
- int pc = 1;
- int regcc = 0;
- unsigned short offset;
- printf("Initial state\n");
- print_format(regs, regcc);
- printf("==================\n");
- printf("after executing instruction");
- FILE*f;
- f = fopen(argv[1], "r");
- fseek(f, 0, SEEK_END);
- fileLength = ftell(f);
- fseek(f, 0, SEEK_SET);
- buffer = malloc(fileLength + 1);
- fread(buffer, fileLength, 1, f);
- fclose(f);
- int i = 0;
- for (i = 0; i < sizeof (buffer); i++) {
- buffer[i] = ((buffer[i] & 0x00FF) << 8 | ((buffer[i] & 0xFF00) >> 8));
- }
- regs[8] = buffer[0];
- for (i = 1; i < sizeof (buffer); i++) {
- pc++;
- opcode = (buffer[i] & 0xf000) >> 12;
- if (opcode != 15) {
- if (opcode == 2) {
- regs[8] = regs[8] + 1;
- rego = (buffer[i] & 14) >> 12;
- offset = buffer[i] & 0x01ff;
- regs[rego] = buffer[pc + offset];
- regs[9] = buffer[i];
- regcc = set_cc(regs[rego]);
- break;
- }
- }
- }
- print_format(regs, regcc);
- printf("==================\n");
- }
- int set_cc(int value) {
- if (value < 0) {
- return -1;
- }
- if (value == 0x0000) {
- return 0;
- }
- if (value > 1) {
- return 1;
- }
- }
- char * toHex(int val) {
- int x = val;
- char *res = (char *) malloc(sizeof (char) * 5);
- if (x <= 0xFFFF) {
- res[0] = TO_HEX(((x & 0xF000) >> 12));
- res[1] = TO_HEX(((x & 0x0F00) >> 8));
- res[2] = TO_HEX(((x & 0x00F0) >> 4));
- res[3] = TO_HEX((x & 0x000F));
- res[4] = '\0';
- }
- return res;
- }
- void print_format(int * registers[], int regcc) {
- int i = 0;
- for (i; i < 11; i++) {
- if (i == 8) {
- printf("PC\t%s\n", toHex(registers[8]));
- } else if (i == 9) {
- printf("IR\t%s\n", toHex(registers[9]));
- } else if (i == 10) {
- if (regcc == 0x0000) {
- printf("CC\t%s\n", "Z");
- }
- if (regcc < 0) {
- printf("CC\t%s\n", "N");
- }
- if (regcc > 0) {
- printf("CC\t%s\n", "P");
- }
- } else {
- printf("R%d\t0x%s\n", i, toHex(registers[i]));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement