CPU와 메모리 만들기

CPU와 메모리 만들기

CPU

우리가 만들 컴퓨터 아키텍처에서 PC칩의 출력에서 나온 선은 ROM 칩의 주소 입력에 연결된다. 여기서 ROM 칩은 PC가 가리키는 명령어 메모리 위치의 값인 ROM[PC]을 계속 출력한다. 이 값은 현재 명령어(Current Instruction)라 불린다. 전반적인 컴퓨터 연산이 한 클록 주기 동안 어떻게 이루어지는지 보면 다음과 같다.

실행(Execute): 현재 명령어의 비트들은 여러 칩에 동시에 전달된다. 만약 명령어가 주소 명령어(최상위 비트=0)라면, 명렁어에 있는 15비트 상수는 A레지스터에 기록된다. 그리고 명령어가 계산명령어(최상위 비트=1)라면, 그 안의 a, c, d, j비트는 제어 비트가 되어 ALU나 레지스터가 그 명령을 수행하게 된다.

Instruction

인출(Fetch): 다음에 인출할 명령어는 현재 명령어의 점프 비트 및 ALU출력에 따라서 결정된다. 점프가 실제로 이뤄질지는 이 두 값을 같이 봐야 한다. 만약 점프가 이뤄진다면 PC는 A레지스터의 값으로 설정되고, 아닐 경우에는 PC가 1 증가한다. 다음 클록 주기에는 프로그램 계수기(PC)가 가리키는 명령어가 ROM의 출력이 되며, 다음 주기가 이어진다.

CPU 구성

CPU

CHIP CPU {

    IN  inM[16],         // M value input  (M = contents of RAM[A])
        instruction[16], // Instruction for execution
        reset;           // Signals whether to re-start the current
                         // program (reset==1) or continue executing
                         // the current program (reset==0).

    OUT outM[16],        // M value output
        writeM,          // Write to M? 
        addressM[15],    // Address in data memory (of M)
        pc[15];          // address of next instruction

    PARTS:
        // decode instruction
        // iii ccccccc ddd jjj

        Mux16(a=instruction, b=aluOut, sel=instruction[15], out=AData);

        // ARegister
        And(a=instruction[5], b=instruction[15], out=inA);
        Mux16(a=instruction, b=aluOut, sel=inA, out=Adata);

            // if instruction[15] == 1 and instruction[5] == 1
        And(a=instruction[15], b=instruction[5], out=a1);
        Not(in=instruction[15], out=a0);

        Or(a=a1, b=a0, out=a2);
        
        ARegister(in=Adata, load=a2, out=AR, out[0..14]=addressM);
        
        // if instruction[12] == 1 aluIn = M else A
        Mux16(a=AR, b=inM, sel=instruction[12], out=aluIn);

        ALU(
            x=DR,
            y=aluIn,
            zx=instruction[11],
            nx=instruction[10],
            zy=instruction[9], 
            ny=instruction[8], 
            f=instruction[7], 
            no=instruction[6],  
            out=aluOut,
            out=outM,
            zr=aluZr,
            ng=aluNg
        );

        // write to M
        And(a=instruction[3], b=instruction[15], out=writeM);        

        // write to D
        And(a=instruction[4], b=instruction[15], out=inB);
        DRegister(in=aluOut, load=inB, out=DR);

        // positive
        Or(a=aluZr, b=aluNg, out=temp);
        Not(in=temp, out=positive);

        // decide whether to jump
        And(a=positive, b=instruction[0], out=jgt);
        And(a=aluZr, b=instruction[1], out=jeq);
        And(a=aluNg, b=instruction[2], out=jlt);

        Or(a=jgt, b=jeq, out=tmp);
        Or(a=tmp, b=jlt, out=tmp1);
        And(a=instruction[15], b=tmp1, out=jump);

        // PC
        PC(in=AR, load=jump, inc=true, reset=reset, out[0..14]=pc);
}

Memory

일반적인 RAM장치의 인터페이스와 동일하고 메모리맵을 통한 CPU와 I/O 장치 사이의 통신도 맡는다.

메모리 맵: 사용자와 상호작용을 위한 주변 장치로 스크린과 키보드를 사용하고, 메모리 매핑 버퍼를 통해 컴퓨터 플랫폼과 통신한다.

Memory

CHIP Memory {
    IN in[16], load, address[15];
    OUT out[16];

    PARTS:

    DMux(in=load, sel=address[14], a=la, b=lb);
    
    RAM16K(in=in, load=la, address=address[0..13], out=oa);
    Screen(in=in, load=lb, address=address[0..12], out=ob);
    Keyboard(out=oc);
    
    Mux4Way16(a=oa, b=oa, c=ob, d=oc, sel=address[13..14], out=out);
}

Computer

기계어로 작성된 프로그램을 실행하도록 설계된 전체 컴퓨터 시스템이다. 컵퓨터 칩은 CPU, 데이터 메모리, 명령어 메모리(ROM), 스크린, 키보드 및 그 외 컴퓨터 작동에 필요한 모든 하드웨어 장치들로 구성된다.

Computer

CHIP Computer {
    IN reset;

    PARTS:

    ROM32K(address=pc, out=instruction);
    
    CPU(
        inM=inM, instruction=instruction, reset=reset,
        outM=outM, writeM=writeM, addressM=addressM, pc=pc
    );

    Memory(in=outM, load=writeM, address=addressM, out=inM);
}