#概要
windowsでiverilogやってみた。
polyphonyでcpu書いてみた。
cpuでfizzbuzz、やってみた。
#サンプルコード
from polyphony import testbench, module, is_worker_running
from polyphony.io import Port
from polyphony.typing import bit, uint8
from polyphony.timing import clksleep
@module
class mpu2:
def __init__(self):
self.data = Port(uint8, 'out', init = 0)
self.start = Port(bit, 'out', init = 0)
self.append_worker(self.worker)
def _wait(self):
for i in range(100000):
pass
def send(self, c:uint8):
self.data(c);
self.start(1)
clksleep(1)
self.start(0)
self._wait()
def worker(self):
rom = [
0x8000, # r0set 0 0
0xd000, # r0save 0 1
0x8030, # r0set '0' 2
0xd001, # r0save 1 3
0x8030, # r0set '0' 4
0xd002, # r0save 2 5
#label_20:
0xc002, # r0load 2 6
0x8401, # r1set 1 7
0x0100, # r0 + r1 8
0xd002, # r0save 2 9
0x8439, # r1set '9' a
0x7100, # r0 < r1 b
0x5013, # if label_32 c
0xc001, # r0load 1 d
0x8401, # r1set 1 e
0x0100, # r0 + r1 f
0xd001, # r0save 1 10
0x8030, # r0set '0' 11
0xd002, # r0save 2 12
#label_32:
0xc000, # r0load 0 13
0x8401, # r1set 1 14
0x0100, # r0 + r1 15
0xd000, # r0save 0 16
0xc000, # r0load 0 17
0x8464, # r1set 100 18
0x6100, # r0 < r1 19
0x5057, # if label_255 1a
0xc000, # r0load 0 1b
0x840f, # r1set 15 1c
0xf100, # r0 % r1 1d
0x502e, # if label_120 1e
0xc000, # r0load 0 1f
0x8405, # r1set 5 20
0xf100, # r0 % r1 21
0x5041, # if label_140 22
0xc000, # r0load 0 23
0x8403, # r1set 3 24
0xf100, # r0 % r1 25
0x504c, # if label_160 26
0xc001, # r0load 1 27
0xa000, # r0out 28
0xc002, # r0load 2 29
0xa000, # r0out 2a
0x8020, # r0set ' ' 2b
0xa000, # r0out 2c
0x4006, # jp label_20 2d
#label_120:
0x8066, # r0set 'f' 2e
0xa000, # r0out 2f
0x8069, # r0set 'i' 30
0xa000, # r0out 31
0x807a, # r0set 'z' 32
0xa000, # r0out 33
0x807a, # r0set 'z' 34
0xa000, # r0out 35
0x8062, # r0set 'b' 36
0xa000, # r0out 37
0x8075, # r0set 'u' 38
0xa000, # r0out 39
0x807a, # r0set 'z' 3a
0xa000, # r0out 3b
0x807a, # r0set 'z' 3c
0xa000, # r0out 3d
0x8020, # r0set ' ' 3e
0xa000, # r0out 3f
0x4006, # jp label_20 40
#label_140:
0x8062, # r0set 'b' 41
0xa000, # r0out 42
0x8075, # r0set 'u' 43
0xa000, # r0out 44
0x807a, # r0set 'z' 45
0xa000, # r0out 46
0x807a, # r0set 'z' 47
0xa000, # r0out 48
0x8020, # r0set ' ' 49
0xa000, # r0out 4a
0x4006, # jp label_20 4b
#label_160:
0x8066, # r0set 'f' 4c
0xa000, # r0out 4d
0x8069, # r0set 'i' 4e
0xa000, # r0out 4f
0x807a, # r0set 'z' 50
0xa000, # r0out 51
0x807a, # r0set 'z' 52
0xa000, # r0out 53
0x8020, # r0set ' ' 54
0xa000, # r0out 55
0x4006, # jp label_20 56
#label_255:
0x4090, # end 57
]
ram = [0] * 8
reg = [0] * 4
pc = 0
while is_worker_running():
while pc < 100:
ins = rom[pc]
#print(pc)
pc = pc + 1
op = (ins >> 14) & 0x3
funct = (ins >> 12) & 0x3
a = (ins >> 10) & 0x3
b = (ins >> 8) & 0x3
data = ins & 0xff
if op == 0:
if funct == 0:
reg[a] = reg[a] + reg[b]
else:
pc = 100
elif op == 1:
if funct == 0:#4
pc = data
elif funct == 1:#5
if (reg[a] == 0):
pc = data
elif funct == 2:#6
if (reg[a] < reg[b]):
reg[a] = 1
else:
reg[a] = 0
elif funct == 3:#7
if (reg[a] > reg[b]):
reg[a] = 1
else:
reg[a] = 0
else:
pc = 100
elif op == 2:
if funct == 0:#8
reg[a] = data
elif funct == 2:#a
self.send(reg[a])
print(chr(reg[a]))
else:
pc = 100
elif op == 3:
if funct == 0:#c
reg[a] = ram[data]
elif funct == 1:#d
ram[data] = reg[a]
elif funct == 3:#f
#print(reg[a], reg[b])
reg[a] = reg[a] % reg[b]
#print(reg[a])
else:
pc = 100
else:
pc = 100
m = mpu2()
@testbench
def test(m):
clksleep(8820)
print("ok")
test(m)
#python実行結果
f
i
z
z
b
u
z
z
3
1
3
2
f
i
z
z
3
4
b
u
z
z
f
i
z
z
3
7
3
8
f
i
z
z
b
u
z
z
4
1
f
i
z
z
4
3
4
4
f
i
z
z
b
u
z
z
4
6
4
7
f
i
z
z
4
9
b
u
z
z
f
i
z
z
5
2
5
3
f
i
z
z
b
u
z
z
5
6
f
i
z
z
5
8
5
9
f
i
z
z
b
u
z
z
6
1
6
2
f
i
z
z
6
4
b
u
z
z
f
i
z
z
6
7
6
8
f
i
z
z
b
u
z
z
7
1
f
i
z
z
7
3
7
4
f
i
z
z
b
u
z
z
7
6
7
7
f
i
z
z
7
9
b
u
z
z
f
i
z
z
8
2
8
3
f
i
z
z
b
u
z
z
8
6
f
i
z
z
8
8
8
9
f
i
z
z
b
u
z
z
9
1
9
2
f
i
z
z
9
4
b
u
z
z
f
i
z
z
9
7
9
8
f
i
z
z
ok
以上。