def gen_mem_image(): # text section text = \ """ # load array pointers csrr x1, mngr2proc < 100 csrr x2, mngr2proc < 0x2000 csrr x3, mngr2proc < 0x3000 csrr x4, mngr2proc < 0x4000 add x5, x0, x1 loop: lw x6, 0(x2) lw x7, 0(x3) add x8, x6, x7 sw x8, 0(x4) addi x2, x2, 4 addi x3, x3, 4 addi x4, x4, 4 addi x5, x5, -1 bne x5, x0, loop # end of the program csrw proc2mngr, x0 > 0 nop nop nop nop nop nop """ mem_image = assemble(text) # load data by manually create data sections using binutils src0_section = mk_section(".data", c_vvadd_src0_ptr, src0) src1_section = mk_section(".data", c_vvadd_src1_ptr, src1) # load data mem_image.add_section(src0_section) mem_image.add_section(src1_section) return mem_image
def run_test(ProcModel, XcelModel, gen_test, dump_vcd=None, src_delay=0, sink_delay=0, mem_stall_prob=0, mem_latency=1, max_cycles=10000): # Instantiate and elaborate the model th = TestHarness(ProcModel, XcelModel, dump_vcd, src_delay, sink_delay, mem_stall_prob, mem_latency) th.elaborate() # Assemble the test program mem_image = assemble(gen_test()) # Load the program into the model th.load(mem_image) # Run the simulation th.apply(SimpleSim[1:]) th.sim_reset() print() T = 0 while not th.done() and T < max_cycles: th.tick() print "{:3}: {}".format(T, th.line_trace()) T += 1 # Force a test failure if we timed out assert T < max_cycles # Add a couple extra ticks so that the VCD dump is nicer th.tick() th.tick() th.tick()
def gen_mem_image(): # text section text = """ csrr x4, mngr2proc < 0x2000 csrr x5, mngr2proc < 0x3000 csrr x6, mngr2proc < 0x4000 csrr x7, mngr2proc < 10 csrr x8, mngr2proc < 10 addi x24, x0, 64 # coeff0 addi x25, x0, 48 # coeff1 # Assume that nrows and ncols are positive and otherwise well-behaved addi x2, x7, -1 # end condition nrows addi x3, x8, -1 # end condition ncols addi x9, x0, 1 # ridx starts at 1 # zero: row loop zero: addi x10, x0, 1 # one: col loop # Calculate mask index one: mul x11, x8, x9 # ridx*ncols add x11, x11, x10 # ridx*ncols + cidx slli x11, x11, 2 # ridx*ncols + cidx (pointer) add x12, x5, x11 # ridx*ncols + cidx (pointer) for mask lw x12, 0(x12) # mask[ridx*ncols + cidx] # If block # if ( !mask[ridx*ncols + cidx] ) goto two: beq x12, x0, two add x12, x6, x11 # ridx*ncols + cidx (pointer) for src lw x13, 0(x12) # src[ridx*ncols + cidx] mul x13, x13, x24 # src[ridx*ncols + cidx] * coeff0 add x23, x13, x0 # out = src[ridx*ncols + cidx] * coeff0 lw x13, 4(x12) # src[ridx*ncols + (cidx+1)] mul x13, x13, x25 # src[ridx*ncols + (cidx+1)] * coeff1 add x23, x23, x13 # out += src[ridx*ncols + (cidx+1)] * coeff1 lw x13, -4(x12) # src[ridx*ncols + (cidx-1)] mul x13, x13, x25 # src[ridx*ncols + (cidx-1)] * coeff1 add x23, x23, x13 # out += src[ridx*ncols + (cidx-1)] * coeff1 addi x22, x9, 1 # ridx+1 mul x12, x8, x22 # (ridx+1)*ncols add x12, x12, x10 # (ridx+1)*ncols + cidx slli x12, x12, 2 # (ridx+1)*ncols + cidx (pointer) add x13, x6, x12 # (ridx+1)*ncols + cidx (pointer) for src lw x13, 0(x13) # src[(ridx+1)*ncols + cidx] mul x14, x13, x25 # src[(ridx+1)*ncols + cidx] * coeff1 add x23, x23, x14 # out += src[(ridx+1)*ncols + cidx] * coeff1 addi x22, x9, -1 # ridx-1 mul x12, x8, x22 # (ridx-1)*ncols add x12, x12, x10 # (ridx-1)*ncols + cidx slli x12, x12, 2 # (ridx-1)*ncols + cidx (pointer) add x13, x6, x12 # (ridx-1)*ncols + cidx (pointer) for src lw x13, 0(x13) # src[(ridx-1)*ncols + cidx] mul x14, x13, x25 # src[(ridx-1)*ncols + cidx] * coeff1 add x23, x23, x14 # out += src[(ridx-1)*ncols + cidx] * coeff1 add x12, x4, x11 # ridx*ncols + cidx (pointer) for dest srai x23, x23, 8 # out >>= shamt sw x23, 0(x12) # dest[ridx*ncols + cidx] = out jal x0, three # End of if block, goto three: # Else block two: add x12, x6, x11 # ridx*ncols + cidx (pointer) for src lw x13, 0(x12) # src[ridx*ncols + cidx] add x14, x4, x11 # ridx*ncols + cidx (pointer) for dest sw x13, 0(x14) # dest[ridx*ncols + cidx] = src[ridx*ncols + cidx] three: addi x10, x10, 1 # cidx++ bne x10, x3, one # if ( cidx != ncols - 1 ) goto one: addi x9, x9, 1 # ridx++ bne x9, x2, zero # if ( ridx != nrows - 1 ) goto zero: csrw proc2mngr, x0 > 0 nop nop nop nop nop nop """ mem_image = assemble(text) # load data by manually create data sections using binutils src_section = mk_section(".data", c_masked_filter_src_ptr, src) mem_image.add_section(src_section) mask_section = mk_section(".data", c_masked_filter_mask_ptr, mask) mem_image.add_section(mask_section) return mem_image
def gen_mem_image(): # text section text = \ """ csrr x25, mngr2proc < 10 csrr x26, mngr2proc < 0x2000 csrr x27, mngr2proc < 0x3000 csrr x28, mngr2proc < 0x4000 add x29, x0, x25 #addi x2, x2, 120 # adjust stack size #li x10, 3904692793 # lui x10 953294 # addi x10 x10 569 addi x2, x2, 512 # adjust stack size lw x10,0(x26) jal x1, Start Start: addi x2, x2, -28 # Adjust stack sw x1, 24(x2) # Save return address sw x8, 20(x2) # Save current fx1me pointer addi x8, x2, 28 # Adjust fx1me pointer lw x12, 12(x27) # Load last value from array sw x12, -8(x8) # Save it for future use in loop lw x12, 8(x27) # Load third value from array sw x12, -12(x8) # Save it for future use in loop lw x12, 4(x27) # Load second value from array sw x12, -16(x8) # Save it for future use in loop lw x11, 0(x27) # Load first value from array sw x11, -20(x8) # Save it for future use addi x16, x0, 0 # int out = 0 addi x11, x11, -1 # tabs[0]-1 srl x12, x10, x11 # seed>>(tabs[0]-1) andi x12, x12, 1 # feedback = bit read from seed addi x13, x0, 1 # int i=1 jal x0, FOR_FEEDBACK FOR_FEEDBACK: # for(int i=1;i<4;i++) addi x14, x0, 4 blt x13, x14, CNT_FEEDBACK # if i<4 count feedback value jal x0, CNT_SEED # else go to counting seed value #x10 seed x11 tab[0] x12 feedback x13 i x14 4 CNT_FEEDBACK: slli x14, x13, 2 # i*4 - used for addressing addi x17, x8, -20 # -20(x8) = address of first array element add x14, x14, x17 # decode address of array element lw x14, 0(x14) # tabs[i] addi x14, x14, -1 # (tabs[i]-1) srl x15, x10, x14 # seed>>(tabs[1]-1)) andi x15, x15, 1 # seed>>(tabs[1]-1))&1 xor x12, x12, x15 # feedback = feedback ^ ((seed>>(tabs[i]-1))&1); addi x13, x13, 1 # i++ jal x0, FOR_FEEDBACK #x10 seed x11 tab[0] x12 feedback x13 i x14 tabs[i]-1 x15 seed>>(tabs[1]-1))&1 x17 -28 CNT_SEED: slli x10, x10, 1 # seed = (seed<<1); or x10, x10, x12 # seed |= feedback; addi x13, x0, 31 # int i = 31 jal x0, FOR_OUT FOR_OUT: blt x13, x0, END # if i<0 end function jal x0, CNT_OUT # else count out value CNT_OUT: slli x14, x16, 1 # (out << 1) srl x15, x10, x13 # (seed>>i) andi x15, x15, 1 # ((seed>>i)&1) or x16, x14, x15 # out = (out << 1) | ((seed>>i)&1); addi x13, x13, -1 # i-- jal x0, FOR_OUT END: sw x16, 0(x28) addi x28, x28, 4 addi x10, x16 0 # start again with init value = out addi x29, x29, -1 beq x29, x0, final jalr x0, x1, 0 final: csrw proc2mngr, x0 > 0 nop nop nop nop nop nop """ mem_image = assemble(text) # load data by manually create data sections using binutils src0_section = mk_section(".data", c_vvadd_src0_ptr, src0) src1_section = mk_section(".data", c_vvadd_src1_ptr, src1) # load data mem_image.add_section(src0_section) mem_image.add_section(src1_section) return mem_image
def gen_mem_image(): # text section text = (""" # load array pointers csrr x1, mngr2proc < {} # size csrr x2, mngr2proc < 0x2000 # src pointer csrr x3, mngr2proc < 0x3000 # mask pointer csrr x4, mngr2proc < 0x4000 # dst pointer addi x5, x0, 16 # shift amount lw x6, 0(x3) # mask = 0xffff addi x11, x0, 1 # Go bit add x7, x0, x0 # cksum = 0 loop_i: lw x8, 0(x2) # load first 32-bit word lw x9, 4(x2) # load second 32-bit word lw x10, 8(x2) # load third 32-bit word # transfer data to accelerator csrw 0x7E0, x7 csrw 0x7E1, x8 csrw 0x7E2, x9 csrw 0x7E3, x10 csrw 0x7E4, x11 # read back the result csrr x7, 0x7E5 addi x2, x2, 12 # increment word address addi x1, x1, -6 # decrement loop counter i bne x1, x0, loop_i sw x7, 0(x4) # End of program csrw proc2mngr, x0 > 0 nop nop nop nop nop nop """.format(c_cksum_size)) mem_image = assemble(text) # load data by manually create data sections using binutils src_section = mk_section(".data", c_cksum_src_ptr, src) msk_section = mk_section(".data", c_cksum_msk_ptr, mask) # load data mem_image.add_section(src_section) mem_image.add_section(msk_section) return mem_image
def gen_mem_image(): # text section text = (""" # load array pointers csrr x1, mngr2proc < {} # size csrr x2, mngr2proc < 0x2000 # src pointer csrr x14, mngr2proc < 0x3000 # mask pointer csrr x3, mngr2proc < 0x4000 # dst pointer add x4, x0, x1 # loop var i addi x5, x0, 16 # shift amount lw x13, 0(x14) # mask = 0xffff loop_i: add x6, x0, x0 # sum1 = 0 add x7, x0, x0 # sum2 = 0 addi x10, x0, 4 loop_j: lw x8, 0(x2) # load 2 16-bit ints and x9, x8, x13 # src[j] srl x8, x8, x5 # src[j+1] add x6, x6, x9 # sum1 += src[j] and x6, x6, x13 # sum1 &= 0xffff add x7, x7, x6 # sum2 += sum1 and x7, x7, x13 # sum2 &= 0xffff add x6, x6, x8 # sum1 += src[j+1] and x6, x6, x13 # sum1 &= 0xffff add x7, x7, x6 # sum2 += sum1 and x7, x7, x13 # sum2 &= 0xffff addi x2, x2, 4 # j+=2 addi x10, x10, -1 # decrement loop counter bne x10, x0, loop_j add x11, x0, x7 # ret = sum2 sll x11, x11, x5 # ret = ret << 16 add x11, x11, x6 # ret |= sum1 sw x11 0(x3) # src[i] = ret addi x3, x3, 4 addi x4, x4, -1 bne x4, x0, loop_i # End of program csrw proc2mngr, x0 > 0 nop nop nop nop nop nop """.format(c_cksum_size)) mem_image = assemble(text) # load data by manually create data sections using binutils src_section = mk_section(".data", c_cksum_src_ptr, src) msk_section = mk_section(".data", c_cksum_msk_ptr, mask) # load data mem_image.add_section(src_section) mem_image.add_section(msk_section) return mem_image
def gen_mem_image(): # text section text = \ """ csrr x1, mngr2proc < 50 csrr x2, mngr2proc < 0x2000 csrr x3, mngr2proc < 0x3000 csrr x4, mngr2proc < 0x4000 add x9, x0, x1 jal x0, start start: lw x6, 0(x2) lw x7, 0(x3) addi x2, x2, 4 addi x3, x3, 4 jal x0, loop loop: bge x6, x7, elseif addi x28, x6, 0 addi x6, x7, 0 addi x7, x28, 0 jal x0, loop elseif: beq x7, x0, else sub x6, x6, x7 jal x0, loop else: addi x5, x6, 0 sw x5, 0(x4) addi x4, x4, 4 addi x9, x9, -1 bne x9, x0, start jal x0, end end: csrw proc2mngr, x0 > 0 nop nop nop nop nop nop """ mem_image = assemble( text ) # load data by manually create data sections using binutils src0_section = mk_section( ".data", c_vvadd_src0_ptr, src0 ) src1_section = mk_section( ".data", c_vvadd_src1_ptr, src1 ) # load data mem_image.add_section( src0_section ) mem_image.add_section( src1_section ) return mem_image
def gen_mem_image(): # text section text = \ """ csrr x25, mngr2proc < 10 csrr x26, mngr2proc < 0x2000 csrr x27, mngr2proc < 0x3000 add x28, x0, x25 addi x2, x2, 120 # adjust stack size jal x0, main main: lw x10,0(x26) addi x26, x26, 4 jal x1, fib #call the fib function sw x10, 0(x2) #result on stack sw x10, 0(x27) #save to array addi x27, x27, 4 addi x28, x28, -1 bne x28, x0, main jal x0, final fib: addi x2, x2,-12 #save all values and return adress sw x1, 0(x2) sw x8, 4(x2) sw x9, 8(x2) addi x8, x10, 0 beq x0,x8, done #get out of this loop if current one is equal to zero addi x5, x0, 1 beq x5,x8, done #same if current one is equal to one addi x10, x8, -1 #do the loop with n-1 jal x1, fib addi x9,x18, 0 #when we gout out of this loop we put the result of the loop in s1 addi x10,x8, -2 #do the loop with n-2 jal x1, fib add x18,x18,x9 #when we get out, we add the final term of this (n-1)+(n-2) iteration jal x0, end done: addi x18,x8, 0 #value of the term is 0 or 1 jal x0, end end: addi x10,x18, 0 #load result in a0 lw x9,8(x2) #restore all values and stack pointer lw x8,4(x2) lw x1,0(x2) addi x2,x2,12 jalr x0, x1, 0 final: csrw proc2mngr, x0 > 0 nop nop nop nop nop nop """ mem_image = assemble(text) # load data by manually create data sections using binutils src0_section = mk_section(".data", c_vvadd_src0_ptr, src0) # load data mem_image.add_section(src0_section) return mem_image
def gen_mem_image(): # text section text = (""" # load array pointers csrr x1, mngr2proc < {} # size csrr x2, mngr2proc < 0x2000 # src pointer csrr x3, mngr2proc < 0x3000 # mask pointer csrr x4, mngr2proc < 0x4000 # dst pointer addi x5, x0, 16 # shift amount lw x6, 0(x3) # mask = 0xffff add x7, x0, x0 # sum1 = 0 add x8, x0, x0 # sum2 = 0 add x9, x0, x0 # sum1_prev = 0 add x10, x0, x0 # sum2_prev = 0 loop_i: and x7, x9, x6 # sum1 = sum1_prev & 0xffff and x8, x7, x6 # sum2 = sum1 & 0xffff add x7, x7, x10 # sum1 += sum2_prev and x7, x7, x6 # sum1 &= 0xffff add x8, x8, x7 # sum2 += sum1 and x8, x8, x6 # sum2 &= 0xffff addi x11, x0, 3 # j = 3 loop_j: lw x12, 0(x2) # load 2 16-bit ints and x13, x12, x6 # src[i+j] = word & 0xffff srl x12, x12, x5 # src[i+j+1] = word >> 16 add x7, x7, x13 # sum1 += src[i+j] and x7, x7, x6 # sum1 &= 0xffff add x8, x8, x7 # sum2 += sum1 and x8, x8, x6 # sum2 &= 0xffff add x7, x7, x12 # sum1 += src[i+j+1] and x7, x7, x6 # sum1 &= 0xffff add x8, x8, x7 # sum2 += sum1 and x8, x8, x6 # sum2 &= 0xffff addi x2, x2, 4 # increment word address addi x11, x11, -1 # decrement loop counter bne x11, x0, loop_j add x9, x0, x7 # sum1_prev = sum1 add x10, x0, x8 # sum2_prev = sum2 addi x1, x1, -6 # decrement loop counter i bne x1, x0, loop_i add x14, x0, x8 # ret = sum2 sll x14, x14, x5 # ret = sum2 << 16 add x14, x14, x7 # ret |= sum1 sw x14, 0(x4) # End of program csrw proc2mngr, x0 > 0 nop nop nop nop nop nop """.format(c_cksum_size)) mem_image = assemble(text) # load data by manually create data sections using binutils src_section = mk_section(".data", c_cksum_src_ptr, src) msk_section = mk_section(".data", c_cksum_msk_ptr, mask) # load data mem_image.add_section(src_section) mem_image.add_section(msk_section) return mem_image
def gen_mem_image(): # text section text = \ """ csrr x25, mngr2proc < 3 csrr x26, mngr2proc < 0x2000 csrr x27, mngr2proc < 0x3000 add x28, x0, x25 # Load number of tests addi x2, x2, 256 jal x0, Start ############# BRESENHAM # only values from range 1-32 !!! Start: lw x10,0(x26) # Load value to x0 lw x11,4(x26) # Load value to y0 lw x12,8(x26) # Load value to x1 lw x13,12(x26) # Load value to y1 addi x26, x26, 16 addi x2, x2, -156 # Adjust stack sw x1, 172(x2) # Save return address sw x8, 168(x2) # Save previous frame pointer addi x8, x2, 140 # Adjust frame pointer sw x10, 0(x9) # Save x0 sw x11, 4(x9) # Save y0 sw x12, 8(x9) # Save x1 sw x13, 12(x9) # Save y1 sub x10, x10, x12 # dx = x0 - x1 sw x10, 16(x9) # save dx blt x10, x0, .ABS_DX # if dx < 0 count abs(dx) jal x0, .CNT_DY # else go to dy init .ABS_DX: sub x10, x0, x10 # Reverse value sw x10, 16(x9) # save abs(dx) jal x0, .CNT_DY # go to dy init .CNT_DY: sub x11, x11, x13 # dy = y0 - y1 sw x11, 20(x9) # dy = y0 - y1 blt x11, x0, .ABS_DY jal x0, .CNT_SX .ABS_DY: sub x11, x0, x11 # Reverse value sw x11, 20(x9) # save abs(dy) jal x0, .CNT_SX .CNT_SX: lw x10, 0(x9) # load x0 bge x10, x12, .SX_NEG # if (x0 < x1) sx = -1 addi x10, x0, 1 # else sx = 1 sw x10, 24(x9) # save sx jal x0, .CNT_SY .SX_NEG: addi x10, x0, -1 # sx = -1 sw x10, 24(x9) # save sx jal x0, .CNT_SY .CNT_SY: lw x10, 4(x9) # Load y0 bge x10, x13, .SY_NEG # if (y0 < y1) sy = -1 addi x10, x0, 1 # else sy = 1 sw x10, 28(x9) # save sx jal x0, .DX_VS_DY .SY_NEG: addi x10, x0, -1 # sy = -1 sw x10, 28(x9) # save sy jal x0, .DX_VS_DY .DX_VS_DY: lw x10, 16(x9) # load dx blt x11, x10, .SET_ERR # if dy < dx then err = (dx>>1) srai x11, x11, 1 # else (dy>>1) sub x11, x0, x11 # err = - (dy>>1) sw x11, 32(x9) # save err jal x0, .LOOP .SET_ERR: srai x10, x10, 1 # err = (dx>>1) sw x10, 32(x9) # save err jal x0, .LOOP .LOOP: lw x10, 0(x9) # Load x0 addi x10, x10, -1 # x0-1 addi x11, x0, 1 # prepare value 1 sll x10, x11, x10 # shift 1 left (x0-1) times lw x11, 4(x9) # Load y0 slli x11, x11, 2 # multiply by 4 to use as address addi x12, x8, -152 # get proper address in stack add x11, x11, x12 # count targeted address in stack lw x12, -4(x11) # get value of arr[y0-1] or x10, x10, x12 # or it with 1<<(x0-1) sw x10, -4(x11) # save the result in the same place lw x10, 0(x9) # Load x0 lw x11, 8(x9) # Load x1 bne x10, x11, .CONTINUE # if(x0!=x1) continue loop lw x10, 4(x9) # Load y0 (y1 is still in x13) bne x10, x13, .CONTINUE # if(y0!=y1) continue loop jal x0, .BREAK # else BREAK LOOP!!!!!!!! .CONTINUE: lw x10, 32(x9) # load err sw x10, 36(x9) # save it as e2 for future use lw x11, 16(x9) # load dx sub x11, x0, x11 # -dx blt x11, x10, .CONDITION_1 # if(-dx < e2) jal x0, .NEXT_COND .CONDITION_1: lw x11, 20(x9) # Load dy sub x10, x10, x11 # err -= dy sw x10, 32(x9) # save err lw x10, 24(x9) # load sx lw x11, 0(x9) # load x0 add x11, x11, x10 # x0 += sx sw x11, 0(x9) # save x0 jal x0, .NEXT_COND .NEXT_COND: lw x10, 36(x9) # load e2 lw x11, 20(x9) # load dy blt x10, x11, .CONDITION_2 # if(e2 < dy) jal x0, .LOOP # else do another LOOP itex1tion .CONDITION_2: lw x10, 16(x9) # Load dx lw x11, 32(x9) # Load err add x11, x11, x10 # err += dx; sw x11, 32(x9) # save err lw x10, 28(x9) # load sy lw x11, 4(x9) # load y0 add x11, x11, x10 # y0 += sy; sw x11, 4(x9) # save y0 jal x0, .LOOP .BREAK: lw x11 -152(x8) sw x11 0(x27) lw x11 -148(x8) sw x11 4(x27) lw x11 -144(x8) sw x11 8(x27) lw x11 -140(x8) sw x11 12(x27) lw x11 -136(x8) sw x11 16(x27) lw x11 -132(x8) sw x11 20(x27) lw x11 -128(x8) sw x11 24(x27) lw x11 -124(x8) sw x11 28(x27) lw x11 -120(x8) sw x11 32(x27) lw x11 -116(x8) sw x11 36(x27) lw x11 -112(x8) sw x11 40(x27) lw x11 -108(x8) sw x11 44(x27) lw x11 -104(x8) sw x11 48(x27) lw x11 -100(x8) sw x11 52(x27) lw x11 -96(x8) sw x11 56(x27) lw x11 -92(x8) sw x11 60(x27) lw x11 -88(x8) sw x11 64(x27) lw x11 -84(x8) sw x11 68(x27) lw x11 -80(x8) sw x11 72(x27) lw x11 -76(x8) sw x11 76(x27) lw x11 -72(x8) sw x11 80(x27) lw x11 -68(x8) sw x11 84(x27) lw x11 -64(x8) sw x11 88(x27) lw x11 -60(x8) sw x11 92(x27) lw x11 -56(x8) sw x11 96(x27) lw x11 -52(x8) sw x11 100(x27) lw x11 -48(x8) sw x11 104(x27) lw x11 -44(x8) sw x11 108(x27) lw x11 -40(x8) sw x11 112(x27) lw x11 -36(x8) sw x11 116(x27) lw x11 -32(x8) sw x11 120(x27) lw x11 -28(x8) sw x11 124(x27) addi x27, x27, 128 # test counter addi x28, x28, -1 sw x0 -152(x8) # clear memory sw x0 -148(x8) sw x0 -144(x8) sw x0 -140(x8) sw x0 -136(x8) sw x0 -132(x8) sw x0 -128(x8) sw x0 -124(x8) sw x0 -120(x8) sw x0 -116(x8) sw x0 -112(x8) sw x0 -108(x8) sw x0 -104(x8) sw x0 -100(x8) sw x0 -96(x8) sw x0 -92(x8) sw x0 -88(x8) sw x0 -84(x8) sw x0 -80(x8) sw x0 -76(x8) sw x0 -72(x8) sw x0 -68(x8) sw x0 -64(x8) sw x0 -60(x8) sw x0 -56(x8) sw x0 -52(x8) sw x0 -48(x8) sw x0 -44(x8) sw x0 -40(x8) sw x0 -36(x8) sw x0 -32(x8) sw x0 -28(x8) addi x2, x2, 156 # move back stack bne x28, x0, Start #start again csrw proc2mngr, x0 > 0 nop nop nop nop nop nop """ mem_image = assemble(text) # load data by manually create data sections using binutils src0_section = mk_section(".data", c_vvadd_src0_ptr, src0) # load data mem_image.add_section(src0_section) return mem_image
def gen_mem_image(): # text section text = """ # load array pointers csrr x1, mngr2proc < 0x2000 csrr x2, mngr2proc < 0x3000 csrr x3, mngr2proc < 20 csrr x4, mngr2proc < 0x4000 csrr x5, mngr2proc < 0x5000 csrr x6, mngr2proc < 50 addi x7, x0, 0 # loop counter i is in x7 zero: slli x25, x7, 2 # multiply by 4 to get i in the index form add x9, x1, x25 # pointer to i in srch_keys lw x9, 0(x9) # key = srch_keys[i] addi x10, x0, 0 # idx_min srai x11, x6, 1 # idx_mid = dict_sz/2 addi x12, x6, -1 # idx_max = (dict_sz-1) addi x13, x0, 0 # done = false addi x14, x0, -1 # -1 add x15, x2, x25 # i pointer in srch_values sw x14, 0(x15) # srch_values[i] = -1 one: slli x24, x11, 2 # idx_mid in pointer form add x16, x4, x24 # idx_mid pointer in dict_keys lw x17, 0(x16) # midkey = dict_keys[idx_mid] bne x9, x17, two # if ( key == midkey ) goto two: # if block starts add x16, x5, x24 # idx_mid pointer in dict_values lw x18, 0(x16) # dict_values[idx_mid] add x15, x2, x25 # i pointer in srch_values sw x18, 0(x15) # srch_values[i] = dict_values[idx_mid] addi x13, x0, 1 # done = true # if block ends two: slt x18, x17, x9 # midkey < key beq x18, x0, three # if ( midkey < key ) goto three # if block for midkey < key addi x10, x11, 1 # idx_min = idx_mid + 1 jal x0, four # if ( midkey > key ) goto four: # end of if block # else block three: slt x18, x9, x17 # if midkey > key beq x18, x0, four # if ( midkey > key ) goto four: # if block for midkey > key addi x12, x11, -1 # idx_max = idx_mid - 1 # end of if block four: add x20, x10, x12 # idx_min + idx_max srai x11, x20, 1 # idx_mid = ( idx_min + idx_max ) / 2 slt x21, x12, x10 # idx_max < idx_min or x22, x21, x13 # done || (idx_max < idx_min) # while # ( !(done || (idx_max < idx_min)) ) # goto one: beq x22, x0, one addi x7, x7, 1 # i++ bne x7, x3, zero # if (i < srch_sz) goto 0: # end of the program csrw proc2mngr, x0 > 0 nop nop nop nop nop nop """ mem_image = assemble(text) # load data by manually create data sections using binutils d_keys_section = mk_section(".data", c_bin_search_d_keys_ptr, d_keys) mem_image.add_section(d_keys_section) d_values_section = mk_section(".data", c_bin_search_d_values_ptr, d_values) mem_image.add_section(d_values_section) s_keys_section = mk_section(".data", c_bin_search_s_keys_ptr, s_keys) mem_image.add_section(s_keys_section) return mem_image