convert_data_to_params.py 5.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
# Copyright (C) 2015, Elphel.inc.
# Helper module to convert zigzag ROM for JPEG quantizer
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  
__author__ = "Andrey Filippov"
__copyright__ = "Copyright 2015, Elphel, Inc."
__license__ = "GPL"
__version__ = "3.0+"
__maintainer__ = "Andrey Filippov"
__email__ = "andrey@elphel.com"
__status__ = "Development"
import sys
Andrey Filippov's avatar
Andrey Filippov committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
def create_no_parity (init_data, # numeric data
                      num_bits,    # number of bits in item
                      start_bit,   # bit number to start filling from 
                      full_bram):  # true if raamb36, false - ramb18
    bsize = (0x4000,0x8000)[full_bram]
    bdata = [0  for i in range(bsize)]
    for item in init_data:
        for bt in range (num_bits):
            bdata[start_bit+bt] = (item >> bt) & 1;
        start_bit += num_bits
    data = []
    for i in range (len(bdata)/256):
        d = 0;
        for b in range(255, -1,-1):
            d = (d<<1) +  bdata[256*i+b]
        data.append(d)
#    print(bdata)  
#    print(data)  
    return {'data':data,'data_p':[]}
        
            
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
def create_gamma(curves_data, half):
    mdata = [0 for i in range(2048)]
    index = half * 1024;
    for n in range(4):
        for i in range (256):
            base =curves_data[257*n+i]
            diff =curves_data[257*n+i+1]-curves_data[257*n+i];
            diff1=curves_data[257*n+i+1]-curves_data[257*n+i]+8;
            if ((diff > 63) or (diff < -64)):
                data18 = (base & 0x3ff) | (((diff1 >> 4) & 0x7f) << 10) | 0x20000 # {1'b1,diff1[10:4],base[9:0]};
            else:
                data18 = (base & 0x3ff) | ((diff & 0x7f) <<10) # {1'b0,diff [ 6:0],base[9:0]};
            mdata[index] = data18
#            print ('%03x: %05x (%03x %02x %02x)'%(index,data18, base,diff,diff1))
            index +=1
            
    data=[]
    for a in range(128):
        d=0
        for w in range(16):
            d |= (mdata[16 * a + w] & 0xffff) << (16 * w);
        data.append(d)
    data_p=[]
    for a in range(16):
        d=0
        for w in range(128):
            d |= (mdata[128 * a + w] & 0x3) << (2 * w);
        data_p.append(d)
    return {'data':data,'data_p':data_p}
def print_params(data,out_file_name):
    with open(out_file_name,"w") as out_file:
        for i, v in enumerate(data['data']):
            if v:
                print (", .INIT_%02X (256'h%064X)"%(i,v), file=out_file)
    #    if (include_parity):
        for i, v in enumerate(data['data_p']):
            if v:
                print (", .INITP_%02X (256'h%064X)"%(i,v), file=out_file)
#print ('Number of arguments: %d'%(len(sys.argv)))
#print ('Argument List:%s'%(str(sys.argv)))
88 89
with open(sys.argv[1]) as f:
    tokens=f.read().split()
90 91 92 93 94 95 96 97 98 99
#    print(lines)
#print (lines.split())
values=[]
for w in tokens:
    values.append(int(w,16))
#print (values)
#print (len(values))

#gamma tables
#print (create_gamma(values,1))
Andrey Filippov's avatar
Andrey Filippov committed
100 101 102 103 104 105 106
if sys.argv[1].find("1028") >= 0:
    print_params(create_gamma(values,0),sys.argv[1]+"0.vh")            
    print_params(create_gamma(values,1),sys.argv[1]+"1.vh")            
elif sys.argv[1].find("huffman") >= 0:
    print_params(create_no_parity(values,32,0,False),sys.argv[1]+".vh")
else:
    print_params(create_no_parity(values,16,0,False),sys.argv[1]+".vh")
107 108
            
'''
Andrey Filippov's avatar
Andrey Filippov committed
109 110 111 112 113
 create_no_parity (init_data, # numeric data
                      num_bits,    # number of bits in item
                      start_bit,   # bit number to start filling from 
                      full_bram):  # true if raamb36, false - ramb18

114
// INIT_00 to I.INIT_00(256'h0
Andrey Filippov's avatar
Andrey Filippov committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
task program_huffman;
// huffman tables data
  reg   [23:0]   huff_data[0:511];
  integer i;
  begin
    $readmemh("huffman.dat",huff_data);
    cpu_wr ('he,'h200);   // start address of huffman tables
    for (i=0;i<512;i=i+1) begin
      cpu_wr('hf,huff_data[i]);
    end
  end
endtask

task program_quantization;
// quantization tables data
//  reg   [11:0]   quant_data[0:255];
  reg   [15:0]   quant_data[0:255];
  integer i;
  begin
//    $readmemh("quantization.dat",quant_data);
    $readmemh("quantization_100.dat",quant_data);
    cpu_wr ('he,'h0);   // start address of quantization tables
    for (i=0;i<256;i=i+2) begin
      cpu_wr('hf,{quant_data[i+1],quant_data[i]});
139
    end
Andrey Filippov's avatar
Andrey Filippov committed
140
  end
141 142
endtask

Andrey Filippov's avatar
Andrey Filippov committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
task program_coring;
// coring tables data
  reg   [15:0]   coring_data[0:1023];
  integer i;
  begin
//    $readmemh("quantization.dat",quant_data);
    $readmemh("coring.dat",coring_data);
    cpu_wr ('he,'hc00);   // start address of coring tables
    for (i=0;i<1024;i=i+2) begin
      cpu_wr('hf,{coring_data[i+1],coring_data[i]});
    end
  end
endtask


158
'''