Commit 002ba2a9 authored by Andrey Filippov's avatar Andrey Filippov
Browse files

ROM data and Python generator for MCLT fold data

parent c78b5a40
Loading
Loading
Loading
Loading

dsp/create_fold_rom.py

0 → 100755
+227 −0
Original line number Original line Diff line number Diff line
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import division
# Copyright (C) 2017, Elphel.inc.
# Helper module create AHCI registers type/default data
# 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/>.
#
'''
Calculate ROM for MCLT fold indices:
A0..A1 - variant, folding to the same 8x8 sample
A2..A4 - sample column in folded 8x8 tile
A5..A7 - sample row in folded 8x8 tile
D0..D4 - pixel column in 16x16 tile
D5..D7 - pixel row in 16x16 tile
D8 -  negate for mode 0 (CC)
D9 -  negate for mode 1 (SC)
D10 - negate for mode 2 (CS)      
D11 - negate for mode 3 (SS)
'''
  
__author__ = "Andrey Filippov"
__copyright__ = "Copyright 2017, Elphel, Inc."
__license__ = "GPL"
__version__ = "3.0+"
__maintainer__ = "Andrey Filippov"
__email__ = "andrey@elphel.com"
__status__ = "Development"
import sys
import math
import os
import datetime
mclt_wnd_rom_path=  '../includes/mclt_fold_rom.vh'

def create_with_parity (init_data,   # numeric data (may be less than full array
                        num_bits,    # number of bits in item, valid:  1,2,4,8,9,16,18,32,36,64,72
                        full_bram):  # true if ramb36, false - ramb18
    d = num_bits
    num_bits8 = 1;
    while d > 1:
        d >>= 1
        num_bits8 <<= 1
    bsize = (0x4000,0x8000)[full_bram]
    bdata = [0  for i in range(bsize)]
    sb = 0
    for item in init_data:
        for bt in range (num_bits8):
            bdata[sb+bt] = (item >> bt) & 1;
        sb += num_bits8
    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)
    data_p = []
    num_bits_p = num_bits8 >> 3
    sb = 0
    print ("num_bits=",num_bits)
    print ("num_bits8=",num_bits8)
    print ("num_bits_p=",num_bits_p)
    if num_bits_p:    
        pbsize = bsize >> 3    
        pbdata = [0  for i in range(pbsize)]
        for item in init_data:
            for bt in range (num_bits_p):
                pbdata[sb+bt] = (item >> (bt+num_bits8)) & 1;
            sb += num_bits_p
        for i in range (len(pbdata)//256):
            d = 0;
            for b in range(255, -1,-1):
                d = (d<<1) +  pbdata[256*i+b]
            data_p.append(d)
    return {'data':data,'data_p':data_p}


def print_params(data,
                 out_file_name,
                 comment=""): # text to add to the file header
    with open(out_file_name,"w") as out_file:
        print ("// Created with "+sys.argv[0], file=out_file)
        if comment:
            print (comment, file=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)

def  get_fold_indices(x, n = 8):
    n1 = n >> 1
#    ind = [[0,0,0,0,0,0],[0,0,0,0,0,0]]# new int[2][6];
    ind = [[0,0,0,0],[0,0,0,0]]# new int[2][6];
    if x < n1:
        ind[0][0] = n + n1 - x - 1 #// C: -cR, S: +cR
        ind[0][1] = n1     + x
        ind[0][2] = -1
        ind[0][3] =  1
#        ind[0][4] = n1     - x -1
#        ind[0][5] =  -1 #// c - window derivative over shift is negative
        
        ind[1][0] = n + n1 + x#;     // C: -d,  S: -d
        ind[1][1] = n1     - x - 1
        ind[1][2] = -1
        ind[1][3] = -1

#        ind[1][4] = n1     + x;
#        ind[1][5] =  -1# // d - window derivative over shift is negative
    else:
        x-=n1;
        ind[0][0] =          x#     // C: +a, S: +a
        ind[0][1] =          x
        ind[0][2] =  1
        ind[0][3] =  1
#        ind[0][4] =  n     - x - 1
#        ind[0][5] =  1#   // a - window derivative over shift is positive
        
        ind[1][0] = n      - x - 1# // C: -bR, S: +bR
        ind[1][1] = n      - x - 1
        ind[1][2] = -1
        ind[1][3] =  1
#        ind[1][4] =          x
#        ind[1][5] =  1#   // b - window derivative over shift is positive
    return ind

def create_fold(n = 8): # n - DCT and window size
    fold_index = (n*n) * [4*[0]] # new int[n*n][4];
    fold_index = []
    for _ in range(n*n):
        fold_index.append([0,0,0,0])
#    fold_k =     new double[4][n*n][4];
#    fold_signs = 4 * [(n*n) * [4*[0]]] 
#    fold_signs = 4 * [(n*n) * [4*[0]]]
    fold_signs = []
    for _ in range(4):
        a = []
        for _a in range(n*n):
            a.append([0,0,0,0])
        fold_signs.append(a)            
    print("fold_signs=",fold_signs) 
    
    vert_ind = [0,0]        # new int[2];
    vert_k =   [[0,0],[0,0]] #new double[2][2];
    hor_ind =  [0,0]        # new int[2];
    hor_k =    [[0,0],[0,0]] #new double[2][2];
    #int [][] fi;
    n2 = 2 * n;
    for i in range(n): # (int i = 0; i < n; i++ ){
        fi = get_fold_indices(i,n)
        vert_ind[0] = fi[0][0];
        vert_ind[1] = fi[1][0];
        vert_k[0][0] =   fi[0][2]# * hwindow[fi[0][1]]; // use cosine sign
        vert_k[0][1] =   fi[1][2]# * hwindow[fi[1][1]]; // use cosine sign
        vert_k[1][0] =   fi[0][3]# * hwindow[fi[0][1]]; // use sine sign
        vert_k[1][1] =   fi[1][3]# * hwindow[fi[1][1]]; // use sine sign
        for j in range(n): # (int j = 0; j < n; j++ ){
            fi = get_fold_indices(j,n)
            hor_ind[0] = fi[0][0];
            hor_ind[1] = fi[1][0];
            hor_k[0][0] =   fi[0][2]# * hwindow[fi[0][1]]; // use cosine sign
            hor_k[0][1] =   fi[1][2]# * hwindow[fi[1][1]]; // use cosine sign
            hor_k[1][0] =   fi[0][3]# * hwindow[fi[0][1]]; // use sine sign
            hor_k[1][1] =   fi[1][3]# * hwindow[fi[1][1]]; // use sine sign
            indx = n * i + j
            for k in range(4): #(int k = 0; k<4;k++) {
                fold_index[indx][k] = n2 * vert_ind[(k>>1) & 1] + hor_ind[k & 1]
            for mode in range(4): # (int mode = 0; mode<4; mode++){
                for k in range(4): #(int k = 0; k<4;k++) {
                    fold_signs[mode][indx][k]=     vert_k[(mode>>1) &1][(k>>1) & 1] * hor_k[mode &1][k & 1]
                    #fold_k[mode][indx][k] =     vert_k[(mode>>1) &1][(k>>1) & 1] * hor_k[mode &1][k & 1]; 
#        for (int i = 0; i < n; i++ ){
#            fi = get_fold_indices(i,n);
#            System.out.println(i+"->"+String.format("?[%2d %2d %2d %2d] [%2d %2d %2d %2d] %f %f",
#                    fi[0][0],fi[0][1],fi[0][2],fi[0][3],
#                    fi[1][0],fi[1][1],fi[1][2],fi[1][3], hwindow[fi[0][1]], hwindow[fi[1][1]]));
#        }
    print("fold_index=",fold_index)
    print("fold_signs=",fold_signs)
    for  i in range(n * n): #(int i = 0; i < n*n; i++){
        print("%3x:   %6x   %6x   %6x   %6x"%(i,fold_index[i][0],fold_index[i][1],fold_index[i][2],fold_index[i][3]))
        print("   :   %2d   %2d   %2d   %2d"%(fold_signs[0][i][0],  fold_signs[0][i][1], fold_signs[0][i][2], fold_signs[0][i][3]))
        print("   :   %2d   %2d   %2d   %2d"%(fold_signs[1][i][0],  fold_signs[1][i][1], fold_signs[1][i][2], fold_signs[1][i][3]))
        print("   :   %2d   %2d   %2d   %2d"%(fold_signs[2][i][0],  fold_signs[2][i][1], fold_signs[2][i][2], fold_signs[2][i][3]))
        print("   :   %2d   %2d   %2d   %2d"%(fold_signs[3][i][0],  fold_signs[3][i][1], fold_signs[3][i][2], fold_signs[3][i][3]))

    fold = (4*n*n)*[0]    
    for var in range(4):
        for i in range(n * n):
            fold[var * 64 + i] = (fold_index[i][var]
                                  + (((0,1)[fold_signs[0][i][var] < 0]) <<  8) +
                                  + (((0,1)[fold_signs[1][i][var] < 0]) <<  9) +
                                  + (((0,1)[fold_signs[2][i][var] < 0]) << 10) +
                                  + (((0,1)[fold_signs[3][i][var] < 0]) << 11))
    return fold

'''
Calculate ROM for MCLT fold indices:
A0..A1 - variant, folding to the same 8x8 sample
A2..A4 - sample column in folded 8x8 tile
A5..A7 - sample row in folded 8x8 tile
D0..D4 - pixel column in 16x16 tile
D5..D7 - pixel row in 16x16 tile
D8 -  negate for mode 0 (CC)
D9 -  negate for mode 1 (SC)
D10 - negate for mode 2 (CS)      
D11 - negate for mode 3 (SS)
'''
                        
print_params(
    create_with_parity(create_fold (), 18, False),
    os.path.abspath(os.path.join(os.path.dirname(__file__), mclt_wnd_rom_path)),
    "// MCLT 16x16 -> 8x8 fold indices")
print ("MCLT 16x16 -> 8x8 fold indices data is written to %s"%(os.path.abspath(os.path.join(os.path.dirname(__file__), mclt_wnd_rom_path))))
                 
+18 −0
Original line number Original line Diff line number Diff line
// Created with ./create_fold_rom.py
// MCLT 16x16 -> 8x8 fold indices
, .INIT_00 (256'h03A303A203A103A006A806A906AA06AB03B303B203B103B006B806B906BA06BB)
, .INIT_01 (256'h038303820381038006880689068A068B039303920391039006980699069A069B)
, .INIT_02 (256'h001300120011001005180519051A051B000300020001000005080509050A050B)
, .INIT_03 (256'h003300320031003005380539053A053B002300220021002005280529052A052B)
, .INIT_04 (256'h06A406A506A606A70CAF0CAE0CAD0CAC06B406B506B606B70CBF0CBE0CBD0CBC)
, .INIT_05 (256'h06840685068606870C8F0C8E0C8D0C8C06940695069606970C9F0C9E0C9D0C9C)
, .INIT_06 (256'h05140515051605170F1F0F1E0F1D0F1C05040505050605070F0F0F0E0F0D0F0C)
, .INIT_07 (256'h05340535053605370F3F0F3E0F3D0F3C05240525052605270F2F0F2E0F2D0F2C)
, .INIT_08 (256'h0FD30FD20FD10FD00AD80AD90ADA0ADB0FC30FC20FC10FC00AC80AC90ACA0ACB)
, .INIT_09 (256'h0FF30FF20FF10FF00AF80AF90AFA0AFB0FE30FE20FE10FE00AE80AE90AEA0AEB)
, .INIT_0A (256'h036303620361036006680669066A066B037303720371037006780679067A067B)
, .INIT_0B (256'h034303420341034006480649064A064B035303520351035006580659065A065B)
, .INIT_0C (256'h0AD40AD50AD60AD700DF00DE00DD00DC0AC40AC50AC60AC700CF00CE00CD00CC)
, .INIT_0D (256'h0AF40AF50AF60AF700FF00FE00FD00FC0AE40AE50AE60AE700EF00EE00ED00EC)
, .INIT_0E (256'h06640665066606670C6F0C6E0C6D0C6C06740675067606770C7F0C7E0C7D0C7C)
, .INIT_0F (256'h06440645064606470C4F0C4E0C4D0C4C06540655065606570C5F0C5E0C5D0C5C)