There’s no doubt about it. Syntax highlighting in code is the cornerstone of readability (in my mind anyway). I was browsing around today and found an assembly language highlighter for visual studio here.
Nostalgia got the better of me today in the form of some good old mode 13 demo code. All the hours I’d blown previously developing little tid-bit apps like this and I never saved off any of my code. Thankfully, I have a good memory and whilst my assembly skills aren’t “top shelf”, they’re certainly up to the task of re-creating this sort of effect.
Smoke, Fire, Flame?
They’re all the same. They work of the same principal.
Set a palette that suits your effect (yellows, reds, orange for fire), (black to white for smoke)
Create some noise as far south as you can on the video buffer
Blur the pixels out on the screen making your resulting pixels ascend and decay
Easy!
On to the code …
So, I’ll present this little demo in a couple of chunks and explain them as I show them. The code is pretty well documented anyway so that reading it line for line should be very self-explanitory.
.modelsmall.stack100h.codestart:movax,0013h; set 320x200x256 modeint10hmovax,0a000h; we can't directly address ES so moves,ax; we do so through AX. ES = A000callsetup_palette; setup a palette with greyscale ; to support the smoke effectno_kbhit:callrandomize_lines; draw some random pixels callbloom; average out the video buffermovah,01h; test for a key pressint16hjzno_kbhit; continue running if no key was hitmovax,0003h; set text modeint10hmovax,4c00h; return control back to dosint21h; -- subroutines removed for brevityendstartend
This is the main program. It needs to drop us into the required video mode, make sure we don’t want to quit (i.e. was there a key hit?), actually perform the effect (the loop of random/average) and then clean up (send us back to text mode, return control to dos).
Giving the effect some colour
Setting up the palette in this type of routine really does determine the “type” of routine that it is. As I’d said above:
Purely greyscale will give you a smokey effect on screen
Gradients running through black, red, orange, yellow, white will give you fire/flame
Black, blue up to purple will give you a cool alcohol type fire
Greens will give you something alien
The idea is to experiment with palette creation to see what comes out best for you. Here’s how I setup a greyscale palette.
setup_palette:movcx,255; 256 colour indicies to setnext_colour_idx:moval,255; setup al so that we're settingsubal,cl; colour indicies from low to highmovdx,3c7h; this port selects the colour index; that we'll set r,g,b foroutdx,almovdx,3c9h; this port sets the r,g,b components; for the selected indexshral,2; rgb intensities are in range of 0..63; so, divide by 4 to adjustoutdx,al; set the redoutdx,al; set the greenoutdx,al; set the bluedeccx; move onto the next colourjnznext_colour_idxret
So, just a touch of VGA theory here. Unlike today’s video modes, the 256 colour VGA supported 256 indices that each had an RGB intensity set ranging (0..63) each. Sometime, I don’t know how we ever used this video mode, but we got by – and made some damn cool stuff using it. So, software port 3c7 takes a colour index. 3c8 can be used to read the colour intensities (not used in this program). 3c9 is the port we use to write (r,g,b) intensities. Dividing by 4 allows me to interpolate 0..255 against 0..63 so that 0 is the colour with least intensity up to 255 which has the greatest.
###(not so) Random
Getting psuedo random numbers with nothing in the toolbox is difficult. The method that I’ve used here is to constantly read from software port 40h which is tightly coupled with the timer interrupt but it keeps a fairly steady count. The uniformity of these numbers actually provides a very tame smoke effect, you’re not going to see much chaos.
randomize_lines:movcx,640; we're going to set two rows of pixels; at the bottom of the screen to be ; random colours, so that's 640 pixelsmovdi,63360; we're going to start writing these ; pixels on the last two lines so that's; 64000 - 640next_rand_pixel:movdx,40h; we get quasi-random values from port 40hinal,dxstosb; store the pixel on screendeccx; move onto the next pixeljnznext_rand_pixelret
So, following along with the effect we only randomize the last two rows of the video array. Simple.
Airbrush, Airbrush!!
It’s just an averaging effect. We take the average of the current pixel, left, right and top most. We then re-set the pixel back into video memory 1 pixel above our current location. Therefore, we have no interest in trying to process the top-most row of video memory.
The only other part that is a little awkward to look at in this code block will be the ADC instructions. We’re dealing with bytes (values of 0..255). We’re adding 4 of these together so we’re going to quickly overflow a byte sized register. ADC (or add with carry adjustment) allows us to overflow this information into AX’s higher-order byte (AH). When it comes time to divide (or take the arithmetic average) of this pixel’s intensity, we’ll be able to perform this operation on the word sized AX register. Neat. Check it out:
bloom:movcx,63680; we average every pixel except the top rowmovdi,320; we start at the 2nd rownext_avg:xorax,ax; clear out our accumulatormoval,es:[di]; get the current pixeladdal,es:[di-1]; add the pixel to the leftadcah,0; adjust for overflowaddal,es:[di+1]; add the pixel to the rightadcah,0; adjust for overflowaddal,es:[di-320]; add the pixel aboveadcah,0; adjust for overflowshrax,2; divide by 4 to get the averagecmpal,0; can we dampen?jzno_damp; jump over if we can'tdecal; dampen the colour by 1no_damp:moves:[di-320],al; put the averaged pixel 1 pixel aboveincdi; next pixeldeccx; keep count of how many we've got leftjnznext_avgret
So, when you put it all together (and run it in DosBox) you’ll get something that looks like this:
Well.. I’m feeling all nostalgic now. Might go and fire up DosBox and play a couple of games of Double Dragon.
Haskell is a strange beast at times. I think you can see from the brute-force approach on this implementation of SPR that I was getting clearly frustrated with even some of the simplest of things.
I really could have used “Maybe”
I really could have used “Read”
It’s a learning experience:
moduleMainwhereimportSystem.IOimportSystem.RandomdataMove=Scissors|Paper|Rock|Unknownderiving(Eq,Show)dataOutcome=Winner|Draw|Loser|NDderiving(Show)str2Move::String->Movestr2Moves=docasesof"s"->Scissors"p"->Paper"r"->Rock_->UnknowngetWinner::Move->MovegetWinnerm=docasemofScissors->RockRock->PaperPaper->ScissorsUnknown->UnknowngetOutcome::Move->Move->OutcomegetOutcomeplayercpu|player==Unknown||cpu==Unknown=ND|player==cpu=Draw|cpu==winner=Loser|otherwise=Winnerwherewinner=getWinnerplayergetCpuMove::StdGen->MovegetCpuMovegen=dolet(randNumber,newGen)=randomR(1,3)gen::(Int,StdGen)caserandNumberof1->Rock2->Scissors3->Papermain=dogen<-getStdGenputStr"Enter your choice (s, p or r): "hFlushstdoutline<-getLineletplayer=str2Movelineletcpu=getCpuMovegenletoutcome=getOutcomeplayercpuputStrLn$"Player Chose: "++(showplayer)putStrLn$"Cpu Chose : "++(showcpu)putStrLn$"Outcome : "++(showoutcome)
Some who have been nice enough to comment from time to time have suggested that I move forward with this implementation (inclusion of Lizard, Spock).