Beispiel #1
0
def write(q=None,
          inst=0,
          data=1,
          hw='DE-SoC [USB-1]',
          dev='@2: 5CSEBA6(.|ES)/5CSEMA6/.. (0x02D020DD)',
          bits=None,
          begin=True,
          end=True,
          fname=None,
          delete_mif=True):
    '''Writes `data` array to memory instance `inst`.
    Option `delete_mif` will delete temporary .mif file if set to `True`.'''
    data = format_mem(data=data, bits=bits)
    fname = fname or 'write_inst{0}'.format(inst)
    if begin:
        begin_mem(q, hw, dev)
    with open(fname, 'w') as f:
        mif.dump(data, f)
        f.close()
    q.update_content_to_memory_from_file(instance_index=inst,
                                         mem_file_path=fname,
                                         mem_file_type='mif')
    if end:
        end_mem(q)
    if delete_mif:
        os.remove(fname)
Beispiel #2
0
    def write_mem(self, inst, data, delete_mif=False):

        fname = self.path + 'w' + self.name.format(inst)

        self.quartus.begin_memory_edit(hardware_name=self.hwname,
                                       device_name=self.devname)

        try:
            with open(fname, 'w') as f:
                mif.dump(data, f)
                f.close()
            self.quartus.update_content_to_memory_from_file(
                instance_index=inst,
                mem_file_path=fname,
                mem_file_type='mif',
            )
            self.quartus.end_memory_edit()
        except:
            self.quartus.end_memory_edit()
        if delete_mif:
            os.remove(fname)
Beispiel #3
0
def process(input, output, force, word_width, dump_radix):

    if word_width % 8 != 0:
        error('word width must be a multiple of 8')

    word_length = word_width // 8

    # print parameters
    print('======Parameters======')
    print(f'Input file: {input}')
    print(f'Output file: {output}')
    print(f'Word width: {word_width} bits ({word_length} bytes)')
    print('=======Output=========')

    if os.path.exists(output) and not force:
        error('output file existed, use --force to overwrite')

    mem = np.fromfile(input, dtype=np.uint8)

    mem_size = mem.shape[0] # in bytes

    print(f'Input file size: {mem_size} bytes')
    if mem_size % word_length != 0:
        pad_bytes = word_length - mem_size % word_length
        print(f'Padding bytes: {pad_bytes}')
        mem = np.append(mem, np.zeros(shape=(pad_bytes,), dtype=np.uint8))

    mem_size = mem.shape[0]
    word_count = mem_size // word_length

    print(f'Memory size: {mem_size} bytes')
    print(f'Depth (word count): {word_count}')

    # reshape to (address, word)
    mem = mem.reshape((word_count, word_length))

    with open(output, 'w') as f:
        mif.dump(mem, f, packed=True, data_radix=dump_radix)
    
    print('Dump succeeded!')
Beispiel #4
0
def process(input, output, mode, force, channel_width, word_width, threshold,
            dump_radix):

    color = mode == 'rgb'

    pixel_width = channel_width * 3 if color else channel_width

    if word_width == -1:
        word_width = pixel_width

    if word_width % pixel_width != 0:
        error('word width must be a multiple of pixel width')

    pixel_per_word = word_width // pixel_width

    # print parameters
    print('======Parameters======')
    print(f'Input file: {input}')
    print(f'Output file: {output}')
    print(f'Mode: {mode}')
    print(f'Channel width: {channel_width} bits')
    print(f'Word width: {word_width} bits ({pixel_per_word} pixels)')
    if channel_width == 1:
        print(f'Binarization threshold: {threshold}')

    print('=======Output=========')

    if os.path.exists(output) and not force:
        error('output file existed, use --force to overwrite')

    # read image in expected format
    img = cv2.imread(input,
                     cv2.IMREAD_COLOR if color else cv2.IMREAD_GRAYSCALE)
    if img is None:
        error('OpenCV could read your image file')
    if color:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # convert to RGB

    # check image dimension & channel
    w, h = img.shape[0], img.shape[1]
    c = img.shape[2] if color else 1
    print(f'Image shape: {w} * {h} with {c} color channel(s)')
    if c == 1 and color:
        error('Grayscale images could not be used in RGB mode')
    if c != 1 and c != 3:
        error('Unsupported channel number {c}')

    # check & calculate word count
    pixel_num = w * h
    if pixel_num % pixel_per_word != 0:
        error('the picture could not be divided into whole words')
    word_count = w * h // pixel_per_word
    mem_size = word_count * word_width
    print(f'Memory size: {mem_size} bits')
    print(f'Depth (word count): {word_count}')

    # channels to process
    if color:
        channels = cv2.split(img)  # in R, G, B
    else:
        channels = [img]  # in grayscale

    def keep_width(pixel):
        return 0

    # initialize memory in pixels
    mem = np.zeros(shape=(mem_size // pixel_width, pixel_width),
                   dtype=np.uint8)

    # keep only required bits in image
    def process_value(value):
        if channel_width == 1:  # binarization, use given threshold
            return 1 if value > threshold else 0
        else:
            return value >> (8 - channel_width
                             )  # take the highest channel_width bits

    # flatten each channel to 1-D array, then process image to bits
    channels = [c.flatten() for c in channels]
    current_pixel = 0
    for i, pixel in enumerate(
            zip(*channels
                )):  # i: pixel offset, pixel is either (r, g, b) or (gray,)
        for j, c in enumerate(pixel):  # j: channel offset
            processed_value = process_value(c)
            for w in range(channel_width):
                mem[i][j * channel_width +
                       w] = 1 if processed_value & (1 << w) != 0 else 0

    # reshape memory to (address, word)
    mem = mem.reshape((word_count, word_width))
    with open(output, 'w') as f:
        mif.dump(mem, f, data_radix=dump_radix)

    print('Dump succeeded!')
Beispiel #5
0
def write_data(mif_file, mifdata):

    with open(mif_file, "w+") as f:
        mif.dump(mifdata, f)
    return mifdata