import framebuf
import lcd
screen=lcd.tftdef loadrom():global programlist,spritelistwith open('1.nes', 'rb') as file:rom_bytes = file.read()checkhead=rom_bytes[0:4]programsum=rom_bytes[4]imagesum=rom_bytes[5]romsettings=rom_bytes[6:8]headothers=rom_bytes[8:16]if checkhead==b'NES\x1a':programend=16+16*1024*programsumprogrambytes=rom_bytes[16:16+programend]imagebytes=rom_bytes[programend:programend+8*1024*imagesum]programlist=[]imagelist=[]spritelist=[]for i in range(0,programsum):programlist.append(programbytes[i*16*1024:i*16*1024+16*1024])for i in range(0,imagesum):imagelist.append(imagebytes[i*8*1024:i*8*1024+8*1024])for i in range(0,512):spritelist.append(imagelist[0][i*16:i*16+16])else:print('wrong nes rom')def drawsprite(bufsp):color1=b'\xff\xff' #whitecolor2=b'\xf8\x00' #rcolor3=b'\x07\x70' #gcolor4=b'\x70\x1f' #by=0for x,data in enumerate(bufsp):newspirite=b''for i in range(0,8):for j in range(0,8):res=(bbit(data[i],(8-j-1))<<1)|bbit(data[i+8],(8-j-1))if res==0:newspirite+=color1elif res==1:newspirite+=color2elif res==2:newspirite+=color3elif res==3:newspirite+=color4fbsprite=framebuf.FrameBuffer(bytearray(newspirite),8,8,framebuf.RGB565)if (x%32)!=0:screen.blit_buffer(fbsprite,8*(x%32),y,8,8)else:y+=8x=0screen.blit_buffer(fbsprite,8*(x%32),y,8,8) def bbit(bdata,index): trsdata=0trsdata=bdata&(1<<index)if trsdata!=0:return 1else:return 0loadrom()
drawsprite(spritelist)