본문 바로가기
Problem Solving/BOJ

백준- 16506 CPU

by 채니_ 2021. 3. 7.

www.acmicpc.net/problem/16506

 

16506번: CPU

디지털하드웨어설계 과목의 최종 프로젝트는 16-bit CPU를 설계하고 Verilog 언어로 구현하는 것이다. 본인이 구현한 CPU가 제대로 동작하는지 테스트하기 위해서는 기계어 코드를 입력으로 주어야

www.acmicpc.net


문제

문제에서 주어진 조건을 그대로 구현하기만 하면 되는 문제

 

machine[] 이라는 길이 16짜리 배열에

1. rD 삽입 ( 6~8 index )

2. rA 삽입 ( 9~11 index )

3. rB 혹은 #C 삽입 ( 12~15 index )

4. opcode 삽입 ( 0~3 index )


Operation 에 맞는 값을 가져오기 위해 Hashmap을 사용하였다.

(value, key)


package study.week7;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class BJCPU {

	public static void main(String[] args) throws FileNotFoundException {
		System.setIn(new FileInputStream("input/cpu.txt"));
		Scanner sc = new Scanner(System.in);
		String [] op_4_0 = {"ADD","SUB","MOV","AND","OR","NOT","MULT","LSFTL","LSFTR","ASFTR","RL","RR"};
		String [] op_4_1 = {"ADDC","SUBC","MOVC","ANDC","ORC","MULTC","LSFTLC","ASFTRC","RLC","RRC"};
		//값을 가져오기 위해
		HashMap<String,Integer> map = new HashMap<>();
	        map.put("ADD",0);map.put("ADDC",0);
	        map.put("SUB",1);map.put("SUBC",1);
	        map.put("MOV",2);map.put("MOVC",2);
	        map.put("AND",3);map.put("ANDC",3);
	        map.put("OR",4);map.put("ORC",4);
	        map.put("NOT",5);
	        map.put("MULT",6);map.put("MULTC",6);
	        map.put("LSFTL",7);map.put("LSFTLC",7);
	        map.put("LSFTR",8);map.put("LSFTRC",8);
	        map.put("ASFTR",9);map.put("ASFTRC",9);
	        map.put("RL",10);map.put("RLC",10);
	        map.put("RR",11);map.put("RRC",11);
	        
		int N =sc.nextInt();
		
		for (int i = 0; i < N; i++) {
			int [] machine = new int [16]; //처음에 다 0000 으로 초기화
			String opcode = sc.next();
			int rD=sc.nextInt();
			int rA=sc.nextInt();
			int BC=sc.nextInt(); //rB 혹은 #C
			int idx=0;
			machine[4]=1;//일단 1로 초기화
			//op_4_0배열 안에 있을 시 0으로 바꿔줌.
			for (int j = 0; j < op_4_0.length; j++) {
				if(op_4_0[j].equals(opcode)) machine[4]=0;
			}//end for
			
			//rD
			String strD = Integer.toBinaryString(rD);
			idx = 8;
			for (int j = strD.length(); j >= 1; j--) {
				machine[idx--] = Integer.parseInt(strD.substring(j-1,j));
			}//end rD
			
			//rA
			if(rA!=0) {//0이 아닐경우
				String strA = Integer.toBinaryString(rA);
				idx = 11;
				for (int j = strA.length(); j >= 1; j--) {
					machine[idx--] = Integer.parseInt(strA.substring(j-1,j));
				}
			}//end rA
			
			//B or C
			if(machine[4]==0) {//rB를 쓰는 경우 B는 0~7이기때문에 앞에 0을 하나 붙여줌.
				String strB = Integer.toBinaryString(BC);
				machine[15]=0;
				idx = 14;
				for (int j =  strB.length(); j >=1; j--) {
					machine[idx--] = Integer.parseInt(strB.substring(j-1,j));
				}
			}
			if(machine[4]==1){//C를 쓰는 경우
				String strC = Integer.toBinaryString(BC);
				idx = 15;
				for (int j = strC.length(); j >=1; j--) {
					machine[idx--] = Integer.parseInt(strC.substring(j-1,j));
				}	
				
			}
			//operation저장
			String OP = Integer.toBinaryString(map.get(opcode));
			idx = 3;
			for (int j =  OP.length(); j >=1; j--) {
				machine[idx--] = Integer.parseInt(OP.substring(j-1,j));
			}
			for (int j = 0; j < machine.length; j++) {
				System.out.print(machine[j]);
			}System.out.println();
		}
		
	}
}

'Problem Solving > BOJ' 카테고리의 다른 글

[백준]- 1759 암호 만들기  (0) 2021.03.16
[백준] 14888번 연산자 끼워넣기  (0) 2021.03.14
백준- 8911 거북이  (0) 2021.03.07
백준- 13300 방 배정  (0) 2021.02.22
백준 - 10163 색종이  (0) 2021.02.22

댓글