cpu/cpu.c
// Handle system calls by routing them to standard I/O functions.
// If the OS should handle the calls instead,
// set NATIVE_TRAP_HANDLING to 1.
Handle system calls by routing them to standard I/O functions.
If the OS should handle the calls instead,
set NATIVE_TRAP_HANDLING to 1.
Handle system calls by routing them to standard I / O functions . If the OS should handle the calls instead set NATIVE_TRAP_HANDLING to 1 .
void CPU_HandleTrap(struct CPU_State *state, int trap)
{
    int offset;
    switch(trap){
    case 0x20: 
        state->reg[0] = getchar();
        break;
    case 0x21: 
        printf("%c", state->reg[0] & 0xff);
        break;
    case 0x22: 
        offset = state->reg[0];
        while(state->mem[offset] != 0 && offset < 0x10000){
            printf("%c", state->mem[offset] & 0xff);
            offset++;
        }
        break;
    case 0x23: 
        printf("?");
        char console_input;
        scanf("%c", &console_input);
        state->reg[0] = console_input;
        break;
    case 0x24: 
        offset = state->reg[0];
        while(state->mem[offset] != 0 && offset < 0x10000){
            printf("%c%c", state->mem[offset] & 0xff, (state->mem[offset] >> 8) & 0xff);
            offset++;
        }
        break;
    case 0x25: 
        state->mem[MCR] = 0; 
        break;
    default: 
        fprintf(stderr, "ERROR: called unknown trap routine %x", trap); 
        break;
    }
    state->pc = state->reg[7];
}
void CPU_HandleTrap ( struct CPU_State * state , int trap ) { int offset ; switch ( trap ) { case 0x20 : state -> reg [ 0 ] = getchar ( ) ; break ; case 0x21 : printf ( " " , state -> reg [ 0 ] & 0xff ) ; break ; case 0x22 : offset = state -> reg [ 0 ] ; while ( state -> mem [ offset ] != 0 && offset < 0x10000 ) { printf ( " " , state -> mem [ offset ] & 0xff ) ; offset ++ ; } break ; case 0x23 : printf ( " " ) ; char console_input ; scanf ( " " , & console_input ) ; state -> reg [ 0 ] = console_input ; break ; case 0x24 : offset = state -> reg [ 0 ] ; while ( state -> mem [ offset ] != 0 && offset < 0x10000 ) { printf ( " " , state -> mem [ offset ] & 0xff , ( state -> mem [ offset ] >> 8 ) & 0xff ) ; offset ++ ; } break ; case 0x25 : state -> mem [ MCR ] = 0 ; break ; default : fprintf ( stderr , " " , trap ) ; break ; } state -> pc = state -> reg [ 7 ] ; }
Handle system calls by routing them to standard I/O functions.
Handle system calls by routing them to standard I / O functions .
// GETC - read character into R0 without prompt // OUT - write character in R0 to display // PUTS - write string to screen. // IN - read single character into R0 with prompt // PUTSP - write string (using two characters for each memory location)  // HALT // set the MCR to 0, stopping the computer // illegal system call