25æ¥ç®: èªä½ã³ã³ãã¤ã©ãèªä½ã¨ãã¥ãèªä½CPUã®çµå
ãã®è¨äºã¯ã²ã¨ãã§CPUã¨ã¨ãã¥ã¬ã¼ã¿ã¨ã³ã³ãã¤ã©ãä½ã Advent Calendar 2017ã®25æ¥ç®ã®è¨äºã§ãã
ããããæçµæ¥ã
ã¾ã復ç¿ãå ¼ãã¦ãä»ã¾ã§ã®è¨äºãæ¯ãè¿ããªãããCè¨èªããåå°ä½ã¾ã§CPUã®ä¸ãæ©ãã
æå¾ã«èªä½ã³ã³ãã¤ã©ãèªä½ã¨ãã¥ã¬ã¼ã¿ãèªä½CPUãçµåããå帰é¢æ°ã§ãã£ããããæ°ãè¨ç®ããã
25æ¥éã®å¾©ç¿
ã¢ããã³ãã«ã¬ã³ãã¼ã® åæ¥ã®è¨äº ã«
ãæ®æ®µç®ã«ããæ½è±¡çãªããã°ã©ã ã³ã¼ããããCPUå ã®é»å§å¤åãæ³åã§ããããã«ãªããããã
ã¨æ¸ããã®ã§ãä»ã¾ã§ã®è¨äºãè¦è¿ãã¤ã¤ãCè¨èªããåå°ä½ã¾ã§ã¬ã¤ã¤ã¼ãéãã¦ã¿ãã
Cè¨èªããã¢ã»ã³ãã©ã¾ã§ (18æ¥ç®-24æ¥ç®)
Cè¨èªã®ãµã³ãã«ã³ã¼ã
int main () { int a ; a = 3 ; return 0 ; }
ããã1é±éããã¦ã¢ã»ã³ãã©ã«å¤æããã ä½ã£ãã³ã³ãã¤ã©ã®ãªãã¸ããªã¯ããã
(18æ¥ç®) ã³ã³ãã¤ã©æ¦è«
Cè¨èªããã¢ã»ã³ãã©ã¸ã®å¤æã®æ¦è¦ãã¾ã¨ããã
(19æ¥ç®) åå¥è§£æ
BNFã¨å¼ã°ãããä»£å ¥å¯è½ãªæ£è¦è¡¨ç¾ãç¨ãã¦å±æ§ãå®ç¾©ãã表ãå°å ¥ããã ããã使ã£ã¦ã½ã¼ã¹ã³ã¼ãã«å«ã¾ããå ¨åèªã«å±æ§ãä»ããã
(20-21æ¥ç®) æ§æ解æ
ãã¼ã¯ã³ã®åãæ§æ解æãã¦è§£ææ¨ã«å¤æããã æ§æ解æã«ç¨ããBNFã¨ãå帰éä¸ã¢ã«ã´ãªãºã ã§çæããã解ææ¨ãè²¼ã£ã¦ããã
(22æ¥ç®) æ½è±¡æ§ææ¨
åæ¥ã®è§£ææ¨ãããåé·ãªæ å ±ãåé¤ããæ½è±¡æ§ææ¨ãä½ã£ãã
(23æ¥ç®) ã·ã³ãã«ãã¼ãã«
åæ¥ã®æ½è±¡æ§ææ¨ããã宣è¨ç³»ã®ãã¼ããã¾ã¨ãããã¼ãã«ãä½ã£ãã
(24æ¥ç®) ã³ã¼ãçæ
æ½è±¡æ§ææ¨ã¨ã·ã³ãã«ãã¼ãã«ãããx86ã®ã¢ã»ã³ãã©ã®ã³ã¼ããä½ã£ãã
ã¾ã以ä¸ã®ãããªãã³ãã¬ãç¨æããã
æ½è±¡æ§ææ¨ããã³ãã¬ã«çªã£è¾¼ãã§çæããããã¢ã»ã³ãã©ã®ã³ã¼ãã¯
bits 32 org 0x0 jmp _main_ _main_: push ebp mov ebp, esp sub esp, 0x10 mov eax, 0x3 ... (以ä¸ç¥) ...
ããã§ã³ã³ãã¤ã©ã®å é¨å¦çãä¸éã追ããã¨ãã§ããã
ããã§æ¬æ¥ã¯ãã¢ã»ã³ãã©ã®ã³ã¼ããx86ã®æ©æ¢°èªã«å¤æããããã®ãªã³ã«ãç¨æããªããã°ãªããªãã ãããå¶ä½æ義ãè¦ãã ããªãã£ãã®ã§ãgccã®ãªã³ã«ã使ããã¨ã«ãããèªä½ããããã°ãâã®è¡¨ãè¦ãã°ç°¡åã«ä½ããã
ã¢ã»ã³ãã©ãããåä½ãã¾ã§ (11æ¥ç®-17æ¥ç®)
- ã³ã³ãã¤ã«ã§å¾ãããx86ã®ã¢ã»ã³ãã©ã®ã³ã¼ããæ©æ¢°èª(ãã¤ããª)ã«å¤æããæ¹æ³
- ãããããx86ã®ã¢ã»ã³ãã©ã¯ã©ãããè¨èªãªã®ã
- x86ã®ã¨ãã¥ã¬ã¼ã¿ãä½ã
- x86ãFPGAã§å®è£ ãã
ãããã®åé¡ã¨æ ¼éããã
(11æ¥ç®) x86ã®æ¦è¦
x86ã®ã¢ã»ã³ãã©ãæ©æ¢°èªã«å¤æããããã®è¡¨ãä½ã£ãã
çµå±ã以ä¸ã®æ©æ¢°èªãå¾ãããã
eb 00 55 89 e5 83 ec 10 b8 03 00 00 00 50 89 45 fc b8 00 00 00 00 50 58 eb 00 f4
(12æ¥ç®) æ°å¤ã®æ±ã
x86ã§æ°å¤ãæ±ãéã®æ³¨æç¹ãã¾ã¨ããããªãã«ã¨ã³ãã£ã¢ã³ãæ°æã¡æªãã£ãã
(13æ¥ç®) ModRM
ã誰ã¨èª°ã足ããããæå®ããããã®ãªãã·ã§ã³ã説æããã
(14æ¥ç®) é¢æ°å¼åº
é¢æ°å¼åºã«ã¯call
ãleave
ãret
ã®3å½ä»¤ãç¨ããã®ã ã£ãã
ãããã¯ã¹ã¿ãã¯ã¡ã¢ãªã¸ã®push
ã¨pop
ãããã¦mov
ã¨jmp
å½ä»¤ã®çµã¿åããã«éããªãã£ãã
(15æ¥ç®) x86ã¨ãã¥ã¬ã¼ã¿ã®è£½ä½éå§
ãªãã¸ããªã¯ããã åæ¥ãªã®ã§å¤§æ ãä½ã£ããã¡ã¢ãªå°å³ãéè¦ã ã£ãã
(16æ¥ç®) x86ã¨ãã¥ã¬ã¼ã¿ãå®æ
æ¨æ¥ä½ã£ã大æ ããå¼ã³åºããããæ©æ¢°èªã®è§£éå¨ãä½ã£ããå ¨ä½ãå®æããã
(17æ¥ç®) x86 CPUãFPGAã§å®è£
ãªãã¸ããªã¯ããã å®éã«ä½ã£ãã®ã¯ã¢ããã³ãã«ã¬ã³ãã¼ã®éå§ç´åã ãã¤ãã¿ã¼ãããºã£ã¦é©ããã
twitter.comæ¨æ©ããx86-80386ã®CPUãFPGAã§èªä½ãã¦ããã ãã©ãããããMOVå½ä»¤ãåããããã¨åæ¥ããã°ããªãã®éã®å½ä»¤ãå®è£ ã§ããæ°ããããFPGAæ £ãã¦ããã#ä½ã¬ã¤ã¤ pic.twitter.com/L7oZSvx4aF
— 解çç¥ (@kaitou_ryaku) 2017å¹´11æ25æ¥
以ä¸ã®å·¥ç¨ã§ãèªä½ã³ã³ãã¤ã©ã¨ãx86風ã®èªä½CPUãå®æããããã®è¨äºã®ä¸çªä¸ã§ãããã2ã¤ãçµã¿åããã¦åä½ãããã
ç¬èªå½ä»¤ã»ããã®CPUã®è£½ä½(8æ¥ç®-10æ¥ç®)
âã§x86風ã®CPUãå®æããããã ããããã¯ããªãè¤éã§ä½ãã®ã大å¤ã ã£ãã ããã§ä»åã®ã¢ããã³ãã«ã¬ã³ãã¼ã§ã¯ãx86ã«åãæããåã«ããããã¿ã¤ãã¨ãã¦ç°¡åãªå½ä»¤ã»ããã®CPUãä½ã£ãã®ã ã£ãã
(8æ¥ç®) å½ä»¤ã»ããã®çå®
mov
, add
, sub
, cmp
, push
, pop
, jmp
, jz
, jnz
, hlt
ã®å½ä»¤ã»ãããæã¤8bitã®CPUãå®ç¾©ããã
å½ä»¤ã®ç¨®é¡ã¯å°ãªãããååãªæ±ç¨æ§ãåãã¦ããä½ã§ãè¨ç®ã§ããã
(9æ¥ç®) ç¬èªå½ä»¤ã»ããCPUã®è£½ä½éå§
ãªãã¸ããªã¯ããã ã¨ãã¥ã¬ã¼ã¿è£½ä½ã®åæ¥ã¨åæ§ã«ã大æ ãä½ã£ãã ã¡ã¢ãªãã¯ããã¯ãç¨æããCPUã¨é»å§å¤ãããã¨ãã§ããããã«ããã
(10æ¥ç®) ç¬èªå½ä»¤ã»ããCPUãå®æ
ã¨ãã¥ã¬ã¼ã¿è£½ä½ã®äºæ¥ç®ã¨åæ§ã«ãæ©æ¢°èªã®è§£éé¨åãä½ã£ãã
ããã¯çµã¿åããåè·¯ã«ãªãã®ã§ãHDLã®always_comb
ãããã¯å
ã«æ¸ãå¿
è¦ããã£ãã
å®æå¾ãã¨ãããããã£ããããæ°ãè¨ç®ããã
twitter.comCPUèªä½ã¢ããã³ãã«ã¬ã³ãã¼ï¼ï¼ã¤ãã«CPUãå®æãã¾ããï¼ï¼HDLã500è¡æ¸ãã ãã§ãå ç®æ¸ç®,if,while,ãã¼ã«ã«å¤æ°,é¢æ°å®ç¾©å¼åºã®å¯è½ãªCPUãä½ããã®ã§ããæ©æ¢°èªã®mov,add,sub,cmp,push,pop,jmp,jccããå®è£ ããã°ãCPUã¨ã³ã³ãã¤ã©ãç¹ããã¾ããåç»ã¯ãã£ããããè¨ç®https://t.co/PieYgoTg50 pic.twitter.com/SfSjxkYaL0
— 解çç¥ (@kaitou_ryaku) 2017å¹´12æ10æ¥
æ©æ¢°èªããDããªããããããã¾ã§(4æ¥ç®-7æ¥ç®)
CPUã®åè·¯æ§æã®èª¬æã«å ¨åæçããã
極è«ããã¨ãCPUã¯Dããªããããããã®ã«ã¼ãã§ãã£ãã
(7æ¥ç®) æ¬æ ¼çãªCPUã®åè·¯æ§æ
è¦ããã«CPUã¨ã¯ãã¡ã¢ãªã«æ ¼ç´ãããæ©æ¢°èªãåå¾ããå®è¡ããåå¾ããå®è¡ãããããç¹°ãè¿ãåè·¯ã ã£ãã
å³ã®ç·ã¯ã¡ã¢ãªã«ç¹ãã£ã¦ããã ã«ã¦ã³ã¿ã«ã¯èªã¿è¾¼ãã¹ãã¡ã¢ãªã®ã¢ãã¬ã¹ãä¿æããã¦ããã å½ä»¤ã®å®è¡ãçµããã¨ã+1ãã§ã¤ã³ã¯ãªã¡ã³ããããã
ã¡ã¢ãªãå«ããã³ã³ãã¥ã¼ã¿ã®å ¨æ¯ãä¸æã«ã¾ã¨ããã¨
(6æ¥ç®) ALU
ALUã¨ã¯è¨ç®åè·¯ã®ãã¨ã ã£ãã
ç¹ã«è¤æ°æ¡ã足ãããã®å ¨å ç®å¨ãéè¦ã ã£ãã
(5æ¥ç®) ã¬ã¸ã¹ã¿ã®é¸æ
è¨ç®åè·¯ã¯CPUã®æ¬è³ªã§ã¯ãªã(è¶
æ´è«)ã®ã§ãåãå¤ãã¦èããã¨ã
mov
å½ä»¤ã ããå®è¡ã§ããCPUã«ãªãã
è¦ãããã®ããã«ã¡ã¢ãªãåã£æãã¨
ããã¯å¤æ° a
, b
, c
, d
ã®éã§å¤ãã³ãã¼(mov
å½ä»¤)ããåè·¯ã ã
ã³ãã¼å
ã¯ç·Sy
ã®é»å§å¤ã§æå®ããã³ãã¼å
ãç·Sx
ã®é»å§å¤ã§æå®ããã
ã¡ã¢ãªã¯ãããã®ç·ã«ç¹ãã£ã¦ããã
ã¤ã¾ãæ©æ¢°èª(ãã¤ããª)ã¯ãSx
ã¨Sy
ã®é»å§å¤ã«å¯¾å¿ãã¦ããã
æå®ã«ç¨ããMUX
ã¯ãã«ããã¬ã¯ãµã¨å¼ã°ããåè·¯ç´ åã§ãif æçãªå½¹å²ãæããã¦ããã
(4æ¥ç®) 1bitå転CPU
ãã«ããã¬ã¯ãµã¯CPUã®æ¬è³ªã§ã¯ãªã(è¶ æ´è«)ã®ã§ãåãå¤ãã¦èããã
è¦ãããã®ããã«å¤æ°ã1種é¡(a
ã ã)ã«ããã
ããã«NOTã²ã¼ããæãã¨
ä»ã¾ã§å¤æ°ã¨èªãã§ããé·æ¹å½¢ã¯ãDããªããããããã¨å¼ã°ããåè·¯ç´ åã ã
- ã¯ããã¯ãä¸ããç¬éã«ãå ¥å(D)ãåºå(Q)ã«ã³ãã¼ããã
- ãã®ç¬é以å¤ã¯ãå ¥åãå¤ãã¦ãåºåã¯å¤ãããªã
ã¨ããæ§è³ªãæã¤ãçµæãåè·¯ã®æåã¯
- ä»£å ¥ : ã¯ããã¯ãç«ã¡ä¸ããç¬éã«ãDããªããããããã®å ¥å£ãåºå£ãä¸æ¸ã
- è¨ç® : ã¯ããã¯ãç«ã¡ä¸ãã£ãå¾ã«ãDããªããããããã®åºå£ããåºãé»å§ãNOTã²ã¼ããéé
CPUã¨ã¯ããã®ããã«ä»£å ¥âè¨ç®âä»£å ¥âè¨ç®â...ãç¹°ãè¿ãåè·¯ã®ãã¨ã ã£ãã
極è«ããã¨ãCPUã¨ã¯ãDããªããããããã®åºå£ãå ¥å£ã«ç¹ãã ã«ã¼ãåè·¯ã§ããã
Dããªããããããããåå°ä½ã¾ã§(2,3æ¥ç®)
DããªãããããããNANDã²ã¼ãã®åå°ä½æ§æã説æããã
(3æ¥ç®) Dããªãããããã
Dããªããããããã®å®è£ ã¯
ã¤ã¾ãNANDã²ã¼ãã¨NOTã²ã¼ãã§æ§æããã¦ããã
(2æ¥ç®) åå°ä½ç´ å
NANDã²ã¼ãã¨NOTã²ã¼ãã®å®è£ ã¯
NOTã²ã¼ãã®åè·¯ããç°¡ç¥åããã«ãã¡ãã¨æãã¨
ããã«åºç¾ãããã©ã³ã¸ã¹ã¿ã¯ãpååå°ä½ã¨nååå°ä½ãçµã¿åããã¦ã以ä¸ã®ããã«ä½ããã¦ããã
ããã«ä¸ã®é層ã«é²ãã«ã¯ãéååå¦ã¨çµ±è¨åå¦ã®ç¥èãå¿ è¦ã«ãªãã æ¬æ°ã§ãããªããå¨æããã³ã·ã£ã«ä¸ã®(å¤ä½ç¸é¢ãç¹°ãè¾¼ãã )ã·ã¥ã¬ãã£ã³ã¬ã¼æ¹ç¨å¼ã解ãã¦ããã³ãå³ãæãã輸éä¿æ°ã調ã¹ãå¿ è¦ãããã
以ä¸ã§ä½ã¬ã¤ã¤ã®è§£èª¬ã¯çµäºã
æå¾ã«ãä»ã¾ã§ã«ä½æãããã®ãçµåãã¦ã¿ãã
èªä½ã³ã³ãã¤ã© + èªä½ã¨ãã¥ã¬ã¼ã¿
10çªç®ã®ãã£ããããæ°ãè¨ç®ããã³ã¼ã(Cè¨èª)
int fib(int n); int main() { // 10çªç®ã®ãã£ããããæ°ãè¨ç® int a; a = fib(10); return a; } // å帰çã«ãã£ããããæ°ãè¨ç® int fib(int n) { int ret; // ããã«è¨ç®çµæãå ¥ãã // ååã¨åã åã®ãã£ããããæ°ã足ã if (n > 2) { ret = fib(n-2) + fib(n-1); } // fib(1)ã¨fib(2)ã¯1ã¨ãã else { if (n == 2) { ret = 1; } else { if (n == 1) { ret = 1; } } } return ret; }
è¦ããã«å¤æ°a
ã«10çªç®ã®ãã£ããããæ°ã代å
¥ããããã°ã©ã ã ã
else if
ãå®è£
ããã¦ãªãã®ã§å°ã
èªã¿ã«ããããææ
¢ãã¦æ¬²ããã
ãããèªä½ã³ã³ãã¤ã©ã® minc (ãªãã¸ããª) ã§ã³ã³ãã¤ã«ããã
$ ./minc.out fibonacci.c fibonacci.asm
ãããæ©æ¢°èªã«å¤æã㦠èªä½x86ã¨ãã¥ã¬ã¼ã¿ (ãªãã¸ããª) ä¸ã§å®è¡ããã¨
$ nasm fibonacci.asm -o fibonacci.bin
$ ./x86-simulator.out fibonacci.bin
å®è¡çµæã®æ«å°¾ãè¦ã¦ã¿ãã¨
Count : 005078 Operation: 00001E : 0xE9 (jmp imm32) --- FLAG --- CF:0 ZF:0 SF:0 OF:0 --- REGISTER --- eax: 0x00000037 ecx: 0x00000000 edx: 0x0000000A ebx: 0x00000000 esp: 0x000003E8 ebp: 0x000003FC esi: 0x00000000 edi: 0x00000000 eip: 0x000000E2 --- STACK MEMORY --- 0003E0: FC 03 00 00 37 00 00 00 37 00 00 00 00 00 00 00 0003F0: 00 00 00 00 00 00 00 00 37 00 00 00 00 00 00 00
éè¦ãªã®ã¯REGISTERã®é¨åã
eax
ã®å¤ã0x00000037
ã«ãªã£ã¦ããã
ã¤ã¾ã16é²æ°ã§37ã10é²æ°ã ã¨16*3+7
ã§55ã«ãªãã
ã¨ããã§ãã£ããããæ°ã¯
1, 1, 2, 3, 5, 8, 13, 21, 34, 55
ããï¼ï¼10çªç®ã®ãã£ããããã¯55ã ã ã¨ãã¥ã¬ã¼ã¿ã§æ£ããè¨ç®ã§ãã¦ããããã³ã³ãã¤ã«ã«ã¯æåããããã ã
å½ä»¤å®è¡åæ°ã¯Count: 005078
ã§ãç´äºååç¨åº¦ã
èªä½ã³ã³ãã¤ã© + èªä½CPU
ãã£ãã®ã³ã¼ããæ©æ¢°èªã«ãã¦èªä½CPU (ãªãã¸ããª) ã§åããã¦ã¿ãã
波形ã·ãã¥ã¬ã¼ã¿ã«ããçµæã¯
eax
ã«0x00000037
ãå
¥ã£ã¦ããæ£ãããã£ããããæ°ãè¨ç®ã§ããããã ã
å½ä»¤ã®çµäºæå»ã¯ãç´50,780ããç§ã ã 1ã¯ããã¯10ããç§ãªã®ã§ãã¨ãã¥ã¬ã¼ã¿ã«ããå½ä»¤å®è¡åæ°ã«ä¸è´ãã¦ããã ããã¯ä½æããCPUãã·ã³ã°ã«ã¯ããã¯ããã»ããµãã¤ã¾ã1ã¯ããã¯æ¯ã«1ã¤ã®æ©æ¢°èªå½ä»¤ãå®è¡ãã¦ããããã ã æ®éã®CPUã ã¨ãã¯ããã¯æ° = å½ä»¤å®è¡æ°ãã¿ãããªå¯¾å¿é¢ä¿ã¯ãªãã®ã§ã注æããå¿ è¦ãããã
ãããFPGAã®å®æ©ã§åãããããããããç¡çã ã£ãã CPUåè·¯ã®è«çåæã«ã¯æåãããã®ã®ãã¡ã¢ãªå®¹éã足ãããã¤ã³ããªã¡ã³ãã«å¤±æããã
(以ä¸ã¯å®å ¨ã«æ¨æ¸¬ã ã)ã¡ã¢ãªã足ããªãã£ãã®ã¯ã·ã³ã°ã«ã¯ããã¯ããã»ããµãåå ã§ã¯ãªããã¨èãã¦ããã ä»åã®CPUã¯1ã¯ããã¯ã§ãã§ãããã¹ãã¡ã¢ãªãã¼ã¿ãå¤ããã¦ããã¼ã¿ãã¹ã120æ¬ã¨ããæå³ä¸æãªæ§æã«ãªã£ã¦ããã ãã®çµæãè«çåææã«ã¡ã¢ãªæ©è½ãããªããããããã§æ§æãããã¨ãã¦ãè³æºã足ããã«ã¨ã©ã¼ãã¯ãã¦ãæ°ãããã
解決ããã«ã¯ã1ã¯ããã¯ã§1byteãã¤ãã§ãããããããªã¹ãã¼ããã·ã³ãä½ã£ã¦ã¢ãã¬ã¹ãã¹ã®æ¬æ°ãæ¸ããã°ãã¡ã¢ãªãã¡ã¢ãªã¢ã¬ã¤ã§æ§æããã¦ãåé¡ã解決ãããããã
ã¾ãã§ã波形ã·ãã¥ã¬ã¼ã¿ä¸ã§ã¯åãã¦ãããããã¨ãããã ãããããã£ãããã㯠10æ¥ç®ã«ä½ã£ãFPGAã®CPU ã§æ¢ã«åãã¦ã(ä¸ã®ãã¤ã¼ã)ãã å ¨ç¶æãããªããã(æãã)1ã
twitter.comCPUèªä½ã¢ããã³ãã«ã¬ã³ãã¼ï¼ï¼ã¤ãã«CPUãå®æãã¾ããï¼ï¼HDLã500è¡æ¸ãã ãã§ãå ç®æ¸ç®,if,while,ãã¼ã«ã«å¤æ°,é¢æ°å®ç¾©å¼åºã®å¯è½ãªCPUãä½ããã®ã§ããæ©æ¢°èªã®mov,add,sub,cmp,push,pop,jmp,jccããå®è£ ããã°ãCPUã¨ã³ã³ãã¤ã©ãç¹ããã¾ããåç»ã¯ãã£ããããè¨ç®https://t.co/PieYgoTg50 pic.twitter.com/SfSjxkYaL0
— 解çç¥ (@kaitou_ryaku) 2017å¹´12æ10æ¥
æå¾ã«
äºæ³å¤ã«å¤ãã®æ¹ã ã«èªãã§ããã ããããã§ãæç¡éã§ãã
æå¾ã«ä¸è¨ã ãã
ä¸é£ã®è¨äºã¯ãããã¾ã§ç§çãªã¡ã¢ã§ãã
åå¿è ãæ¸ããè¨äºãªã®ã§ã誤ããå¤æ°å«ã¾ãã¦ããã¯ãã§ãã ãã®è¨äºãç²ç®çã«ä¿¡ããã®ã¯ãã¸ã§ã¤ãã¤ã®ã§ããã£ããããæç§æ¸ãèªãã¹ãã§ãã
ããã§ã¯25æ¥éããããã¨ããããã¾ããã