cachemgr.c
上传用户:super_houu
上传日期:2008-09-21
资源大小:4099k
文件大小:20k
- /****************************************************************************************
- * Copyright (c) 2002 ZORAN Corporation, All Rights Reserved
- * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF ZORAN CORPORATION
- *
- * File: $Workfile: cachemgr.c $
- *
- * Description:
- * ============
- *
- *
- * Log:
- * ====
- * $Revision: 7 $
- * Last Modified by $Author: Fwang $ at $Modtime: 04-03-20 14:45 $
- ****************************************************************************************
- * Updates:
- ****************************************************************************************
- * $Log: /I76/I76_Common/I76_Reference/Playcore/ScPad/cachemgr.c $
- *
- * 7 04-03-20 14:46 Fwang
- * Fix cache address calculation overflow problem.
- *
- * 6 12/09/03 12:17p Leonh
- * Unite the way to read data from SDRAM, use the new function
- *
- * 5 11/15/03 7:11p Leslie
- * Replace old Video API Low Level Files with new ones
- *
- * 4 10/20/03 11:43a Leslie
- * update the change from I64
- *
- * 7 10/17/03 6:35p Leonh
- * clean up code and avoid psn error
- *
- * 6 9/25/03 3:56p Leonh
- * add signature
- *
- * 5 9/25/03 3:47p Leonh
- * Fix the psn error
- *
- * 4 9/23/03 1:55p Leonh
- * fix the error when read disc
- *
- * 3 9/22/03 3:55p Leonh
- * Avoid offset caculated error when get bytes
- *
- * 2 8/21/03 3:26p Leonh
- * add compile switch
- *
- * 1 03-07-09 10:22 Leonh
- * cache enable
- *
- * 8 23/04/02 9:36 Nirm
- * - Added dependency in "Config.h".
- *
- * 7 2/17/02 15:59 Rinata
- * remove code and files under not define I49_STEPA0
- *
- * 6 14/02/02 9:05 Atai
- * Correct path
- *
- * 5 30/01/02 20:26 Nirm
- * Replaced min() with MIN().
- *
- * 4 16/01/02 15:55 Atai
- * Change debug printing
- *
- * 3 9/01/02 21:21 Nirm
- * Corrected Include-paths.
- *
- * 2 9/01/02 16:53 Nirm
- * Corrected Include-Paths.
- ****************************************************************************************/
- #include "Config.h" // Global Configuration - do not remove!
- #ifdef _DEBUG
- #undef IFTRACE
- extern int gTraceCache;
- #define IFTRACE if (gTraceCache)
- #endif
- /* Standard includes */
- #include <stdio.h>
- /* Specific includes */
- #include "Includemath-macro.h"
- #include "PlaycoreCoremaincoregdef.h"
- #include "PlaycoreAuxCacheAuxCache.h"
- #include "PlaycoreNav_DVDHeadernvdvgdef.h"
- #include "PlaycoreScPadscmgr.h"
- #include "PlaycoreScPadscllmgr.h"
- #include "PlaycoreScPadcachemgr.h"
- #include "Includesysdefs.h"
- #include "Decoderlow_levelDEC_LL_Api.h"
- #include "Decoderlow_levelDEC_LL_Reg.h"
- #ifdef DISC_CACHE_ENABLE
- BYTE slot_list_head, slot_list_tail;
- BYTE free_slot_count;
- DWORD blk_in_first_slot;
- #define cache_get_scHandle( slot_id ) (SC_DISC_CACHE_ADDR + (DWORD)(512UL*(slot_id-1)) )
- #define slotTbl_get_scHandle( slot_id ) (SC_DISC_CACHE_TABLE_ADDR + (DWORD)(2*(slot_id-1)) )
- BYTE slot_GetNew(void);
- /*---------------------------------------------------------------------------
- // Function: BOOL dc_cache_CacheData( DWORD lbn, DWORD offset, WORD bytes , WORD sc_handle )
- //
- // Description : If the data requested exists in cache, then copy to sc_handle. Otherwise
- read from disc.
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- BOOL dc_cache_CacheData( DWORD lbn, DWORD offset, WORD bytes , WORD sc_handle )
- {
- BOOL b_res;
- dbg_printf(("<G(%ld:%ld,%d, sc_handle=0x%08lx)", lbn, offset, bytes,sc_handle));
- GLB_NAV_DVD( b_first_seek_after_get_data ) = TRUE;
- b_res = sc_CopyFromCache( lbn + GLB_NAV_DVD(dw_ifobase) , offset, bytes, sc_handle );
- dbg_printf(("g>n"));
- return b_res;
- }
- /*---------------------------------------------------------------------------
- // Function: BOOL dc_cache_GetData( DWORD lbn, DWORD offset, WORD bytes, BYTE *buffer )
- //
- // Description : If the data requested exists in cache, then copy to buffer. Otherwise
- read from disc.
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- BOOL dc_cache_GetData( DWORD lbn, DWORD offset, WORD bytes, BYTE *buffer )
- {
- BOOL b_res;
- dbg_printf(("<G(%ld:%ld,%d, ptr=0x%08lx)", lbn, offset, bytes,buffer));
- GLB_NAV_DVD( b_first_seek_after_get_data ) = TRUE;
- b_res = cache_GetBytes( lbn + GLB_NAV_DVD(dw_ifobase), offset, bytes, buffer );
- dbg_printf(("g>n"));
- return b_res;
- }
- /*---------------------------------------------------------------------------
- // Function: BOOL dc_cache_PreCacheData( DWORD lbn, DWORD offset, WORD bytes )
- //
- // Description : If the data requested exists in cache, don't do anything. Otherwise
- read from disc and save in cache.
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- BOOL dc_cache_PreCacheData( DWORD l_lbn, DWORD l_Offset, WORD i_NbrBytes )
- {
- DWORD dw_norm_lbn;
- WORD i_norm_offset;
- WORD i_remain_count;
- BOOL b_res = TRUE;
- BYTE cache_slot_id;
- dbg_printf(("<G(%ld:%ld,%d, ptr=0x%08lx)", l_lbn, l_Offset, i_NbrBytes, 0));
- GLB_NAV_DVD( b_first_seek_after_get_data ) = TRUE;
- l_lbn += GLB_NAV_DVD(dw_ifobase);
- dw_norm_lbn = l_lbn + ((l_Offset>>11) & 0x1fffffUL);
- i_norm_offset = (WORD)(l_Offset & 0x7FFL);
- i_remain_count = i_NbrBytes;
- while(i_remain_count && b_res){
- if (blk_in_first_slot == dw_norm_lbn)
- cache_slot_id = slot_list_head;
- else
- cache_slot_id = dc_cache_hit( dw_norm_lbn);
-
- if ( cache_slot_id ){ /* data is already cached */
- WORD i_request_count;
- i_request_count = (WORD) MIN( i_remain_count, (2048-i_norm_offset) );
- i_remain_count -= i_request_count;
- if (i_remain_count){ /* means we still have data to get from subsequent sectors */
- dw_norm_lbn++; /* set lbn and offset to beginning of next sector */
- i_norm_offset = 0;
- }
- }
- else{
- b_res = dc_cache_sector( dw_norm_lbn );
- }
- }
- dbg_printf(("g>n"));
- return b_res;
-
- }
- /*---------------------------------------------------------------------------
- // Function: void dc_cache_init()
- //
- // Description : Initialize disc cache
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- void dc_cache_init(void)
- {
- BYTE slot_id;
- TAG_LIST_ITEM list_item = {0,0,0};
-
- for (slot_id = 1; slot_id <= CACHE_NBR_SLOT; slot_id++ )
- sc_SetBytes32((DWORD)slotTbl_get_scHandle(slot_id), 0, sizeof(TAG_LIST_ITEM), &list_item);
- slot_list_head = 0; slot_list_tail = 0;
- free_slot_count = (BYTE)CACHE_NBR_SLOT;
- blk_in_first_slot = 0;
- }
- #define COPYFROMDISC_CONTAINER_CNT 4
- #define COPYFROMDISC_XFER_SZ (COPYFROMDISC_CONTAINER_CNT*BYTE_PER_CONTAINER)
- /*---------------------------------------------------------------------------
- // Function: BOOL sc_CopyFromCache( DWORD dw_lbn, DWORD dw_offset, WORD n_byte_count, WORD sc_handle )
- //
- // Description : Copy data to SCPAD.
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- BOOL sc_CopyFromCache( DWORD dw_lbn, DWORD dw_offset, WORD n_byte_count, WORD sc_handle )
- {
- BYTE buff[COPYFROMDISC_XFER_SZ];
- WORD fraction_count;
- while( n_byte_count ){
- fraction_count = MIN( n_byte_count, COPYFROMDISC_XFER_SZ);
-
- if (cache_GetBytes( dw_lbn, dw_offset, fraction_count, buff ) == FALSE)
- return FALSE;
- sc_Write( sc_handle, CONTAINER_COUNT(fraction_count), (Sc_cont *) buff );
- dw_offset += (DWORD) fraction_count;
- sc_handle += COPYFROMDISC_CONTAINER_CNT;
- n_byte_count -= fraction_count;
- }
- return TRUE;
- }
- /*---------------------------------------------------------------------------
- // Function: BOOL cache_GetBytes(DWORD l_lbn, DWORD l_Offset, WORD i_NbrBytes, BYTE *buff)
- //
- // Description : If the data requested exists in cache, then copy to buffer. Otherwise
- read from disc.
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- BOOL cache_GetBytes(DWORD l_lbn, DWORD l_Offset, WORD i_NbrBytes, BYTE *buff)
- {
- DWORD dw_norm_lbn;
- WORD i_norm_offset;
- WORD i_remain_count;
- BOOL b_res = TRUE;
- BYTE cache_slot_id;
- dw_norm_lbn = l_lbn + ((l_Offset>>11) & 0x1fffffUL) ;//LeonHe_0922_2003_a: Avoid offset caculated error when get bytes
- i_norm_offset = (WORD)(l_Offset & 0x7ffUL);
- i_remain_count = i_NbrBytes;
- while(i_remain_count && b_res){
- if (blk_in_first_slot == dw_norm_lbn)
- cache_slot_id = slot_list_head;
- else
- cache_slot_id = dc_cache_hit( dw_norm_lbn);
-
- if ( cache_slot_id ){ /* data is already cached */
- WORD i_request_count;
- i_request_count = (WORD) MIN( i_remain_count, (2048-i_norm_offset) );
- //sc_GetBytes( cache_get_scHandle(cache_slot_id), i_norm_offset, i_request_count, buff);
- sc_cache_getBytes(( DWORD)cache_get_scHandle(cache_slot_id), i_norm_offset, i_request_count, buff);
- i_remain_count -= i_request_count;
- if (i_remain_count){ /* means we still have data to get from subsequent sectors */
- dw_norm_lbn++; /* set lbn and offset to beginning of next sector */
- i_norm_offset = 0;
- buff += i_request_count; /* increment buffer ptr accordingly */
- }
- }
- else{
- b_res = dc_cache_sector( dw_norm_lbn );
- }
- }
- return b_res;
- }
- /*---------------------------------------------------------------------------
- // Function: BOOL dc_cache_sector( DWORD lbn)
- //
- // Description : Read data from disc, and save in cache
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- BOOL dc_cache_sector( DWORD lbn)
- {
- BYTE slot_id = slot_GetNew();
- dbg_printf(("nCache sector %li to block %li",lbn,slot_id));
-
- if (!sc_CopyFromDisc32( lbn, 0, 2048, cache_get_scHandle(slot_id)) )
- return FALSE;
- slot_set_tag( slot_id, lbn );
- return TRUE;
- }
- /*---------------------------------------------------------------------------
- // Function: BOOL dc_cache_hit( DWORD lbn)
- //
- // Description : Check if the request block is already in cache
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- BYTE dc_cache_hit( DWORD lbn)
- {
- BYTE slot_id;
- TAG_LIST_ITEM list_item;
- //Search block list table to check if the target lbn is already cached
- for (slot_id=slot_list_head ; ;slot_id = list_item.next)
- {
- if (slot_id == 0) // Vacant block
- break;
-
- sc_cache_getBytes( ( DWORD)slotTbl_get_scHandle(slot_id), 0, sizeof(TAG_LIST_ITEM), &list_item);
- if ( lbn == slot_get_tag(slot_id) )
- {
- slot_list_move2head( slot_id );
- return slot_id;
- }
- if (slot_id == slot_list_tail) //Not cached
- break;
- }
- return 0;
- }
- /************************ Code for slot list of disc cache ******************************/
- /*---------------------------------------------------------------------------
- // Function: void slot_list_insert2head( BYTE slot_id )
- // Description : Insert specified slot to the head of slot list
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- void slot_list_insert2head( BYTE slot_id )
- {
- TAG_LIST_ITEM list_item, temp_item;
- sc_cache_getBytes( ( DWORD)slotTbl_get_scHandle(slot_id), 0, sizeof(list_item), &list_item);
- //list_item.next = head
- list_item.next = slot_list_head;
- list_item.prev = 0;
- sc_SetBytes32( ( DWORD)slotTbl_get_scHandle(slot_id), 0, sizeof(list_item), &list_item);
- // head.prev = list_item
- if (slot_list_head != 0)
- {
- sc_cache_getBytes( ( DWORD)slotTbl_get_scHandle(slot_list_head), 0, sizeof(temp_item), &temp_item);
- temp_item.prev = slot_id;
- sc_SetBytes32( ( DWORD)slotTbl_get_scHandle(slot_list_head), 0, sizeof(temp_item), &temp_item);
- }
- slot_list_head = slot_id; blk_in_first_slot = list_item.tag;
- if (slot_list_tail == 0) // If list is empty
- slot_list_tail = slot_id;
- return;
- }
- /*---------------------------------------------------------------------------
- // Function: void slot_list_move2head( BYTE slot_id)
- // Description : Move specified slot to the head of slot list
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- void slot_list_move2head( BYTE slot_id)
- {
- TAG_LIST_ITEM list_item, temp_item;
- sc_cache_getBytes( ( DWORD)slotTbl_get_scHandle(slot_id), 0, sizeof(list_item), &list_item);
- //Remove from current position in list
- if (slot_id == slot_list_head)
- return;
- else if (list_item.prev != 0)
- {
- //list_item.prev.next = list_item.next
- sc_cache_getBytes( ( DWORD)slotTbl_get_scHandle(list_item.prev), 0, sizeof(temp_item), &temp_item);
- temp_item.next = list_item.next;
- sc_SetBytes32( ( DWORD)slotTbl_get_scHandle(list_item.prev), 0, sizeof(temp_item), &temp_item);
- }
- if (slot_id == slot_list_tail) //list_item.next.prev = list_item.prev
- slot_list_tail = list_item.prev;
- else if (list_item.next != 0)
- {
- //list_item.next.prev = list_item.prev
- sc_cache_getBytes( ( DWORD)slotTbl_get_scHandle(list_item.next), 0, sizeof(temp_item), &temp_item);
- temp_item.prev = list_item.prev;
- sc_SetBytes32(( DWORD) slotTbl_get_scHandle(list_item.next), 0, sizeof(temp_item), &temp_item);
- }
- //Insert to list head
- slot_list_insert2head( slot_id );
- return;
- }
- /*---------------------------------------------------------------------------
- // Function: void slot_list_test()
- // Description : Display the status of slot list
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- void slot_list_test()
- {
- TAG_LIST_ITEM list_item;
- int i;
- tr_printf(("nhead: %li",slot_list_head));
- tr_printf((" tail: %li",slot_list_tail));
- tr_printf(("nBlock table:n"));
- for (i=1; i<=CACHE_NBR_SLOT; i++)
- {
- sc_cache_getBytes( ( DWORD)slotTbl_get_scHandle(i), 0, sizeof(list_item), &list_item);
- tr_printf(("blk: %li",i));
- tr_printf((" tag: %li"));
- tr_printf((" prev: %li"));
- tr_printf((" next: %lin"));
- }
- return;
- }
- /*---------------------------------------------------------------------------
- // Function: BYTE slot_GetNew()
- // Description : Get a available slot from cache.
- If there is a empty slot, insert it to the head of slot list.
- Othwise, get the slot calculated by LRU algorithm
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- BYTE slot_GetNew(void)
- {
- BYTE slot_id;
- if (free_slot_count)
- {
- slot_id = (BYTE)CACHE_NBR_SLOT - free_slot_count + 1;
- free_slot_count--;
- slot_list_insert2head( slot_id );
- return slot_id;
- }
- slot_id = slot_list_tail;
- slot_list_move2head( slot_list_tail );
- return slot_id;
- }
- /*---------------------------------------------------------------------------
- // Function: DWORD slot_get_tag( BYTE slot_id)
- // Description : Get the tag(lbn) of specified slot
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- DWORD slot_get_tag( BYTE slot_id)
- {
- TAG_LIST_ITEM list_item;
- sc_cache_getBytes( ( DWORD)slotTbl_get_scHandle(slot_id), 0, sizeof(list_item), &list_item);
- return list_item.tag;
- }
- /*---------------------------------------------------------------------------
- // Function: void slot_set_tag( BYTE slot_id, DWORD lbn)
- // Description : Set the tag(lbn) of specified slot
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- void slot_set_tag( BYTE slot_id, DWORD lbn)
- {
- TAG_LIST_ITEM list_item;
- sc_cache_getBytes( ( DWORD)slotTbl_get_scHandle(slot_id), 0, sizeof(list_item), &list_item);
- list_item.tag = lbn;
- sc_SetBytes32( ( DWORD)slotTbl_get_scHandle(slot_id), 0, sizeof(list_item), &list_item);
- if (slot_id == slot_list_head)
- blk_in_first_slot = lbn;
-
- return;
- }
- #if 0
- /************************ Code for read data from SCPAD cache ***************************/
-
- //#define SC_BASE_ADDR 0x1E4C00 // 64M SDRAM 218K SCPAD
- /*---------------------------------------------------------------------------
- // Function: void sc_read_cache_data( DWORD dw_abs_word_offset, WORD *p_buff, WORD i_word_count )
- //
- // Description : Read word from SCPAD
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- void sc_read_cache_data( DWORD dw_abs_word_offset, WORD *p_buff, WORD i_word_count )
- {
- //dw_abs_word_offset += SC_BASE_ADDR;
- if (!((DWORD) p_buff&0x01)){ /* case where destination ptr is EVEN */
- /* we can do WORD copy without any problem*/
- I49_ReadDRAMDataNSW( (UINT32)dw_abs_word_offset, (UINT16*)p_buff, i_word_count );
- }
- else{ /* case where destination ptr is ODD */
- /* must do a byte copy */
- BYTE *p_byte = (BYTE *)p_buff;
- WORD i_tmp;
- BYTE *p_tmp_byte;
- int i;
- p_tmp_byte = (BYTE *) &i_tmp;
- #ifdef MOTOROLA
- for(i=0;i<i_word_count;i++){
- I49_ReadDRAMDataNSW(dw_abs_word_offset+i, &i_tmp, 1 );
- p_byte[i<<1] = p_tmp_byte[0];
- p_byte[(i<<1)+1] = p_tmp_byte[1];
- }
- #else
- for(i=0;i<i_word_count;i++){
- I49_ReadDRAMDataNSW(dw_abs_word_offset+i, &i_tmp, 1 );
- p_byte[i<<1] = p_tmp_byte[1];
- p_byte[(i<<1)+1] = p_tmp_byte[0];
- }
- #endif
- }
- }
- #endif//if 0
- /*---------------------------------------------------------------------------
- // Function: int sc_cache_getBytes( DWORD sc_handle, WORD i_byte_offset, WORD i_byte_count, BYTE *p_buff )
- //
- // Description : Read data from SCPAD. The usage is same with sc_GetBytes(), but is faster.
- // Author : Wang Fan
- // Date : 30/10/01
- //-------------------------------------------------------------------------*/
- void sc_cache_getBytes( DWORD sc_handle, WORD i_byte_offset, WORD i_byte_count, BYTE *p_buff )
- {
- WORD i_VarTmp = 0;
- DWORD dw_absolute_offset;
- if (!i_byte_count)
- return;
- dw_absolute_offset = (((DWORD)sc_handle)<<2)+(DWORD) i_byte_offset;
- DEC_GetBytesInSDRAM( dw_absolute_offset,0, i_byte_count, p_buff,0x00);
- #if 0
- wai_sem(SEM_DRAM_ID);
- /* Odd Offset to retrieve */
- if(dw_absolute_offset & 0x01) {
- if (i_byte_count > 1) {
- /* Read the even part of the buffer */
- sc_read_cache_data((DWORD)((dw_absolute_offset+2)>>1), (WORD*)p_buff, (WORD)(i_byte_count>>1));
-
- /* Shift the Whole buffer to free the first byte */
- for (i_VarTmp=i_byte_count-1; i_VarTmp>0; i_VarTmp--)
- {
- p_buff[i_VarTmp]=p_buff[i_VarTmp-1];
- }
- }
- sc_read_cache_data((dw_absolute_offset>>1), &i_VarTmp, 1);
- /* Shift the WORD to get the Higher Byte */
- #ifndef MOTOROLA
- i_VarTmp = (i_VarTmp>>8)&0xFF;
- #endif
- /* Write the First Byte into the Buffer */
- p_buff[0]=(BYTE)i_VarTmp;
- }
- else if(!(dw_absolute_offset&0x01)&&(i_byte_count&0x01)) {
- /* Read the even part of the buffer */
- sc_read_cache_data((DWORD)(dw_absolute_offset>>1), (WORD*)p_buff, (WORD)(i_byte_count>>1));
- /* Read the last byte */
- sc_read_cache_data(((dw_absolute_offset+i_byte_count)>>1) , &i_VarTmp, 1);
- #ifdef MOTOROLA
- i_VarTmp = (i_VarTmp>>8)&0xFF;
- #endif
- i_VarTmp = i_VarTmp&0x00FF;
- /* Write the last Byte */
- p_buff[i_byte_count-1]=(BYTE)i_VarTmp;
- } else /* Odd Offset and Even Nbr of Bytes to retrieve */
- if(!(dw_absolute_offset&0x01)) {
- sc_read_cache_data((DWORD)(dw_absolute_offset>>1), (WORD*)p_buff, (WORD)(i_byte_count>>1));
- }
- sig_sem(SEM_DRAM_ID);
- #endif//if 0
- }
- #endif//DISC_CACHE_ENABLE