/*
 *  Source File of IPC cmd Task
 *     It is verilog tasks in BB
 * :set tabstop=4
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include "acc_user.h"
#include "veriuser.h"
#include "ipc.h"
#include "sim.h"
#include "req.h"
#include "req_cmd.h"
#include <sc/sc.h>

extern int ipc_mon;
extern long long cur_time; 

rtl_tm_adv tm_adv[IPC_CMD_TM_ADV_NUM_TYPE] = {
  {"Posedge @ gba clk",  IPC_CMD_TM_ADV_GBA_POS, IPC_REG_GBA_POS_TICK, 
    IPC_GBA_CLK_PERIOD * 1000},
  {"Posedge @ core clk", IPC_CMD_TM_ADV_CORE_POS, IPC_REG_CORE_POS_TICK,  10000},
  {"@ ns ",              IPC_CMD_TM_ADV_NS, IPC_REG_NS_TICK, 1000},
  {"Posedge @ cpu0 clk", IPC_CMD_TM_ADV_CPU0_NEG, IPC_REG_CPU0_NEG_TICK, 10000},
  {"Posedge @ cpu1 clk", IPC_CMD_TM_ADV_CPU0_NEG, IPC_REG_CPU1_NEG_TICK, 10000},
  {"Posedge @ xtal clk", IPC_CMD_TM_ADV_XTAL_POS, IPC_REG_XTAL_POS_TICK, 75000}
};

int time_advance_init()
{
    set_u64_value(IPC_REG_WAIT_TO, IPC_CLK_MAX);

    return 0; 
}

int cal_time_advance_req(p_ipc_req preq, unsigned int id, int tm_type, int num)
{
    long long cur_to, next_to;
    int i, type = -1;

    cur_to = get_u64_value(IPC_REG_WAIT_TO);
    for (i=0; i<IPC_CMD_TM_ADV_NUM_TYPE; i++) {
        if (tm_type == tm_adv[i].type) {
            type = i;
            break; 
        }
    }

    if (type < 0) {
        printf("ERROR: invalid time advance type(0x%x)\n", tm_type);
        return -1;
    }

    next_to    = get_u64_value(tm_adv[type].rtl_tick);
    if (num > 0) next_to += (long long) tm_adv[type].period * num / 1000;
    else next_to = cur_time + 1;
    preq->t = next_to;

    return 0;
}

int set_time_period(int tm_type, int num)
{
    int i, type = -1;

    for (i=0; i<IPC_CMD_TM_ADV_NUM_TYPE; i++) {
        if (tm_type == tm_adv[i].type) {
            type = i;
            break; 
        }
    }

    printf("Clock %d period set from %dns to %dns \n", type, tm_adv[type].period, num);
    tm_adv[type].period = num;

    return 0;
}

int time_advance_req(p_ipc_req preq, unsigned int id, int tm_type, int num)
{
    long long next_to, cur_to;

    cur_to = get_u64_value(IPC_REG_WAIT_TO);
    next_to = preq->t;

    if (next_to < cur_to) {
        set_u64_value(IPC_REG_WAIT_TO, next_to);
        set_u64_value(IPC_REG_WAIT_ID, id);
        if (tm_type == IPC_CMD_TM_ADV_NS) 
            set_value(IPC_REG_NS_CLK_EN, 1);
        else set_value(IPC_REG_NS_CLK_EN, 0);
        set_value(IPC_REG_TIMER_TYPE, tm_type);
    }

    return 0;
}

int get_sock_status(p_ipc_socket psock)
{
    int i, status = 0;

    for (i=0; i<IPC_SOCK_NUM; i++) {
        if (psock[i].fd > 0) 
            status |= (1<<i);
    }

    return status;
}

void gba_mem_bd_ops(int id, int rd, int offs, int count, int type)
{
    unsigned int pli_cmd;
    
    pli_cmd = count & IPC_PLI_CMD_MASK;
    pli_cmd |= (rd ? IPC_PLI_READ : IPC_PLI_WRITE);
          
    set_value(IPC_REG_GBA_PLI_ID, id);
    set_value(IPC_REG_GBA_PLI_ADDR, offs);
    set_value(IPC_REG_GBA_PLI_CMD, pli_cmd);
    set_nb_value(IPC_REG_GBA_PLI_DO, 1);
}

int gba_mem_sz(int count, int type)
{
    int num=0;
    
    if (type == IPC_SIM_TYPE_16_BIT_D) 
        num = count;

    if (type == IPC_SIM_TYPE_16_BIT_XZ) 
        num = count + count;

    return num * sizeof(short);
}

unsigned int * gba_mem_get(int offs, int count, int type)
{
    unsigned int mem_sz;
    unsigned short *pdata = NULL;

    if ((type != IPC_SIM_TYPE_16_BIT_D) && (type != IPC_SIM_TYPE_16_BIT_XZ)) {
        printf("ERROR: Wrong gba_mem_get type 0x%x \n", type);
        return NULL;
    }

    mem_sz = (gba_mem_sz(count, type) + 3) & (~3); 
    if (mem_sz) pdata = (unsigned short *) malloc(mem_sz);
    if (!pdata) {
        printf("ERROR: Memory alloc failed %d gba_mem_get\n", mem_sz);
        return NULL;
    }     
    
    return ((unsigned int *) pdata);
}

int gba_set_wait(int wait1, int wait2)
{
    int busy;

    busy = get_value(IPC_REG_GBA_BUSY);
    if (busy) {
        printf("ERROR: GBA is busy(changing waiting states).\n");
        printf("WANING: GBA wait state is %d %d\n",
            get_value(IPC_REG_GBA_WAIT1), 
            get_value(IPC_REG_GBA_WAIT2));
        return -1;
    }

    set_value(IPC_REG_GBA_WAIT1, wait1);
    set_value(IPC_REG_GBA_WAIT2, wait2);

    return 0;
}

void gba_rom_ops(int type, int ptr, int addr, int num)
{
    set_value(IPC_REG_GBA_TYPE, type);
    set_value(IPC_REG_GBA_PTR, ptr);
    set_value(IPC_REG_GBA_ADDR, addr);
    set_value(IPC_REG_GBA_NWORD, num);
    set_nb_value(IPC_REG_GBA_BUSY, 1);
}

int gba_bus_trans(int id, int type)
{
    if (get_value(IPC_REG_GBA_RST)) {
        printf("ERROR: Gba bus is in rst\n");
        return 0;
    }
    
    if (get_value(IPC_REG_GBA_BUSY)) {
        printf("ERROR: Gba bus is busy(rom read)\n");
        return 0;
    }

    set_value(IPC_REG_GBA_TID, id & (~IPC_REQ_ID_COMMIT));
    set_value(IPC_REG_GBA_TYPE, type);
    set_nb_value(IPC_REG_GBA_BUSY, 1);

    return 1;
}
int gba_rom_read(int id, int ptr, int addr, int num)
{
    if (get_value(IPC_REG_GBA_RST)) {
        printf("ERROR: Gba bus is in rst\n");
        return 0;
    }
    
    if (get_value(IPC_REG_GBA_BUSY)) {
        printf("ERROR: Gba bus is busy(rom read)\n");
        return 0;
    }
    set_value(IPC_REG_GBA_TID, id & (~IPC_REQ_ID_COMMIT));
    gba_rom_ops(IPC_CMD_GBA_TYPE_ROM_READ, ptr, addr, num);

    return 1;
}

int gba_rom_write(int id, int ptr, int addr, int num)
{
    if (get_value(IPC_REG_GBA_RST)) {
        printf("ERROR: Gba bus is in rst\n");
        return 0;
    }
    if (get_value(IPC_REG_GBA_BUSY)) {
        printf("ERROR: Gba bus is busy(rom write)\n");
        return 0;
    }

    set_value(IPC_REG_GBA_TID, id & (~IPC_REQ_ID_COMMIT));
    gba_rom_ops(IPC_CMD_GBA_TYPE_ROM_WRITE, ptr, addr, num);
    return 1;
}

int gba_sram_rw(int id, int addr, int data, int xz, int rd)
{
    unsigned int d, x;

    d = ((data & 0xff) << 16) | (addr & 0xffff);
    x = (xz & 0xff) << 16; 

    if (get_value(IPC_REG_GBA_RST)) {
        printf("ERROR: Gba bus is in rst\n");
        return 0;
    }

    if (get_value(IPC_REG_GBA_BUSY)) {
        printf("ERROR: Gba bus is busy(sram %s)\n", 
            rd ? "read" : "write");
        return 0;
    }

    set_value(IPC_REG_GBA_TYPE, 
        rd ? IPC_CMD_GBA_TYPE_SRAM_READ : IPC_CMD_GBA_TYPE_SRAM_WRITE);

    if (xz)    set_value_xz(IPC_REG_GBA_ADDR, d, xz); 
    else set_value(IPC_REG_GBA_ADDR, d);

    set_value(IPC_REG_GBA_TID, id & (~IPC_REQ_ID_COMMIT));    
    set_nb_value(IPC_REG_GBA_BUSY, 1);
    return 1;
}

int cpu_set_mode(int wh, int m, int t, int p)
{
    if (wh == IPC_SOCK_CPU0)  {
        if (get_value(IPC_REG_SC_FPGA)) {
            set_value(IPC_REG_FPGA_CPU0_MODE, m); 
            set_value(IPC_REG_FPGA_CPU0_THUMB, t); 
            set_value(IPC_REG_FPGA_CPU0_PRIV, p); 
        } else {
            set_value(IPC_REG_CPU0_MODE, m); 
            set_value(IPC_REG_CPU0_THUMB, t); 
            set_value(IPC_REG_CPU0_PRIV, p); 
        }
    } else {
        if (get_value(IPC_REG_SC_FPGA)) {
            set_value(IPC_REG_FPGA_CPU1_MODE, m); 
            set_value(IPC_REG_FPGA_CPU1_THUMB, t); 
            set_value(IPC_REG_FPGA_CPU1_PRIV, p); 
        } else {
            set_value(IPC_REG_CPU1_MODE, m); 
            set_value(IPC_REG_CPU1_THUMB, t); 
            set_value(IPC_REG_CPU1_PRIV, p); 
        }
    }

    return 0;
}

int cpu_get_mode(int wh, int *m, int *t, int *p)
{
    if (wh == IPC_SOCK_CPU0)  {
        if (get_value(IPC_REG_SC_FPGA)) {
            *m = get_value(IPC_REG_FPGA_CPU0_MODE); 
            *t = get_value(IPC_REG_FPGA_CPU0_THUMB); 
            *p = get_value(IPC_REG_FPGA_CPU0_PRIV); 
        } else {
            *m = get_value(IPC_REG_CPU0_MODE); 
            *t = get_value(IPC_REG_CPU0_THUMB); 
            *p = get_value(IPC_REG_CPU0_PRIV); 
        }
    } else {
        if (get_value(IPC_REG_SC_FPGA)) {
            *m = get_value(IPC_REG_FPGA_CPU1_MODE); 
            *t = get_value(IPC_REG_FPGA_CPU1_THUMB); 
            *p = get_value(IPC_REG_FPGA_CPU1_PRIV); 
        } else {
            *m = get_value(IPC_REG_CPU1_MODE); 
            *t = get_value(IPC_REG_CPU1_THUMB); 
            *p = get_value(IPC_REG_CPU1_PRIV); 
        }
    }

    return 0;
}

int cpu_handle_issue(unsigned int id, unsigned int *pdata)
{
    if (pdata == NULL) {
        printf("ERROR: CPU parameter wrong\n");
        return 0;
    }
    
    switch (pdata[0]) {
    case IPC_SOCK_CPU0:
        if (get_value(IPC_REG_SC_FPGA)) {
            set_value(IPC_REG_FPGA_CPU0_ID, id);
            set_value(IPC_REG_FPGA_CPU0_ADDR, pdata[1]);
            set_value(IPC_REG_FPGA_CPU0_SIZE, pdata[2]);
            set_value(IPC_REG_FPGA_CPU0_WR, pdata[3]);
            set_value(IPC_REG_FPGA_CPU0_LOCK, pdata[4]);
            set_value(IPC_REG_FPGA_CPU0_LS, pdata[5]);
            set_value(IPC_REG_FPGA_CPU0_TYPE, pdata[6]);
            set_value_xz(IPC_REG_FPGA_CPU0_WDATA, pdata[7], pdata[8]);
            set_nb_value(IPC_REG_FPGA_CPU0_BUSY, 1);
        } else {
            set_value(IPC_REG_CPU0_ID, id);
            set_value(IPC_REG_CPU0_ADDR, pdata[1]);
            set_value(IPC_REG_CPU0_SIZE, pdata[2]);
            set_value(IPC_REG_CPU0_WR, pdata[3]);
            set_value(IPC_REG_CPU0_LOCK, pdata[4]);
            set_value(IPC_REG_CPU0_LS, pdata[5]);
            set_value(IPC_REG_CPU0_TYPE, pdata[6]);
            set_value_xz(IPC_REG_CPU0_WDATA, pdata[7], pdata[8]);
            set_nb_value(IPC_REG_CPU0_BUSY, 1);
        }
        break;
    case IPC_SOCK_CPU1:
        if (get_value(IPC_REG_SC_FPGA)) {
            set_value(IPC_REG_FPGA_CPU1_ID, id);
            set_value(IPC_REG_FPGA_CPU1_ADDR, pdata[1]);
            set_value(IPC_REG_FPGA_CPU1_SIZE, pdata[2]);
            set_value(IPC_REG_FPGA_CPU1_WR, pdata[3]);
            set_value(IPC_REG_FPGA_CPU1_LOCK, pdata[4]);
            set_value(IPC_REG_FPGA_CPU1_LS, pdata[5]);
            set_value(IPC_REG_FPGA_CPU1_TYPE, pdata[6]);
            set_value_xz(IPC_REG_FPGA_CPU1_WDATA, pdata[7], pdata[8]);
            set_nb_value(IPC_REG_FPGA_CPU1_BUSY, 1);
        } else {
            set_value(IPC_REG_CPU1_ID, id);
            set_value(IPC_REG_CPU1_ADDR, pdata[1]);
            set_value(IPC_REG_CPU1_SIZE, pdata[2]);
            set_value(IPC_REG_CPU1_WR, pdata[3]);
            set_value(IPC_REG_CPU1_LOCK, pdata[4]);
            set_value(IPC_REG_CPU1_LS, pdata[5]);
            set_value(IPC_REG_CPU1_TYPE, pdata[6]);
            set_value_xz(IPC_REG_CPU1_WDATA, pdata[7], pdata[8]);
            set_nb_value(IPC_REG_CPU1_BUSY, 1);
        }
        break;
    default:
        printf("ERROR: Invalid request wh=%d\n", pdata[0]);
        return 0;    
    }

    return 1;
}

int
checkTagAddr(char *name, int cfg, int addr, int rstart, int rsz, int sz)
{
    int taddr;

    if (rsz < 0) {
        printf("ERROR: acc 0x%x but no %s (cfg=0x%x)\n",
                    addr, name, cfg);
        return -1;
    }    
   
    /* Remove cache/ops/address bits */
    taddr = addr & (~(CPS_CACHE_MASK | CPS_OP_MASK | CPS_START));
    if ((taddr + sz) > rsz) {
        printf("ERROR: %s out of range:cfg=0x%x 0x%x 0x%x\n",
                name, cfg, taddr, sz);
        return -1;
    } else     taddr += rstart;

    return taddr;
}

/* Convert input address into cache tag addr 
   return -1 if failed 
 */
int 
convertTagAddr(unsigned int addr, int sz)
{
    int taddr = -1;
    int cfg;
    int da_start, da_sz, sc_start, sc_sz, cc_start, cc_sz;

    if ((addr & 0xf) || (sz & 0xf)) {
        printf("ERROR: tag backdoor ops 0x%x 0x%x %s\n", 
                addr, sz, "not cacheline aligned");
        return -1;
    }

    da_start = sc_start = cc_start = 0;
    da_sz = cc_sz = sc_sz = -1;
    cfg = get_value(IPC_REG_TAG_CACHE_CFG) & IPC_CACHE_CFG_MASK;         
    switch (cfg) {
    case 0:
        da_sz = IPC_CTAG_320K;
        break;
    case 1:
        sc_sz = IPC_CTAG_256K;
        sc_start = IPC_CTAG_64K;
        da_sz = IPC_CTAG_64K;
        break;
    case 2:
        cc_start = IPC_CTAG_64K;
        cc_sz = IPC_CTAG_256K;
        da_sz = IPC_CTAG_64K;
        break;
    case 3:
        sc_sz = cc_sz = IPC_CTAG_128K;
        sc_start = IPC_CTAG_192K; 
        cc_start = IPC_CTAG_64K;
        da_sz = IPC_CTAG_64K;
        break;
    case 4:
        cc_sz = IPC_CTAG_128K;
        cc_start = IPC_CTAG_192K; 
        da_sz = IPC_CTAG_192K;
        break;
    case 5:
        da_sz = IPC_CTAG_192K;
        sc_sz = cc_sz = IPC_CTAG_64K;
        sc_start = IPC_CTAG_256K;
        cc_start = IPC_CTAG_192K;
        break;
    case 6:
        sc_sz = da_sz = IPC_CTAG_128K;
        cc_sz = IPC_CTAG_64K;        
        sc_start = IPC_CTAG_192K;
        cc_start = IPC_CTAG_128K;
        break;
    case 7:
        cc_sz = da_sz = IPC_CTAG_128K;
        sc_sz = IPC_CTAG_64K;
        cc_start = IPC_CTAG_192K;
        sc_start = IPC_CTAG_128K;
        break;
    default:
        printf("ERROR: Invalid cache configuration\n");
        return -1;
    }

    if (CPS_CACHE(addr) == CPS_CACHE_DA) { 
        taddr = checkTagAddr("DA", cfg, addr, da_start, da_sz, sz);
    } else if (CPS_CACHE(addr) == CPS_CACHE_SC) {
        taddr = checkTagAddr("SC", cfg, addr, sc_start, sc_sz, sz);
    } else if (CPS_CACHE(addr) == CPS_CACHE_CC) {
        taddr = checkTagAddr("CC", cfg, addr, cc_start, cc_sz, sz);
    }

    return taddr;
}

int ipc_pg_size_bit()
{
    int cfg;
    cfg = get_value(IPC_REG_TAG_CACHE_CFG) & (~IPC_CACHE_CFG_MASK);

    return (cfg ? 13 : 14);
}

#define ENDIAN_SWAP_WORD(d)  ( (((d) & 0xff) << 24) | \
                               (((d) & 0xff00) << 8) | \
                               (((d) & 0xff0000) >> 8) | \
                               (((d) >> 24) & 0xff) )
unsigned int *     /* return buffer to hold data */
cpu_bd_read(unsigned int id, int wh, unsigned int addr, int sz, int xz_type) 
{
    void *p = NULL;
    int msz, wsz;
    unsigned int mpli_cmd;
    int pg_sz, pg_mask, taddr;

#ifndef CVER
    char *gate[5]={IPC_MOD_CRAM_GATE0, IPC_MOD_CRAM_GATE1, IPC_MOD_CRAM_GATE2, IPC_MOD_CRAM_GATE3, IPC_MOD_CRAM_GATE4};
    char *nm, snm[256];
    unsigned short *p16;
    unsigned int caddr, i, d, xz, bk, *p32, idx, d1, xz1;
#endif

    if (sz == IPC_CMD_CPU_SZ_WORD) {
        wsz = 4;
    } else if (sz & 3) {
        printf("ERROR: cpu_bd_read PLI Invalid size(%d) \n", sz);
        return NULL;
    } else wsz = sz;
    mpli_cmd = (wsz & IPC_PLI_CMD_MASK) | IPC_PLI_READ;

    if ((addr >= CPS_START) && (addr < CPS_END)) {
        if (CPS_CACHE(addr) != CPS_CACHE_PT) {
            taddr = convertTagAddr(addr, wsz);
            if (taddr < 0) return NULL;
 
            msz = (xz_type) ? sizeof(short)*2 : sizeof(short);
            msz *= (wsz >> 4);   /* align to 16 Bytes cacheline */
            p = malloc(msz);         
            if (p == NULL) {
                printf("ERROR: alloc memory for cops backdoor\n");
            } else {
#ifdef CVER
                set_value(IPC_REG_TAG_PLI_ID, id);
                set_value(IPC_REG_TAG_PLI_ADDR, taddr);
                set_value(IPC_REG_TAG_PLI_CMD, mpli_cmd);
                set_nb_value(IPC_REG_TAG_PLI_DO, 1);
#else 
                p16 = (unsigned short *) p;
                for (i=0; i<wsz;i+=16) {
                    caddr = IPC_TAG_IDX(taddr + i);
                    if (get_value(IPC_REG_TAG_PLI_MODEL)) 
                        nm = IPC_MEM_TAG_MODEL;
                    else if (get_value(IPC_REG_SC_FPGA)) 
                        nm = IPC_MEM_TAG_FPGA;
                    else {
                        if (get_value(IPC_REG_SC_GATE)) {
                            nm = (caddr & 1) ? IPC_MEM_GATE_TAG1 : IPC_MEM_GATE_TAG0;
                        } else nm = (caddr & 1) ? IPC_MEM_TAG1 : IPC_MEM_TAG0;
                        caddr >>= 1;
                    }
                    sprintf(snm, "%s[%d]", nm, caddr);
                    get_value_xz(snm, &d, &xz);
                    if (xz_type) {
                        *p16++ = d;
                        *p16++ = xz;
                    } else *p16++ = d;
                }
#endif
            }
            return p;
        } else {
            /* backdoor access Page table */
            pg_sz = ipc_pg_size_bit();
            pg_mask = (1 << pg_sz) - 1;
            if ((addr | wsz) & pg_mask) {
                printf("ERROR: pg_bd p=%d addr=0x%x sz=0x%x",
                    pg_sz, addr, sz);
                return NULL;
            }    
            msz = (xz_type) ? sizeof(short)*2 : sizeof(short);
            msz *= (wsz >> pg_sz);   /* align to page size */
            p = malloc(msz);         
            if (p == NULL) {
                printf("ERROR: alloc memory for pt backdoor\n");
            } else {
#ifdef CVER
                set_value(IPC_REG_PT_PLI_ID, id);
                set_value(IPC_REG_PT_PLI_ADDR, addr & 
                            (~(CPS_CACHE_MASK | CPS_OP_MASK | CPS_START)));
                set_value(IPC_REG_PT_PLI_CMD, mpli_cmd);
                set_nb_value(IPC_REG_PT_PLI_DO, 1);
#else
                p16 = (unsigned short *) p;
                if (get_value(IPC_REG_PT_PLI_MODEL))
                    nm = IPC_MEM_PT_MODEL;
                else if (get_value(IPC_REG_SC_FPGA))
                    nm = IPC_MEM_PT_FPGA;
                else nm = get_value(IPC_REG_SC_GATE) ? IPC_MEM_PT_GATE : IPC_MEM_PT;

                for (i=0; i<wsz; i+=(1<<pg_sz)) {
                    caddr = (addr+i) & (~(CPS_CACHE_MASK | CPS_OP_MASK | CPS_START));
                    caddr >>= pg_sz;
                    sprintf(snm, "%s[%d]", nm, caddr);
                    get_value_xz(snm, &d, &xz);
                    if (xz_type) {
                        *p16++ = d;
                        *p16++ = xz;
                    } else *p16++ = d;
                }
#endif
            }
            return p;    
        }
    } else if ( ((addr >= MEM0_START) && (addr < MEM0_END)) ||
                ((addr >= MEM1_START) && (addr < MEM1_END)) ||
                ((addr >= MEM2_START) && (addr < MEM2_END)) ||
                ((addr >= MEM3_START) && (addr < MEM3_END))) {
        /* Backdoor access SDRAM */
        msz = (xz_type) ? wsz * 2 : wsz;
        p = malloc(msz);    
        if (p == NULL) {
            printf("ERROR: alloc memory for sdram backdoor\n");
        } else {
#ifdef CVER
            set_value(IPC_REG_SDRAM_PLI_ID, id);
            set_value(IPC_REG_SDRAM_PLI_ADDR, addr & IPC_SDRAM_ADDR_MASK);
            set_value(IPC_REG_SDRAM_PLI_CMD, mpli_cmd);
            set_nb_value(IPC_REG_SDRAM_PLI_DO, 1);
#else
            p32 = (unsigned int *) p;
            caddr = addr & IPC_SDRAM_ADDR_MASK;
            for (i=0; i<wsz; i+=4, caddr+=4) {
                bk = (caddr >> 3) & 0x3;    /* which bank */
                //{[24], [22], [21:11], [23], [10:5], [2:1] 
                idx = (caddr >> 24) & 1;
                idx = (idx << 1) | ((caddr >> 22) & 1);
                idx = (idx << 11) | ((caddr >> 11) & 0x7FF);
                idx = (idx << 1) | ((caddr >> 23) & 1);
                idx = (idx << 6) | ((caddr >> 5) & 0x3F);
                idx = (idx << 2) | ((caddr >> 1) & 0x3);
                if (get_value(IPC_SD_PART) == 0) {
                    sprintf(snm, "%s%d[%d]", IPC_BK_SDRAM, bk, idx);
                    get_value_xz(snm, &d, &xz);
                    sprintf(snm, "%s%d[%d]", IPC_BK_SDRAM, bk, idx+1);
                    get_value_xz(snm, &d1, &xz1);
                } else {
                    idx = (bk << 22) | idx;
                    sprintf(snm, "%s[%d]", IPC_SD_INFINEON_ARR, idx);
                    get_value_xz(snm, &d, &xz);
                    sprintf(snm, "%s[%d]", IPC_SD_INFINEON_ARR, idx+1);
                    get_value_xz(snm, &d1, &xz1);
                }
                if (xz_type) {
                    *p32++ = (d1 << 16) | d;
                    *p32++ = (xz1 << 16) | xz;
                } else *p32++ = (d1 << 16) | d;
            }
#endif
        }
        return p;
    } else if (((addr >= BOOT_START) && (addr < BOOT_END)) ||
               ((addr >= ALT_ROM_START) && (addr < ALT_ROM_END))) {
        int swap = get_value(IPC_REG_ROM_FLIP);
        msz = (xz_type) ? wsz * 2 : wsz;
        p = malloc(msz);

        if (p != NULL) {
            if ((swap && (addr >= BOOT_START) && (addr < BOOT_END)) ||
                (!swap && (addr >= ALT_ROM_START) && (addr < ALT_ROM_END))) { 
#ifdef CVER
                /* Cache ram access, (not only DA !!!) */
                set_value(IPC_REG_PLI_CRAM_ID, id);
                set_value(IPC_REG_PLI_CRAM_ADDR, 
                         addr - (swap ? BOOT_START : ALT_ROM_START));
                set_value(IPC_REG_PLI_CRAM_CMD, mpli_cmd);
                set_nb_value(IPC_REG_PLI_CRAM_DO, 1);
#else
                p32 = (unsigned int *) p;
                if (get_value(IPC_REG_CRAM_MODEL))  nm = IPC_MEM_CRAM_MODEL;
                else if (get_value(IPC_REG_SC_FPGA)) nm = IPC_MEM_CRAM_FPGA;
                else nm = IPC_MEM_CRAM;
        
                for (i=0; i<wsz; i+=4) {
                    caddr = addr - (swap ? BOOT_START : ALT_ROM_START) + i;
                    if (get_value(IPC_REG_SC_GATE)) 
                        sprintf(snm, "%s%d.%s[%d]", IPC_MOD_CRAM, (caddr >> 16) & 7, 
                                     gate[(caddr >> 16) & 7],  (caddr&0xffff) >> 2);
                    else sprintf(snm, "%s%d.%s[%d]", IPC_MOD_CRAM, (caddr >> 16) & 7, nm, (caddr&0xffff) >> 2);
                    get_value_xz(snm, &d, &xz);
                    if (xz_type) {
                        *p32++ = d; 
                        *p32++ = xz;
                    } else *p32++ = d;
                }
#endif
            } else {
                /* Rom space */
#ifdef CVER
                set_value(IPC_REG_PLI_ROM_ID, id);
                set_value(IPC_REG_PLI_ROM_ADDR, 
                            addr - (swap ? ALT_ROM_START : BOOT_START));
                set_value(IPC_REG_PLI_ROM_CMD, mpli_cmd);
                set_nb_value(IPC_REG_PLI_ROM_DO, 1);
#else 
                p32 = (unsigned int *) p;
                if (get_value(IPC_REG_ROM_MODEL)) nm = IPC_MEM_ROM_MODEL;
                else if (get_value(IPC_REG_SC_FPGA)) nm = IPC_MEM_ROM_FPGA;
                else if (get_value(IPC_REG_SC_GATE)) nm = IPC_MEM_GATE_ROM;
		else nm = IPC_MEM_ROM;

                for (i=0; i<wsz; i+=4) {
                    caddr = addr - (swap ? ALT_ROM_START : BOOT_START) + i;
                    sprintf(snm, "%s[%d]", nm, caddr >> 2);
                    get_value_xz(snm, &d, &xz);
                    if (xz_type) {
                        *p32++ = d;
                        *p32++ = xz;
                    } else *p32++ = d;
                }  
#endif
            }
        } else printf("ERROR: alloc memory for cram backdoor\n");
        return p;
    } 

    printf("ERROR: Backdoor addr 0x%x not supported\n", addr);    
    return NULL;   
}

int 
cpu_bd_write(unsigned int id, int wh, unsigned int addr,
    int sz, int xz_type, unsigned int *pdata, int fill)
{
    unsigned int mpli_cmd;
    int pg_sz, pg_mask, taddr, wsz;
#ifndef CVER
    char *gate[5]={IPC_MOD_CRAM_GATE0, IPC_MOD_CRAM_GATE1, IPC_MOD_CRAM_GATE2, IPC_MOD_CRAM_GATE3, IPC_MOD_CRAM_GATE4};
    char *nm, snm[256];
    unsigned short *p16;
    unsigned int caddr, i, bk, *p32, idx, d, d1, xz1, xz;
#endif

    if (sz == IPC_CMD_CPU_SZ_WORD) {
        wsz = 4;
    } else if (sz & 3) {
        printf("ERROR: cpu_bd_write PLI Invalid size(%d) \n", sz);
        return 0;
    } else wsz = sz;
    mpli_cmd = (wsz & IPC_PLI_CMD_MASK) | IPC_PLI_WRITE;

    if ((addr >= CPS_START) && (addr < CPS_END)) {
        if (CPS_CACHE(addr) != CPS_CACHE_PT) {
            taddr = convertTagAddr(addr, sz);
            if (taddr < 0) return 0;

            if (CPS_OP(addr) != CPS_OP_SET) {
                printf("ERROR: only set ops is supported\n");
                return 0;
            } 
#ifdef CVER
            set_value(IPC_REG_TAG_PLI_ID, id);
            set_value(IPC_REG_TAG_PLI_ADDR, taddr);
            set_value(IPC_REG_TAG_PLI_CMD, mpli_cmd);
            set_nb_value(IPC_REG_TAG_PLI_DO, 1);
#else
            p16 = (unsigned short *) pdata;
            for (i=0; i<wsz; i+=16) {
                caddr = IPC_TAG_IDX(taddr + i);
                if (get_value(IPC_REG_TAG_PLI_MODEL)) 
                    nm = IPC_MEM_TAG_MODEL;
                else if (get_value(IPC_REG_SC_FPGA)) 
                    nm = IPC_MEM_TAG_FPGA;
                else {
                    if (get_value(IPC_REG_SC_GATE)) {
                        nm = (caddr & 1) ? IPC_MEM_GATE_TAG1 : IPC_MEM_GATE_TAG0;
                    } else nm = (caddr & 1) ? IPC_MEM_TAG1 : IPC_MEM_TAG0;
                    caddr >>= 1;
                }
                sprintf(snm, "%s[%d]", nm, caddr);
                if (xz_type) { 
                    set_value_xz(snm, p16[0], p16[1]);
                } else set_value(snm, *p16);
                if (!fill) p16 += (xz_type) ? 2 : 1;
            } 
#endif
            return 1;
        } else {
            /* backdoor access Page table */
            pg_sz = ipc_pg_size_bit();
            pg_mask = (1 << pg_sz) - 1;
            if ((addr | sz) & pg_mask) {
                printf("ERROR: pg_bd wr p=%d addr=0x%x sz=0x%x\n",
                    pg_sz, addr, sz);
                return 0;
            }
#ifdef CVER    
            set_value(IPC_REG_PT_PLI_ID, id);
            set_value(IPC_REG_PT_PLI_ADDR, addr &
                        (~(CPS_CACHE_MASK | CPS_OP_MASK | CPS_START)));
            set_value(IPC_REG_PT_PLI_CMD, mpli_cmd);
            set_nb_value(IPC_REG_PT_PLI_DO, 1);
#else
            p16 = (unsigned short *) pdata;
            if (get_value(IPC_REG_PT_PLI_MODEL))
                nm = IPC_MEM_PT_MODEL;
            else if (get_value(IPC_REG_SC_FPGA))
                nm = IPC_MEM_PT_FPGA;
            else nm = get_value(IPC_REG_SC_GATE) ? IPC_MEM_PT_GATE : IPC_MEM_PT;

            for (i=0; i<wsz; i+=(1<<pg_sz)) {
                caddr = (addr+i) & (~(CPS_CACHE_MASK | CPS_OP_MASK | CPS_START));
                caddr >>= pg_sz;
                sprintf(snm, "%s[%d]", nm, caddr);
                if (xz_type) {
                    set_value_xz(snm, p16[0], p16[1]);
                } else set_value(snm, *p16);
                if (!fill) p16 += (xz_type) ? 2 : 1;
            }
#endif
            return 1;    
        }
    } else if ( ((addr >= MEM0_START) && (addr < MEM0_END)) ||
                ((addr >= MEM1_START) && (addr < MEM1_END)) ||
                ((addr >= MEM2_START) && (addr < MEM2_END)) ||
                ((addr >= MEM3_START) && (addr < MEM3_END))) {
        /* Backdoor access SDRAM */
        if ((sz > 4) && (addr & 3)) {
            printf("ERROR: wr sz=%d a=%0x unsupported\n", sz, addr);
            return 0;
        }
#ifdef CVER
        set_value(IPC_REG_SDRAM_PLI_ID, id);
        set_value(IPC_REG_SDRAM_PLI_ADDR, addr & IPC_SDRAM_ADDR_MASK);
        set_value(IPC_REG_SDRAM_PLI_CMD, mpli_cmd);
        set_nb_value(IPC_REG_SDRAM_PLI_DO, 1);
#else
        p32 = (unsigned int *) pdata;
        caddr = addr & IPC_SDRAM_ADDR_MASK;
        for (i=0; i<wsz; i+=4, caddr+=4) {
            bk = (caddr >> 3) & 0x3;    /* which bank */
            //{[24], [22], [21:11], [23], [10:5], [2:1] 
            idx = (caddr >> 24) & 1;
            idx = (idx << 1) | ((caddr >> 22) & 1);
            idx = (idx << 11) | ((caddr >> 11) & 0x7FF);
            idx = (idx << 1) | ((caddr >> 23) & 1);
            idx = (idx << 6) | ((caddr >> 5) & 0x3F);
            idx = (idx << 2) | ((caddr >> 1) & 0x3);
            d = p32[0] & 0xffff;
            d1 = (p32[0] >> 16) & 0xffff;
            if (xz_type) {
                xz = p32[1] & 0xffff;
                xz1 = (p32[1] >> 16) & 0xffff;
            } else xz = xz1 =  0;
            if (get_value(IPC_SD_PART) == 0) {  
                sprintf(snm, "%s%d[%d]", IPC_BK_SDRAM, bk, idx);
                set_value_xz(snm, d, xz);
                sprintf(snm, "%s%d[%d]", IPC_BK_SDRAM, bk, idx+1);
                set_value_xz(snm, d1, xz1);
            } else {
                idx = (bk << 22) | idx;
                sprintf(snm, "%s[%d]", IPC_SD_INFINEON_ARR, idx);
                set_value_xz(snm, d, xz);
                sprintf(snm, "%s[%d]", IPC_SD_INFINEON_ARR, idx+1);
                set_value_xz(snm, d1, xz1);
            }
            if (!fill) p32 += (xz_type) ? 2 : 1;
        }
#endif
        return 1;
    } else if (((addr >= BOOT_START) && (addr < BOOT_END)) ||
                ((addr >= ALT_ROM_START) && (addr < ALT_ROM_END))) {
        int swap = get_value(IPC_REG_ROM_FLIP);
        
        if ((swap && (addr >= BOOT_START) && (addr < BOOT_END)) ||
            (!swap && (addr >= ALT_ROM_START) && (addr < ALT_ROM_END))) { 
#ifdef CVER
            set_value(IPC_REG_PLI_CRAM_ID, id);
            set_value(IPC_REG_PLI_CRAM_ADDR, 
                      addr - (swap ? BOOT_START : ALT_ROM_START));
            set_value(IPC_REG_PLI_CRAM_CMD, mpli_cmd);
            set_nb_value(IPC_REG_PLI_CRAM_DO, 1);
#else
            p32 = (unsigned int *) pdata;
            if (get_value(IPC_REG_CRAM_MODEL))  nm = IPC_MEM_CRAM_MODEL;
            else if (get_value(IPC_REG_SC_FPGA)) nm = IPC_MEM_CRAM_FPGA;
            else nm = IPC_MEM_CRAM;

            for (i=0; i<wsz; i+=4) {
                caddr = addr - (swap ? BOOT_START : ALT_ROM_START) + i;
                if (get_value(IPC_REG_SC_GATE)) 
                    sprintf(snm, "%s%d.%s[%d]", IPC_MOD_CRAM, (caddr >> 16) & 7, 
                                 gate[(caddr >> 16) & 7], (caddr&0xffff) >> 2);
                else sprintf(snm, "%s%d.%s[%d]", IPC_MOD_CRAM, (caddr >> 16) & 7, nm, (caddr&0xffff) >> 2);
                if (xz_type) set_value_xz(snm, p32[0], p32[1]);
                else set_value(snm, p32[0]);
                if (!fill) p32 += (xz_type) ? 2 : 1;
            }
#endif
        } else  {
#ifdef CVER
            set_value(IPC_REG_PLI_ROM_ID, id);
            set_value(IPC_REG_PLI_ROM_ADDR, 
                      addr - (swap ? ALT_ROM_START : BOOT_START));
            set_value(IPC_REG_PLI_ROM_CMD, mpli_cmd);
            set_nb_value(IPC_REG_PLI_ROM_DO, 1);
#else 
            p32 = (unsigned int *) pdata;
            if (get_value(IPC_REG_ROM_MODEL)) nm = IPC_MEM_ROM_MODEL;
            else if (get_value(IPC_REG_SC_FPGA)) nm = IPC_MEM_ROM_FPGA;
            else if (get_value(IPC_REG_SC_GATE)) nm = IPC_MEM_GATE_ROM;
            else nm = IPC_MEM_ROM;
            
            for (i=0; i<wsz; i+=4) {
                caddr = addr - (swap ? ALT_ROM_START : BOOT_START) + i;
                sprintf(snm, "%s[%d]", nm, caddr >> 2);
                if (xz_type) set_value_xz(snm, p32[0], p32[1]);
                else set_value(snm, p32[0]);
                if (!fill) p32 += (xz_type) ? 2 : 1;
            }
#endif 
        }
        return 1;
    }
    
    printf("ERROR: Backdoor addr 0x%x not supported\n", addr);    
    return 0;
} 

int acc_bd_read(p_ipc_req preq, unsigned int addr, 
    unsigned int d, unsigned int xz) 
{
    unsigned int *p32, *ptmp;
    unsigned short *p16;
    int pg_sz, pg_mask, taddr;
    int which, xz_type;
    int ptr = 0;
    unsigned int st_addr, sz, st;

    p32 = (unsigned int *) preq->data3;
    p16 = (unsigned short *) preq->data3;
    which = preq->data0 & 0xff;
    xz_type = (preq->data0 >> 16) & 1;
    st_addr = preq->data1;
    sz = preq->data2;
    
    if ((st_addr >= CPS_START ) && (st_addr < CPS_END)) {
        if (CPS_CACHE(st_addr) != CPS_CACHE_PT) {
            taddr = convertTagAddr(st_addr, 0);
            if (taddr < 0) return -1;
            ptr = (addr - taddr) >> 4;
        } else {
            /* backdoor access Page table */
            pg_sz = ipc_pg_size_bit();
            pg_mask = (1 << pg_sz) - 1;
            if ((addr | st_addr) & pg_mask) {
                printf("ERROR: pg_acc_bd sz=%d addr=0x%x st_addr=0x%x\n",
                    pg_sz, addr, st_addr);
                return -1;
            }
            st = st_addr & (~(CPS_CACHE_MASK | CPS_OP_MASK | CPS_START));
            ptr = (addr - st) >> pg_sz;
        }

        if (xz_type) {
            p16[ptr+ptr] = d;
            p16[ptr+ptr+1] = xz;
        } else p16[ptr] = d; 

        return 0;
    } else if ( ((st_addr >= BOOT_START) && (st_addr < BOOT_END)) ||
                ((st_addr >= ALT_ROM_START) && (st_addr < ALT_ROM_END))) {
        if ((st_addr >= BOOT_START) && (st_addr < BOOT_END)) 
            ptr = addr + BOOT_START - st_addr;
        else ptr = addr +  ALT_ROM_START - st_addr;

        ptmp = p32 + ((ptr >> 2) << (xz_type ? 1 : 0));
        ptmp[0] = d;
        if (xz_type) ptmp[1] = xz;
        return 0;
    } else if ( ((st_addr >= MEM0_START) && (st_addr < MEM0_END)) ||
                ((st_addr >= MEM1_START) && (st_addr < MEM1_END)) ||
                ((st_addr >= MEM2_START) && (st_addr < MEM2_END)) ||
                ((st_addr >= MEM3_START) && (st_addr < MEM3_END))) {
        ptr = (addr - (st_addr & IPC_SDRAM_ADDR_MASK)) >> 2;
        if (xz_type) {
            p32[ptr+ptr] = d;
            p32[ptr+ptr+1] = xz;
        } else p32[ptr] = d;
        return 0;
    } 
    
    return -1; 
}  

int acc_bd_write(p_ipc_req preq, unsigned int addr, 
    unsigned int *d, unsigned int *xz) 
{
    unsigned int *p32, *ptmp;
    unsigned short *p16;
    int pg_sz, pg_mask, taddr;
    int which, xz_type;
    int ptr = 0;
    unsigned int st_addr, sz, st;

    p32 = (unsigned int *) preq->data3;
    p16 = (unsigned short *) preq->data3;
    which = preq->data0 & 0xff;
    xz_type = (preq->data0 >> 16) & 1;
    st_addr = preq->data1;
    sz = (preq->data2 == IPC_CMD_CPU_SZ_WORD) ?  4 : preq->data2;
    *xz = 0;
    
    if ((st_addr >= CPS_START ) && (st_addr < CPS_END)) {
        if (CPS_CACHE(st_addr) != CPS_CACHE_PT) {
            taddr = convertTagAddr(st_addr, 0);
            if (taddr < 0) return -1;
            ptr = (addr - taddr) >> 4;
        } else {
            /* backdoor access Page table */
            pg_sz = ipc_pg_size_bit();
            pg_mask = (1 << pg_sz) - 1;
            if ((addr | st_addr) & pg_mask) {
                printf("ERROR: pg_acc_bd sz=%d addr=0x%x st_addr=0x%x\n",
                    pg_sz, addr, st_addr);
                return -1;
            }
            st = st_addr & (~(CPS_CACHE_MASK | CPS_OP_MASK | CPS_START));
            ptr = (addr -st) >> pg_sz;
        }
        if (xz_type) {
            *d = p16[ptr+ptr];
            *xz = p16[ptr+ptr+1];
        } else *d = p16[ptr];
        return 0;
    } else if ( ((st_addr >= BOOT_START) && (st_addr < BOOT_END)) ||
                ((st_addr >= ALT_ROM_START) && (st_addr < ALT_ROM_END))) {
        if ((st_addr >= BOOT_START) && (st_addr < BOOT_END))
            ptr = addr + BOOT_START - st_addr;
        else ptr = addr +  ALT_ROM_START - st_addr;
        
        ptmp = p32 + ((ptr >> 2) << (xz_type ? 1 : 0));
        *d = ptmp[0];
        if (xz_type) *xz = ptmp[1];
    } else if ( ((st_addr >= MEM0_START) && (st_addr < MEM0_END)) ||
                ((st_addr >= MEM1_START) && (st_addr < MEM1_END)) ||
                ((st_addr >= MEM2_START) && (st_addr < MEM2_END)) ||
                ((st_addr >= MEM3_START) && (st_addr < MEM3_END))) {
        ptr = (addr - (st_addr & IPC_SDRAM_ADDR_MASK)) >> 2;
        if (xz_type) {
            *d = p32[ptr+ptr];
            *xz = p32[ptr+ptr+1];
        } else *d = p32[ptr];
        return 0;
    } 
    
    return 0; 
}

  
int ipc_bd_data_size(unsigned int addr, int sz, int xz_type) 
{
    int msz;
    int pg_sz;

    if ((addr >= CPS_START) && (addr < CPS_END)) {
        if (CPS_CACHE(addr) != CPS_CACHE_PT) {
            msz = (xz_type) ? sizeof(short)*2 : sizeof(short);
            msz *= (sz >> 4);   /* align to 16 Bytes cacheline */
            return msz;
        } else {
            pg_sz = ipc_pg_size_bit();
            msz = (xz_type) ? sizeof(short)*2 : sizeof(short);
            msz *= (sz >> pg_sz);   /* align to page size */
            return msz;    
        }    
    } else {
        if (sz < 4) msz = (xz_type) ? 8 : 4; 
        else msz = (xz_type) ? (sz + sz) : sz; 
        return msz;
    } 

    return 0;
}

int ipc_bd_done(int wh, unsigned int addr)
{
#ifdef CVER
    if ((addr >= CPS_START) && (addr < CPS_END)) {
        if (CPS_CACHE(addr) != CPS_CACHE_PT) {
            set_value(IPC_REG_TAG_PLI_DO, 0);
        } else set_value(IPC_REG_PT_PLI_DO, 0);
    } else if ( ((addr >= MEM0_START) && (addr < MEM0_END)) ||
                ((addr >= MEM1_START) && (addr < MEM1_END)) ||
                ((addr >= MEM2_START) && (addr < MEM2_END)) ||
                ((addr >= MEM3_START) && (addr < MEM3_END))) {
        set_value(IPC_REG_SDRAM_PLI_DO, 0);
    } else if (((addr >= BOOT_START) && (addr < BOOT_END)) ||
                ((addr >= ALT_ROM_START) && (addr < ALT_ROM_END))) {
        int swap = get_value(IPC_REG_ROM_FLIP);
        if ((swap && (addr >= BOOT_START) && (addr < BOOT_END)) ||
            (!swap && (addr >= ALT_ROM_START) && (addr < ALT_ROM_END))) 
            set_value(IPC_REG_PLI_CRAM_DO, 0);
        else set_value(IPC_REG_PLI_ROM_DO, 0);
    } 
#endif
    
    return 0;
} 

