/** * @brief 从一个文件中任意位置读入任意长度的字符(不能超过文件的大小) * * @param [in] head : struct dbSysHead * * @param [in] fid : long 文件标识 * @param [in] pos : long 文件起始地址 * @param [in] legnth : long 读取的长度 * @param [out] des : void * 空指针,用于接受读取的字符 * @return int * * @author tianzhenwu * @date 2015/10/20 **/ int rdFile( struct dbSysHead *head, int bufferID, long fid, long pos, long length, void *des) { int i; int idx; long staPage,endPage; long pnt; long rSta,rEnd; long pageMap; char *tmp; idx = queryFileID(head,fid); if( idx<0 ) { isAvail(NULL,"rdFile",ARRAY); } if( (pos+length) > SIZE_PER_PAGE * head->desc.fileDesc[idx].filePageNum ){ // 注意:SIZE_PER_PAGE * head->desc.fileDesc[i].filePageNum 可能溢出 isAvail(NULL,"rdFile",BOUND); } // 这里没有必要malloc一块临时的内存区,这样浪费时间。应该直接把数据写到des tmp = (char *)malloc( length+1 ); isAvail( tmp, "rdFile", MALLOC ); staPage = pos/SIZE_PER_PAGE; endPage = (pos+length-1)/SIZE_PER_PAGE; pnt = 0; for( i=staPage; i<(endPage+1); i++ ) { rSta = (pos+pnt) - ( (pos+pnt)/SIZE_PER_PAGE ) * SIZE_PER_PAGE; rEnd = ( (i+1)*SIZE_PER_PAGE>(pos+length)) ? ((pos+length) - ((pos+length)/SIZE_PER_PAGE)*SIZE_PER_PAGE) : SIZE_PER_PAGE ; pageMap = mapPage( head, fid, i); readInPage( head, bufferID, pageMap, rSta, rEnd - rSta, tmp+pnt ); pnt += (rEnd-rSta); } memcpy( des, tmp, length); free(tmp); return 0; }
static void write(RomMapperMegaFlashRomScc* rm, UInt16 address, UInt8 value) { int change = 0; int bank; address += 0x4000; if (address >= 0x9800 && address < 0xa000 && rm->sccEnable) { sccWrite(rm->scc, address & 0xff, value); } address -= 0x4000; bank = address >> 13; if (rm->flashPage[bank] >= 0) { amdFlashWrite(rm->flash, (address & 0x1fff) + 0x2000 * rm->flashPage[bank], value); } if ((address - 0x1000) & 0x1800) { return; } if (bank == 2) { int newEnable = (value & 0x3F) == 0x3F; change = rm->sccEnable != newEnable; rm->sccEnable = newEnable; } value &= rm->romMask; if (rm->romMapper[bank] != value || change) { mapPage(rm, bank, value); } }
static void loadState(RomMapperMegaFlashRomScc* rm) { SaveState* state = saveStateOpenForRead("mapperMegaFlashRomScc"); char tag[16]; int i; for (i = 0; i < 4; i++) { sprintf(tag, "romMapper%d", i); rm->romMapper[i] = saveStateGet(state, tag, 0); } rm->sccEnable = saveStateGet(state, "sccEnable", 0); saveStateClose(state); sccLoadState(rm->scc); amdFlashLoadState(rm->flash); for (i = 0; i < 4; i++) { mapPage(rm, i, rm->romMapper[i]); } }
/** * @brief 往一个文件中任意位置写入任意长度的字符,若超过文件长度,则会自动扩展文件长度 * * @param [in] head : struct dbSysHead * * @param [in] fid : long 文件标识 * @param [in] pos : long 文件起始地址 * @param [in] legnth : long 写入的长度 * @param [out] des : void * 空指针,用于传递要写入的字符 * @return int * * @author tianzhenwu * @date 2015/10/20 **/ int wtFile( struct dbSysHead *head, int bufferID, long fid, long pos, long length, void *des) { int i; int idx; int extpage; long staPage,endPage; long pnt; long rSta,rEnd; long pageMap; char *tmp; idx = queryFileID(head,fid); if( idx<0 ) { isAvail(NULL,"wtFile",ARRAY); } if( (pos+length) > SIZE_PER_PAGE * head->desc.fileDesc[idx].filePageNum ){ // 注意:SIZE_PER_PAGE * head->desc.fileDesc[i].filePageNum 可能溢出 extpage = (pos+length) / SIZE_PER_PAGE - head->desc.fileDesc[idx].filePageNum + 1; extendFileSpace( head, fid, extpage ); } tmp = (char *)malloc( length+1 ); isAvail( tmp, "wtFile", MALLOC ); memcpy( tmp, des, length ); staPage = pos/SIZE_PER_PAGE; endPage = (pos+length-1)/SIZE_PER_PAGE; pnt = 0; for( i=staPage; i<(endPage+1); i++ ) { rSta = (pos+pnt) - ( (pos+pnt)/SIZE_PER_PAGE ) * SIZE_PER_PAGE; rEnd = ( (i+1)*SIZE_PER_PAGE>(pos+length)) ? ((pos+length) - ((pos+length)/SIZE_PER_PAGE)*SIZE_PER_PAGE) : SIZE_PER_PAGE ; pageMap = mapPage( head, fid, i); writeInPage( head, bufferID, pageMap, rSta, rEnd - rSta, tmp+pnt ); pnt += (rEnd-rSta); } free(tmp); return 0; }
int romMapperMegaFlashRomSccCreate(const char* filename, UInt8* romData, int size, int slot, int sslot, int startPage, UInt32 writeProtectMask) { DeviceCallbacks callbacks = { destroy, reset, saveState, loadState }; RomMapperMegaFlashRomScc* rm; int i; rm = calloc(1, sizeof(RomMapperMegaFlashRomScc)); rm->deviceHandle = deviceManagerRegister(ROM_MEGAFLSHSCC, &callbacks, rm); slotRegister(slot, sslot, startPage, 4, read, peek, write, destroy, rm); if (size >= 0x80000) { size = 0x80000; } rm->romData = malloc(0x80000); memset(rm->romData, 0xff, 0x80000); memcpy(rm->romData, romData, size); rm->size = 0x80000; rm->slot = slot; rm->sslot = sslot; rm->romMask = rm->size / 0x2000 - 1; rm->startPage = startPage; rm->scc = sccCreate(boardGetMixer()); sccSetMode(rm->scc, SCC_REAL); rm->sccEnable = 0; rm->flash = amdFlashCreate(AMD_TYPE_2, 0x80000, 0x10000, writeProtectMask, romData, size, sramCreateFilenameWithSuffix(filename, "", ".sram"), 1); for (i = 0; i < 4; i++) { mapPage(rm, i, i); } return 1; }