-----------------------------------------------
- V810 CPU Tracer v0.5
- Copyrite 1998 the VB-98 team
-----------------------------------------------

-------------------------------------------------------------------------------------
- CopyWrite Info/Disclamer
-------------------------------------------------------------------------------------
the V810_CPU Tracer and VB emu is copywrite David Tucker 1998, all rights reserved.
   You may freely use this code as long as you make no mony from the use of this code and you aknowlege the origional author (Me).  I reserve the right to dictate who can use this code and how (Just so you dont do sompthing stupid with it).
   Most Importantly, this code is swap ware.  If you use It send along your new program (W/code) or som other Interesting tidbits you wrote, that I might be Interested in.
   This code is In beta, there are bugs!  I am not responsible for any damage done to your Computer, reputation, ego, dog or Famly Life due to the use of this code.  All source is provided as is, I make no garenties, and am not responsible for anything you do with the code (Legal or outherwise).
   Virtual Boy is a trademark of Nintendo, and V810 is a TradeMark of NEC.  I am in no way affiliated with Ither party and all Information contained hear was found frelly through Public Domain sources.

-----------------------------------------------------------------------------------
- History
-----------------------------------------------------------------------------------
    5/20/98 - CPU core started

    5/21/98 - First instructions executed! Fixed the dissasembler, now most opcodes are comming out right (only opcodes I dont have documentation on are failing, like Floating point opcodes, and string manipulation opcodes). added suport for the first 3 opcodes...

    6/03/98 - fixed the flags (Several times, still bugs, I think).  Added fairly decent memory support, added most important Instructions (Missing Mul/Div/SetF).  Now writes out Ram to the drive, so you can Inspect the ram at your leasure.  Finished (for the most part) the interface.

    6/13/98 - Added suport for Mul and div (MUL, DIV, MULU, DIVU)instructions,  Updated the memory accesing routines (All memory is anded with 07FF FFFF, the VB only looks at the lower 26 bits of a memory adddress.).  Cleand up memory acesses, merged mem1 and mem4 into one linear address.  Added the VB_DISP.EXE util, to display the BG mapes (Curently only looks good with VTetris).  Added suport for american games as well as jap games (memory access to the rom needed to be mirrored.).  Once again I improved on the flags.  Still I dont trust them.

    6/15/98 - Finaly added Muli, In(B,H,L), Out(B,H,L), EI, and DI. Leaving only SETF and odd instructions to do.  On first analisis In and out are maped to memory (ld and st)as near as I can tell in.b 5000 == ld.b 5000.  For now Ive added the ability to seperate the two, but Im not right now.  Interesting note: Vertiacal Force has started to generate a proper display screen (Wohooo).  Flags are still a problem, it apears that Virtual Golf (Jap) and Virtual Pinball (Jap) both get into bad loops due to improper flag testing.  This needs more research thoe.

    6/16/98 - Fixed ASR instruction (I was always copying a one to the MSB in stead of checking for the MSB and copying it), fixed the BGT instruction I was improperly bailing out (Missing brackets).  Theas fixes solve the problems in VGolf and VPinball. But nither one do anything anyway... (Bummber).  Fixed up a lot of flag tests, I was missing brackets to set proper presidnet.  Now 8 games are generating the beginings of a display list (VBBall_J, VBBall_E, VTETRIS, WarioLand, VForce, PBomber_J, PBomber_E, & GPinball).  Water World seems to be generating a display list as well, but there is some garbage in it, this garbage is in the rom as well, it remains to be seen if it is a bug in my CPU or If I need to re dump the rom.

    6/19/98 - Began to build up a true graphics lib for the VB display,  Im hoping to have the ability to view the CHR tbl, BGMAP tbl, OBJ tbl, and World tbl (In perspective).  For now you will only be able to hop from the debuger to the video display, the display will not update itself in realtime (bummber) maby when we get booting a little better.

    6/23/98 - Finished a simple viewer for the CHR ram OBJ ram and BG_MAP ram.  type (V)ideo in the tracer to enter Video mode then select your resolution, chose carfully if you enter an unsuported video mode, things could get ugly (I dont check!)  Once in you will be prompted to display (C)hars, (B)gmap or (O)bj map, on the bg map hit any key to cycle through all 14 planes on all others hit any key to exit, to leave graphics mode hit <ESC>  This is quite plimnary and dont expect to mutch, I need to add suport for scrolling when the graphics dont fit on the screen, for now try 640x480 mode, this will display all but the last 4 lines of a BG_map...  I temporaroly made the last ram1.bin get reloaded into memory so you can unzip vtetris.zip and view the immages in the ram without the need to trace through the actual code (For debug purposes its a real pain getting VTetris to generate an OBJ list, I can only do it one out of 10 times...).  Finaly added the (B)reak command just pass it a memory address and it will break on the address (If it ever makes it there =)  hit <ESC> to break out if your caught in a infinent loop...  Added <ESC> suport to (T)race as well, now hiting esc breaks you out of a trace that has gon on to long (IE. t 10000000, will take about 20 minuts to execute =), added another command (M) dump Memory, dumps the memory to the HD, memory is nolonger writen out everytime the program executes... this makes it easier to take snapshots of memory without acidently overwriting the snapshots (Big Bummber)

    6/29/98 - Profiled the program today, i made some obvious speed ups (Mostly in the Display code)  Some known weak spots: 
In v810_dsp.cpp, 
  CHR_2_SPRT() this func is quite slow mainly due to the PutPixle Routine, its posible there is a blitPixle... need to investigate... (I speed it up, osme, already need to reprofiel to be shure but it seems to have helped)
  GetChr() Also slow mainly due to v810_rbyte() I changed this to rhword, no swaping and eliminated multiplications in the loop (Actualy I unroled the loop...) seems to be better, still need to reprofile... as before
  BlitScreen() used in several spots, its posible that writing direct to video ram would be evin faster than screen blits, but maby not, I need to test.
  I can shave off 12 test from v810_trc() by ading a jump table to the instruction mode in stead of a case statment... but the overhead of calling another func might eliminate the advantage, its something to try (Note that most opcodes will bail out after 6 tests at max, and probably average 2-3 test... so its a tough desision...)  Its posible I can eliminate the need all together, but It would meen offloading a lot more code to the indavidual instructions.
  Memory access and Instruction execution is quite streamlined (I could use som asm in the instruction code to make indaviduale instructions a lot more efficient) overal I think the processor is up to par, it should be fast enough to emulate a real v810 (I can execute 2,097,152 instructions in 6 sec (thats t 200000 in case your wondering why the wierd number, remember t takes hex # as a param) thats 349,525 instructions per sec, fairly decent and I only have a pentium 90.  I think we would be lucky to double this with the best of optimizations (Unless we went to an all asm core, but then you gota debug...)
  Video is acceptible I can display 14 screens in about 2 sec, realy this needs to be more like 60 screens in 1 sec but Im thinking most of the hold up is in the blit func, but more profiling is needed, for now its updating almost as fast as you can see, thats good enough for still screens.....  But it will need to be overhauld.
  Im droning on, but Im trying to keep a log for my own reference later, I thought the readme was as good of a place as any!  
  I spent some time tracing down the phantom FPU instructions, it apears that they are realy FPU instructions (not some other inst, besides they are definently 4 byte instructions) but I dont have good docs on the FPU It needs a lot of work! maby its covered in the v810 manual, we will know when my manual arives...

    6/30/98 - Did another profile and researched out the PutPixle routine, I replaced it with direct memory access to the sprite, and shaved off about 45 seconds on a 60 second run (70% faster) basicaly I blast 30*14 BG maps to the screen as fast as I can, (thats 420 screens) anyway it now takes 15 seconds to do that verses 65-ish.  There is still a lot of optimizing to be done, but the drawing routines will be undergoing some major transformations soon, and I dont want to waist my time optimizing if Im only going to destroy them in the end.  I began working on a World Viewer (Viewing each indavidual world for now, some day we can see them all in perspective.) It seems straight forward for the BG map, but there is a lot of ambiguity about the other 3 modes (IE, the OBJ...) for now Ill wait for Ben to enlighten me more on how the worlds work (Char efects, zooming rotation. Transparency. Intensity. and how all is cast into 3d space...)
  Added a (P)rint screen command in the (V)ideo menues, pressing P while viewing a screen dumps a .bmp file (named after the screen, bg_map1.bmp etc...) also 'Q' or <ESC> are the only ways out of the Video screens.  And in the BG_Map screen any key takes you to the next BG_Map and <ESC> gets you out fast.

    7/1/98 - Added a new function to the (V)ideo Menu: (R)om  alows you to graphicaly view the rom immage (Like VB_View.exe) it only has 3 controlls right now ((U)p, (D)own, screen (P)rint, and of corse <ESC> and (Q)uit).
   I did some more profilling on the display.  I modified al the functions to access the ram array directly (Without going through v810_rhword(addr) this made things nice and fast.  The only 2 week spots now are the screen blit and the chr2sprite() fn.  I think i can take care of the screen blit, but chr2sprite is about as fast as It can be (Without making the code ugly!)  I can now pump out 1500 displays in about 1 minut (about 25 frames per second) this is quite aceptible for now...

    7/2/98 - added +/- commands to the (R)om command, now you can fine tune the displaying of the rom...  Boy can you tell Im board!  Still being board I updated the rom func with the rest of the variables (U/D) Up/Down a page, (u/d)Up/down a line, (</>) left right 1 char(You dont need to hold shift), (+/-) up/down 1 byte, (1/2) shrink/grow the columbs.  Thease are all the usfull commands from VB_View.exe I consider this to be a full replacement for VB_View, it is now oficialy dead! oh(H)elp has also been added to the (R)om func...

    7/8/98 - things are slow, but Im making progress.  Tryed to implement the memory mirroring, took a guess and asumed there masking out only the significant bits, it seems to work, no guarenties thoe...  figured out most of the lower memory, fixed the CHR table (Properly Mirrored). Figured out most of the Interupts, need to Implement them.

    7/9/98 - Added in Interupt Suport (i) # (0-16)  only the first 5 interupts are actualy used on the VB and then its patchy whether they suport it.  For now Im not checking if a given Interupt is valid, When I can figure it out the VB has the ability to turn on and off each Interupt so I will tie into that and not alow you to select a bogus Interupt.  NonMascible Interupts and Exceptions (and Traps) have no handler code in the rom's so for now I will not add suport for them.  Adjusted the return values on some of the registers, I was trying to get VTetris to boot, that faild.  But I got Mario Tennis going Instead (Has a major Bug thoe, the chr ram is not mapping right, Im guessing its the mirroring...)

    7/22/98 - made some changes I cant remember...  Mostly Banging my head against the wall (Arg!!!)
10/18/98 - Im still alive...  Updated the memory map, and fixed the Interupts (Partly), I need to update the IntPending line in the VIP registers...  Fixed up OBJ memory, but need to Implement Worlds and the realtime screen updates...

    11/2/98 - Fixed up the World display (Most of the way) and got display Interupts going, now I need controller and timer Interupts working right... Implemented the controller (Minus Interupts), VFishing, Vtetris and MarioTennis are all booting (Sort of), need to Implement the Missing FPU and BITSTRING Opcodes..., NEW properly clear the KBD buffer!  and I display the BG maps from back to front, like there generated (Pictures are stored back to front...), also fixed the size of World Objects...

    11/8/98 - Fixed the OBJ up a bit (Not perfect), added some speed ups, and fixed a KBD Error. VTetris is playable, and VFishing, SpaceSquash, and InMouseHouse are almost playable! added a display Skip option, helps speed things up...

    11/11/98 - added true palets, might need to work out some bugs hear...  I need to implement Mode2BGMaps so Mario Tennis looks better (Zooming)...

    11/12/98 - Fixed OBJ Selection (I think), and began to Understand the Alternant BGMap modes...  Need more work...

    11/17/98 - added in the FPU opcodes, I think they work (Except for the flags??).  Found a cool C trick, to cast an int to a Float without converting (Keeping the same binary bit patern, just do this: *((float *) &MyInt)  and back *((int *) &MyFloat)  way to handy!!! (The V810 uses the same 32 bit registers for Floats and Ints, and I need to trick the Compiler into seeing my 'Word' register as a float, without trying to convert it...).  Now Space Squash is almost playable...

    11/20/98 - added in a fiew limits on the Image Structires, and added in a screen wrap, now Insmouse House is going better (In the demo mode that is) and The BGMaps in space squash rotate around properly.  I can get Vir Fishing to display the fish properly, but not without causing extra garbage to show up on the screen (Why?).  Space squash is still not playable. the ball and the oponent are constantly blinking (Why?).  Im still over running memory in Insmouse House, I think actualy im 'Under Running' it, ie im writing to a negative value on an array!

    11/21/98 - Did some more fidling, now GPinbal, MarioClash and Telero boxer boot, and WarioLand starts to.  I had GPinball going perfectly, but I screwed it up, and I cant get it back (I need to make more backups of my source!!!)  A lot of games are crashing, becaus the Interupts are handled wrong.  I need to figure out the Interupt mask bits a little bit better!

    11/23/98 - Moved and Renamed a bit of the source, to try to draw the line between the V810 CPU core, and the Virtual Boy Spesific stuff.  Im trying to keep the CPU core reusable.
    Summary of games: GPinball, InsMouseHouse, SpaceSquash, VFishing, MClash, and TeleroBoxer are almost playable.  VTetris is 100% playable (Wee).  VBBall, WarioLand, WaterWorld, ane MarioTennis are almost booting (Beginging screens, then they get stuck wating for some display update (Probably easy to fix)). PBomber, VForce, 3DTetris, RedAlarm,VGolf, Jbros, and Nesters Bowling all crash.  I think they cant handle an Interupt Im calling.  I need to track this down (Especialy on PBomber and VForce, they never used to crash before I put in Interupts)...

    11/24/98 - cleand up the Interupts a bit (Still confused on them).  This did ont make any new games start, but at least there not going off in the weeds.  Need to Fix the Pallet, and there is always more work to be done on the display.  I added a RELEASE flag, so you can run it without dumping to the debuger (Some day Ill make this prety.)  First Beta goes out tomorow!
-----------------------------------------------------------------------------
- too do:
-----------------------------------------------------------------------------
* Need to Improve emulation of the Interupts... Need to set the Flags right (Done, sort of)
* Need to Properly fix the flags (I have no faith in there acuricy!) (Maby)
* Need to pin down what Is memory and what is a register, and handle both... (Getting there)
  I only know of 673k of ram, there should be 1.5meg of ram (If we can see it all...)
* Need to emulate FPU Opcodes? (addf.s mulf.s), and BSTR opcode? (Fixt FPU, sort of)
* Need to fix the video mode up.
   1) its dog slow!, I need to make a custom Dispaly routine (Fixed for now) 
   2) Need to be less dependant on the screen resolution! (Getting Better)
   3) Need to handle the Extended BGMap Modes (WavyGravy, and Zoom/Rotate)
* In my profiling the CPU core takes up 2/3 of the time with graphics trailing for the rest.  I need to make a ASM CPU core, some day (If Ben does not make one first, right Ben)...

-----------------------------------------------------------------------------
- Usage:
-----------------------------------------------------------------------------
Note: vb_98.exe is a subset of v810_trc.exe, that jumps strait into the 'D'isplay mode.  This is equivlent to 't 20000', 'd' in the tracer.

 v810_trc <rom_name>  

u - unasemble the next 16 instructions
d - dump the next 16 bytes (should be words(32bits))
t - trace through next instruction
b - Run continusly, break at address
j - Jump Straight to address
r - show Registers and next instruction (Without executing)
v - Go to Video Mode (View the various screen structures...)
	On all screens!
	 q,<ESC> - quite the video mode (Fast)
	 p - print the virtual screen to a .bmp file

	C - chr ram
	O - Obj map
	B - Bgmap (hit any key to cycle through all 14 bg maps, <esc> to exit early)
	W - World (Hit any key to Cycle through all 32 World Maps, <esc> to exit early)
	R - Rom, view the rom graphicaly like VB_View.exe
		u/d - Up/Down 1 line
		U/D - Up/Down 1 page (Screen)
		+/- - Up/Down 1 Byte (To realign sprites when not on evin boundrys)
		</> - Left/Right 1 char (Sprite)
		1/2 - Shrink/Expand  the # of columbs displayd, helps align 
		  pictures in the rom (vBaseball, and vGolf are great canidates
		  for this
		H - help, prints this list...
	P - Print the buffers to err.txt (So we can see the world array)
	D - Display mode, start up the Interupts and go (Hit, <esc> to exit
		<ESC>     - quit....
		E,C,S,F   - (Up,Down,Left,Righ) LControler
		V,B,N,M   - (Sel,Start,B,A)
		I,'<',J,L - (Up,Down,Left,Righ) RControler
		G,H       - (L,R)
		1         - Low Battery Indicator
		2         - All butons at once...

m - dump Memory to the hard disk
? - help
q - quit
i - Generate Interupt # 

----------------------------------------------------------------------------
- Comments:
----------------------------------------------------------------------------
   This is in early Beta, it has suport for most Instructions and memory.  
   Virtual Tetris is fully playable, and VFishing, InsMouseHouse, SpaceSquash, MarioClash, TeleroBoxer, Glactic Pinball and Mario Tennis all display stuff on the screen (to various extents)
----------------------------------------------------------------------------
- Acknowlegments:
----------------------------------------------------------------------------
 Bob VanderClay (vandercl@umich.edu) - most of the code is bassed off of his VB dissasembler.

 Ben Haynor (bch132@psu.edu) - Provided me with a mutch better understanding of the VB Internals

 Joseph LoCicero, Dave Shadoff - I stole the jumptable Ideas from there tg16 dissasembler, thanks guys

 Neill Corlett (corlett@elwha.nrrc.ncsu.edu) - took many ideas (and some code) from his Starscream CPU core

 Megan Tucker - For putting up with my tinkering all night, and resisting the  urge to toss all my videogames out the window 

----------------------------------------------------------------------------  
v810 is a trademark of NEC co.
Virtual Boy is a trade mark of Nintendo
VB-98 is in now way afiliated with ither of thease parties

----------------------------------------------------------------------------
David Tucker
dbt@dana.ucc.nau.edu
http://dana.ucc.nau.edu/~dbt
 
