LoginSignup
0
0

More than 3 years have passed since last update.

windowsでiverilog その45

Last updated at Posted at 2020-07-07

概要

windowsでiverilogやってみた。
polyphonyでcpu書いてみる。

サンプルコード

from polyphony import testbench, module, is_worker_running, rule
from polyphony.io import Port
from polyphony.typing import uint8
from polyphony.timing import clksleep

@module
class mpu:
    def __init__(self):
        self.out = Port(uint8, 'out')
        self.append_worker(self.worker)
    def worker(self):
        rom = [
            0x8000,  #r0set  0   0
            0xd000,  #r0save  0  1
                   #loop:
            0xc000,  #r0load  0  2
            0x841a,  #r1set  26  3
            0x6100,  #r0  <  r1  4
            0x5090,  #if  pass   5
            0xc000,  #r0load  0  6
            0x8461,  #r1set  97  7
            0x0100,  #r0  +  r1  8
            0xa000,  #r0out      9
            0xc000,  #r0load  0  10
            0x8401,  #r1set  1   11
            0x0100,  #r0  +  r1  12
            0xd000,  #r0save  0  14
            0x4002,  #jp  loop   15
            0x400f, #pass:       16
        ]
        ram = [0] * 8
        reg = [0] * 4
        pc = 0
        while is_worker_running():
            with rule(scheduling = 'pipeline'):
                while pc < 100:
                    ins = rom[pc]
                    pc = pc + 1
                    op = (ins >> 14) & 0x3
                    funct = (ins >> 12) & 0x3
                    a = (ins >> 10) & 0x3
                    b = (ins >> 8) & 0x3
                    data = ins & 0xff
                    addr = ins & 0xff
                    if op == 0:
                        if funct == 0:
                            reg[a] = reg[a] + reg[b]
                        elif funct == 1:
                            reg[a] = reg[a] - reg[b]
                        elif funct == 2:
                            reg[a] = reg[a] * reg[b]
                        elif funct == 3:
                            reg[a] = reg[a] / reg[b]
                        else:
                            pc = 100
                    elif op == 1:
                        if funct == 0:
                            pc = addr
                        elif funct == 1:
                            if (reg[a] == 0):
                                pc = addr
                        elif funct == 2:
                            reg[a] = reg[a] < reg[b]
                        elif funct == 3:
                            reg[a] = reg[a] % reg[b]
                        else:
                            pc = 100
                    elif op == 2:
                        if funct == 0:
                            reg[a] = data
                        elif funct == 1:
                            reg[a] = reg[b] + data
                        elif funct == 2:
                            self.out(reg[a])
                            print(reg[a])
                        elif funct == 3:
                            self.out(data)
                        else:
                            pc = 100
                    elif op == 3:
                        if funct == 0:
                            reg[a] = ram[addr]
                        elif funct == 1:
                            ram[addr] = reg[a]
                        else:
                            pc = 100
                    else:
                        pc = 100

m = mpu()

@testbench
def test(m):
    clksleep(120)
    print("ok")

test(m)




python実行結果


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
ok
121
122


以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0