def mkLed(numthreads=8): m = Module('blinkled') clk = m.Input('CLK') rst = m.Input('RST') mymutex = vthread.Mutex(m, 'mymutex', clk, rst) def myfunc(tid): mymutex.lock() print("Thread %d Lock" % tid) for i in range(20): pass # sleep print("Thread %d Hello" % tid) mymutex.unlock() print("Thread %d Unlock" % tid) def blink(): for tid in range(numthreads): pool.run(tid, tid) for tid in range(numthreads): pool.join(tid) th = vthread.Thread(m, 'th_blink', clk, rst, blink) pool = vthread.ThreadPool(m, 'th_myfunc', clk, rst, myfunc, numthreads) fsm = th.start() return m
def mkLed(numthreads=8): m = Module('blinkled') clk = m.Input('CLK') rst = m.Input('RST') led = m.OutputReg('LED', 8) datawidth = 32 addrwidth = 10 myram = vthread.RAM(m, 'myram', clk, rst, datawidth, addrwidth) mymutex = vthread.Mutex(m, 'mymutex', clk, rst) def myfunc(tid, size): mymutex.lock() print("Thread %d Lock" % tid) for i in range(size): read_data = myram.read(i) write_data = read_data + tid + i myram.write(i, write_data) print("Thread %d ram[%d] <- %d" % (tid, i, write_data)) mymutex.unlock() print("Thread %d Unlock" % tid) def blink(): all_ok = True size = 16 for i in range(size): myram.write(i, 0) for tid in range(numthreads): pool.run(tid, tid, size) for tid in range(numthreads): pool.join(tid) for i in range(size): read_data = myram.read(i) led.value = read_data print("result ram[%d] = %d" % (i, read_data)) expected = i * numthreads + (0 + numthreads - 1) * numthreads // 2 if vthread.verilog.NotEql(read_data, expected): all_ok = False print(i, read_data, expected) if all_ok: print('# verify: PASSED') else: print('# verify: FAILED') th = vthread.Thread(m, 'th_blink', clk, rst, blink) pool = vthread.ThreadPool(m, 'th_myfunc', clk, rst, myfunc, numthreads) fsm = th.start() return m
def mkLed(numthreads=8): m = Module('blinkled') clk = m.Input('CLK') rst = m.Input('RST') led = m.OutputReg('LED', 8) datawidth = 32 addrwidth = 10 myram = vthread.RAM(m, 'myram', clk, rst, datawidth, addrwidth) mymutex = vthread.Mutex(m, 'mymutex', clk, rst) def myfunc(tid, size): mymutex.lock() print("Thread %d Lock" % tid) for i in range(size): read_data = myram.read(i) write_data = read_data + tid + i myram.write(i, write_data) print("Thread %d ram[%d] <- %d" % (tid, i, write_data)) mymutex.unlock() print("Thread %d Unlock" % tid) def blink(): size = 16 for i in range(size): myram.write(i, 0) for tid in range(numthreads): pool.run(tid, tid, size) for tid in range(numthreads): pool.join(tid) for i in range(size): read_data = myram.read(i) led.value = read_data print("result ram[%d] = %d" % (i, read_data)) th = vthread.Thread(m, 'th_blink', clk, rst, blink) pool = vthread.ThreadPool(m, 'th_myfunc', clk, rst, myfunc, numthreads) fsm = th.start() return m
def mkLed(numthreads=8): m = Module('blinkled') clk = m.Input('CLK') rst = m.Input('RST') led = m.Output('LED', 8) count = vthread.Shared(m.Reg('count', 32, initval=0)) led.assign(count.value) mymutex = vthread.Mutex(m, 'mymutex', clk, rst) def myfunc(tid): mymutex.lock() print("Thread %d Lock" % tid) for i in range(20): pass # sleep count.write(count.value + 1) print("Thread %d count = %d" % (tid, count.value)) mymutex.unlock() print("Thread %d Unlock" % tid) def blink(): count.write(0) for tid in range(numthreads): pool.run(tid, tid) for tid in range(numthreads): pool.join(tid) print("result count = %d" % count.value) th = vthread.Thread(m, 'th_blink', clk, rst, blink) pool = vthread.ThreadPool(m, 'th_myfunc', clk, rst, myfunc, numthreads) fsm = th.start() return m
def mkLed(numthreads=64): m = Module('blinkled') clk = m.Input('CLK') rst = m.Input('RST') btnc = m.Input('btnC') btnu = m.Input('btnU') btnl = m.Input('btnL') btnr = m.Input('btnR') btnd = m.Input('btnD') sw = m.Input('sw', 16) led = m.Output('led', 16) count = vthread.Shared(m.Reg('count', 15, initval=0)) done = m.Reg('done', initval=0) led.assign(Cat(done, count.value)) mymutex = vthread.Mutex(m, 'mymutex', clk, rst) def sleep(time): for i in range(time): for _ in range(1024): pass # sleep def myfunc(tid): mymutex.lock() print("Thread %d Lock" % tid) sleep(sw) count.write(count.value + 1) print("Thread %d count = %d" % (tid, count.value)) mymutex.unlock() print("Thread %d Unlock" % tid) def wait_start(polarity=True): while btnc != polarity: pass def blink(): count.write(0) while True: done.value = 0 wait_start() for tid in range(numthreads): pool.run(tid, tid) for tid in range(numthreads): pool.join(tid) for tid in range(numthreads): pool.reset(tid) done.value = 1 th = vthread.Thread(m, 'th_blink', clk, rst, blink) pool = vthread.ThreadPool(m, 'th_myfunc', clk, rst, myfunc, numthreads) fsm = th.start() return m