38

Click here to load reader

How to Implement a CPU Emulator in Scheme

Embed Size (px)

DESCRIPTION

Cf. http://mcpc.arnip.org/wiki/csmeiji/log/20130322

Citation preview

Page 1: How to Implement a CPU Emulator in Scheme

How  to  Implement  a  CPU  Emulator  in  Scheme

Representa9ve  of  MCPC  

Kazuhiro  Hishinuma

MCPC:  a  Compe99ve  Programming  Circle   1  

hBp://mcpc.arnip.org/ #csmeiji

Page 2: How to Implement a CPU Emulator in Scheme

The Trigger to  Implement  a  CPU  Emulator

MCPC:  a  Compe99ve  Programming  Circle   2  

Page 3: How to Implement a CPU Emulator in Scheme

MCPC:  a  Compe99ve  Programming  Circle   3  

Page 4: How to Implement a CPU Emulator in Scheme

MCPC:  a  Compe99ve  Programming  Circle   4  

Page 5: How to Implement a CPU Emulator in Scheme

MCPC:  a  Compe99ve  Programming  Circle   5  

Page 6: How to Implement a CPU Emulator in Scheme

MCPC:  a  Compe99ve  Programming  Circle   6  

Page 7: How to Implement a CPU Emulator in Scheme

(  ◠‿◠  )☛  Make  It  Yourself

MCPC:  a  Compe99ve  Programming  Circle   7  

Page 8: How to Implement a CPU Emulator in Scheme

▂▅▇█▓▒░('ω')░▒▓█▇▅▂  ▂▅▇█▓▒░('ω')░▒▓█▇▅▂  ▂▅▇█▓▒░('ω')░▒▓█▇▅▂

MCPC:  a  Compe99ve  Programming  Circle   8  

Page 9: How to Implement a CPU Emulator in Scheme

The Reason to  Implement  in  Scheme

MCPC:  a  Compe99ve  Programming  Circle   9  

Page 10: How to Implement a CPU Emulator in Scheme

In  Scheme,  You  can  …

Get  Current  Con9nua9on  And  

Return  Mul9ple  Values  

MCPC:  a  Compe99ve  Programming  Circle   10  

Page 11: How to Implement a CPU Emulator in Scheme

Con9nua9on...?

/*  Pseudo-­‐Code  */  func9on  f  (  x  )  {      res  =  x  +  1;        return  (  res  );  };  

MCPC:  a  Compe99ve  Programming  Circle   11  

Con9nua9on  :=  A  Func9on-­‐Like  Object  To  Return  Value!!!  

Page 12: How to Implement a CPU Emulator in Scheme

What’s  the  Difference  Between  Func9on  And  Con9nua9on??

MCPC:  a  Compe99ve  Programming  Circle   12  

Page 13: How to Implement a CPU Emulator in Scheme

(  ◠‿◠  )☛In  Func9on

MCPC:  a  Compe99ve  Programming  Circle   13  

PROGRAM FUNCTION

CALL

RETURN

Page 14: How to Implement a CPU Emulator in Scheme

(  ◠‿◠  )☛In  Con9nua9on

MCPC:  a  Compe99ve  Programming  Circle   14  

PROGRAM CONTINU-­‐  ATION

CALL

Page 15: How to Implement a CPU Emulator in Scheme

(  ◠‿◠  )☛  A  Con9nua9on  is  Like  A  Func9on,  But  It  Won’t  Return.

MCPC:  a  Compe99ve  Programming  Circle   15  

Page 16: How to Implement a CPU Emulator in Scheme

o。(˘⊖˘  )    A  Con9nua9on  is  Like  A  Func9on…?

MCPC:  a  Compe99ve  Programming  Circle   16  

Page 17: How to Implement a CPU Emulator in Scheme

┗(☋` )┓三    return  (  a,  b,  c  );  

MCPC:  a  Compe99ve  Programming  Circle   17  

Page 18: How to Implement a CPU Emulator in Scheme

f  (  x  )  returns  y

MCPC:  a  Compe99ve  Programming  Circle   18  

f  (  x  )

x

y

Page 19: How to Implement a CPU Emulator in Scheme

f  (  x  )  returns  (  a,  b,  c  )

MCPC:  a  Compe99ve  Programming  Circle   19  

f  (  x  )

x a

b

c

Page 20: How to Implement a CPU Emulator in Scheme

ヾ(*>ヮ<)ノ”  A  Func9on  Returns  Mul9ple  Values!!  

MCPC:  a  Compe99ve  Programming  Circle   20  

Page 21: How to Implement a CPU Emulator in Scheme

Mul9ple  Values  Func9on

MCPC:  a  Compe99ve  Programming  Circle   21  

f  (  x  )

x a

b

c

Page 22: How to Implement a CPU Emulator in Scheme

Composing  Func9ons

MCPC:  a  Compe99ve  Programming  Circle   22  

f  (  x  ) g  (  a,  b,  c  )

x

d

e

Page 23: How to Implement a CPU Emulator in Scheme

Composing  Func9ons…?

MCPC:  a  Compe99ve  Programming  Circle   23  

fetch generator

3

2

Page 24: How to Implement a CPU Emulator in Scheme

This  is  a  Hardware  Diagram!!

MCPC:  a  Compe99ve  Programming  Circle   24  

clk_gen

counter

MUX 0

decoder

dec7seg

dec7seg

Page 25: How to Implement a CPU Emulator in Scheme

_人人人人人人人人_> Scheme  is  a  HDL!! <‾^Y^Y^Y^Y^Y^Y^Y^Y^‾

MCPC:  a  Compe99ve  Programming  Circle   25  

Page 26: How to Implement a CPU Emulator in Scheme

The Way to  Implement  a  CPU  Emulator

MCPC:  a  Compe99ve  Programming  Circle   26  

Page 27: How to Implement a CPU Emulator in Scheme

(  ◠‿◠  )☛Read  it!

MCPC:  a  Compe99ve  Programming  Circle   27  

Page 28: How to Implement a CPU Emulator in Scheme

(  ◠‿◠  )☛980  yen  (tax  in)

MCPC:  a  Compe99ve  Programming  Circle   28  

日経吔听吟叽叿叹  

咇咅咅咎年咅咇月号

Page 29: How to Implement a CPU Emulator in Scheme

(  ◠‿◠  )☛Implement  It!!

MCPC:  a  Compe99ve  Programming  Circle   29  

<machine>

gr mem

pc

fr fetch

run

Page 30: How to Implement a CPU Emulator in Scheme

(ฅ`・ω・´)っ=  There  is  One  Problem.

MCPC:  a  Compe99ve  Programming  Circle   30  

Page 31: How to Implement a CPU Emulator in Scheme

State  Transi9on  on  Hardware  is  …

DESTRUCTIVE!!

MCPC:  a  Compe99ve  Programming  Circle   31  

Page 32: How to Implement a CPU Emulator in Scheme

Destruc9ve  State  Transi9on

/*  Pseudo-­‐Code,  Given  s:  Current  State  */  S1:  /*  Procedure  of  State  S1  */  

 s  =  /*  S1-­‐>S2  */(  s  );    goto  S2;  

 S2:  /*  Procedure  of  State  S2  */  

 /*  …  */

MCPC:  a  Compe99ve  Programming  Circle   32  

Page 33: How to Implement a CPU Emulator in Scheme

Pure  State  Transi9on

/*  Pseudo-­‐Code,  Given  s:  Current  State  */  func9on  S1  (  s  )  {      /*  Procedure  of  State  S1  */      return  S2  (  /*  S1-­‐>S2  */(  s  )  );  }  func9on  S2  (  s  )  {      /*  Procedure  of  State  S2  ...  

MCPC:  a  Compe99ve  Programming  Circle   33  

Tail  Call

Page 34: How to Implement a CPU Emulator in Scheme

p  (`・ω・´q)  It’s  Solved.

MCPC:  a  Compe99ve  Programming  Circle   34  

Page 35: How to Implement a CPU Emulator in Scheme

The Result of  Implement  in  Scheme

MCPC:  a  Compe99ve  Programming  Circle   35  

Page 36: How to Implement a CPU Emulator in Scheme

(  ◠‿◠  )☛Demonstra9on

MCPC:  a  Compe99ve  Programming  Circle   36  

hBps://gist.github.com/kazh98/5117299 解説:  hBp://qiita.com/items/b667dc18e769c4bf857e  hBps://gist.github.com/kazh98/5213280 解説:  このスライド  

Page 37: How to Implement a CPU Emulator in Scheme

Conclusion

•  Scheme  is  a  Hardware  Descrip9on  Language.  •  State  Transi9on  can  be  Expressed  as  Tail  Call.  •  To  Implement  CPU  Emulator  is  interes9ng.  

Let  us  Be  a  Schemer!!

MCPC:  a  Compe99ve  Programming  Circle   37  

Page 38: How to Implement a CPU Emulator in Scheme

Thanks  for  YOUR  listening!

MCPC:  a  Compe99ve  Programming  Circle   38  

hBp://mcpc.arnip.org/