def mkLed(numthreads=8): m = Module('blinkled') clk = m.Input('CLK') rst = m.Input('RST') mybarrier = vthread.Barrier(m, 'mybarrier', clk, rst, numthreads) clock_counter = m.Reg('clock_counter', 32, initval=0) seq = Seq(m, 'seq', clk, rst) seq(clock_counter.inc()) def myfunc(tid): for step in range(10): for i in range(20): pass # sleep mybarrier.wait() print("Thread %d Time %d" % (tid, clock_counter)) 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') mylock = vthread.Lock(m, 'mylock', clk, rst) def myfunc(tid): mylock.acquire() print("Thread %d Lock" % tid) for i in range(20): pass # sleep print("Thread %d Hello" % tid) mylock.release() 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=4): m = Module('blinkled') clk = m.Input('CLK') rst = m.Input('RST') def blink(times): for tid in range(numthreads): pool.run(tid, tid) sum = 0 for tid in range(numthreads): pool.join(tid) sum += pool.ret(tid) print('sum = %d' % sum) def myfunc(tid): print('tid = %d' % tid) for i in range(30 - tid): pass return tid + 100 pool = vthread.ThreadPool(m, clk, rst, 'th_myfunc', myfunc, numthreads) th = vthread.Thread(m, clk, rst, 'th_blink', blink) fsm = th.start(20) 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 = 4 myfifo = vthread.FIFO(m, 'myfifo', clk, rst, datawidth, addrwidth) def myfunc(tid): myfifo.lock() print("Thread %d Lock" % tid) read_data = myfifo.deq() print("Thread %d fifo.out = %d" % (tid, read_data)) write_data = read_data + 1 myfifo.enq(write_data) print("Thread %d fifo.in <- %d" % (tid, write_data)) read_data = myfifo.deq() print("Thread %d fifo.out = %d" % (tid, read_data)) write_data = read_data + 1 myfifo.enq(write_data) print("Thread %d fifo.in <- %d" % (tid, write_data)) myfifo.unlock() print("Thread %d Unlock" % tid) def blink(): myfifo.enq(100) myfifo.enq(200) for tid in range(numthreads): pool.run(tid, tid) for tid in range(numthreads): pool.join(tid) read_data = myfifo.deq() led.value = read_data print("result fifo.out = %d" % read_data) read_data = myfifo.deq() led.value = read_data print("result fifo.out = %d" % 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.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=8): m = Module('blinkled') clk = m.Input('CLK') rst = m.Input('RST') mymutex = vthread.Mutex(m, 'mymutex', clk, rst) def myfunc(tid): print("-- Thread %d TryLock" % tid) lock = mymutex.try_lock() waitcount = 0 while not lock: print("-- Thread %d TryLock" % tid) waitcount += 1 lock = mymutex.try_lock() print("Thread %d Lock: waitcount=%d" % (tid, waitcount)) 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') 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_as_module=True) 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