Cpu Simulator



  • Hallo leute,
    Bräuchte bei prog eure hilfe. Muss ein CPU simultator programmieren weiss nicht wie ich das angehen soll! hoffe ihr könnt mir da weiterhelfen!!

    wir haben eine text datei vorgegeben und dazu muss ich ein prog.schreiben das diese punkte erfüllt:

    1)remove all comments,

    2)replace all constants with their values and remove the definition lines containing the constants,

    3)map the commands to address locations evaluating and replacing the .org commands (the entire address range is linear covering 2^9 address locations),

    4)replace the labels within the code with addresses and replace the variable names with their locations that were assigned in the previous step.

    5)Finally write the initial values of the variables to the according locations.

    ; a simple program that adds two constants using a sub-routine and stores
    ; the result in a variable vc
    ;
    ca .equ 5 ; define a constant ca with value 5
    cb .equ 7 ; define a constant cb with value 7

    .org 128
    vc .dw 0 ; define a variable vc at address 128 (= 0x80) with value 0

    .org 0
    _main:
    lda,c ca ; load constant ca to accu
    push,r a ; push accu to the stack
    lda,c cb ; load constant cb to accu
    push,r a ; push accu to the stack
    call sum ; call sub-routine sum
    sta vc ; vc = accu
    inc,r sp,2 ; deallocate parameter stack
    jmp _end ; jmp to label end
    _sum:
    push,r fp ; save old framepointer
    ldr fp,sp ; mark current frame
    dec,r sp,1 ; space for temp below the fp
    lda,c 0 ; accu = 0
    sta,ri fp,-1 ; temp = accu
    lda,ri fp,2 ; accu = 7 (via stack)
    add,ri fp,3 ; accu = 7 + 5 (via stack)
    sta,ri fp,-1 ; temp = accu
    lda,ri fp,-1 ; accu = temp
    ldr sp,fp ; remove locals from stack
    pop,r fp ; restore fp of caller
    ret ; back to caller
    _end:
    .end

    so soll die neue datei dann ausschauen:

    0x000:
    0x002:
    0x004:
    0x006:
    0x008:
    0x010:
    0x012:
    0x014:
    0x016:
    0x018:
    0x020:
    0x022:
    0x024:
    0x026:
    0x028:
    0x030:
    0x032:
    0x034:
    0x036:
    0x038:
    0x040:
    ...
    0x080:
    lda,c 5
    push,r a
    lda,c 7
    push,r a
    call 0x016
    sta 0x080
    inc,r sp,2
    jmp 0x040
    push,r fp
    ldr fp,sp
    dec,r sp,1
    lda,c 0
    sta,ri fp,-1
    lda,ri fp,2
    add,ri fp,3
    sta,ri fp,-1
    lda,ri fp,-1
    ldr sp,fp
    pop,r fp
    ret
    .end
    ...
    0

    Mfg
    Kebapmaster



  • CPUs zu emulieren ist nicht einfach. Du kannst du aber QEMU anschauen. Da gibt es viele CPUs, die dort nachgebaut haben.



  • Also deiner Beschreibung nach, sieht es für mich ehr so aus, als sollst du einen Assembler und keinen Emulator schreiben. Wo genau ist denn jetzt dein Problem? So wie es ausschaut, sollst du nur die entsprechenden Labels durch absolute Adressen ersetzen. Das ist also nur ein kleiner Teil, eines Assemblers, den du umsetzen muss.

    Du musst also die Befehle einlesen, eine Sprungmarkentabelle aufbauen und dann die Sprungmarken ersetzen.



  • das ganze wird aus einer datei gelesen?

    FILE *inputfile = fopen("filename.txt", "r");
    fseek(inputfile, SEEK_END);
    unsigned length = ftell(inputfile);
    fssek(inputfile, SEEK_SET);
    
    char *buffer = malloc(length + 1);
    fread(buffer, 1, length, inputfile);
    fclose(inputfile);
    buffer[length] = 0;
    
    char *result = malloc(length + 1);
    char *ptr1 = buffer, *ptr2 = result;
    while (*ptr1)
    {
        if (*ptr1 == ';') // kommentar
            while (*ptr1 && *ptr1 != '\n')
                ++ptr1;
        else
            *ptr2++ = *ptr1++;
    }
    ptr2[1] = 0;
    
    // hier noch andere sachen machen
    
    FILE *outputfile = fopen("result.txt", "w");
    fwrite(result, strlen(result), 1, outputfile);
    fclose(outputfile);
    free(buffer);
    free(result);
    

    so das ist ein grundgerüst, was schonmal alle kommentare entfernt

    für die konstanten würde ich alle werte in einen dynamischen array
    speichern und beim lesen erstzen

    MfG helferlein


Anmelden zum Antworten