<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Coder&#8217;s Diary</title>
	<atom:link href="https://cdry.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://cdry.wordpress.com</link>
	<description>0 0 1 0 1 0 1 0</description>
	<lastBuildDate>Sun, 05 Apr 2020 20:13:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<site xmlns="com-wordpress:feed-additions:1">6572007</site><cloud domain='cdry.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>https://s0.wp.com/i/buttonw-com.png</url>
		<title>Coder&#8217;s Diary</title>
		<link>https://cdry.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="https://cdry.wordpress.com/osd.xml" title="Coder&#039;s Diary" />
	<atom:link rel='hub' href='https://cdry.wordpress.com/?pushpress=hub'/>
	<item>
		<title>BFP Chapter 06: RIVERS OF BLOOD</title>
		<link>https://cdry.wordpress.com/2020/04/05/bfp-chapter-06-rivers-of-blood/</link>
					<comments>https://cdry.wordpress.com/2020/04/05/bfp-chapter-06-rivers-of-blood/#respond</comments>
		
		<dc:creator><![CDATA[mgeorgoulopoulos]]></dc:creator>
		<pubDate>Sun, 05 Apr 2020 19:44:20 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">http://cdry.wordpress.com/?p=1202</guid>

					<description><![CDATA[Download sources for this chapter Introduction This is probably the least exciting chapter in the series. Feel free to skip it, unless you are into random numbers, in which case you can just skip to the final section about random numbers. The first section might be of some relevance as well, where we create a [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a href="http://mgeo.psyflex.com/BlastFromThePast/CH06.zip">Download sources for this chapter</a></p>
<h1>Introduction</h1>
<p>This is probably the least exciting chapter in the series. Feel free to skip it, unless you are into random numbers, in which case you can just skip to the final section about random numbers. The first section might be of some relevance as well, where we create a static allocator for RAM, but this tool is not going to be used much in the subsequent chapters. The middle section has almost nothing to do with Megadrive. I just plan for a demo effect and anticipate how many cycles I can spare for its implementation. The effect was never implemented, so feel free to skip this section as well.</p>
<p><span id="more-1202"></span>I just finished watching these and I feel highly motivated. It&#8217;s a good thing to know you&#8217;re not the only psycho around:</p>
<ul>
<li><a href="https://www.youtube.com/watch?v=jOyfZex7B3E">Saturn CD cracked after 20 years.</a></li>
<li><a href="https://www.youtube.com/watch?v=2vPe452cegU">Bad apple! An awesome Megadrive demo. </a>(Note: Run it on the emulator; it&#8217;s got better sync than the video)</li>
</ul>
<h1>Handling RAM and variables</h1>
<p>It&#8217;s becoming apparent that we will need some variables in the near future. This can be as easy as pointing to an address from FF0000 onward, but I think such an approach will be troublesome later, when we have a lot of variables. Furthermore, we can&#8217;t allocate variables in the assembly source, because our binary will be mapped to ROM, so we can only put constants there.</p>
<p>I decided to write yet another tool. Let&#8217;s call it &#8216;stalloc&#8217;, for &#8220;static allocator&#8221;. The tool will parse a simple source file, like the following, and output a file containing assembler symbols assigned to their corresponding addresses:</p>
<pre>; Global variables
-global { ; The minus sign means "bypass namespace prefix"
variable1 ; All variables are words
variable2
array[23] ; array elements are words
}

level1 { ; This is a namespace called "level1"
bossPosition[] = {0, 0} ; Variables and arrays can be initialized
bossHP = $100 ; Support for hex
}

level2 {
; ...
; ...
}

bonusLevel {
; ...
; ...
}

#mutex1 { ; This is a mutually exclusive group
bonusLevel ; Namespaces can be listed here
level* ; Why not? :)
}</pre>
<p>The idea is that games and demos are usually composed of scenes, or levels where only one of them is active at any given time. So we can put all of them in the same mutually exclusive group, and they will occupy the same block of memory.</p>
<p>Namespaces that do not exist in an exclusion group will occupy their own block.</p>
<p>Also, for our convenience while looking at the memory contents, we&#8217;re going to align all blocks to 16 bytes (8 words).</p>
<p>The variables will be initialized from the source file to the value we want. We&#8217;ll just pass through the values to the assembler, so all values the assembler can recognize will be valid. The tool will output a macro that will initialize all variables to our desired values.</p>
<p>We&#8217;ll call the macro from our systemInit.x68, and then we&#8217;ll have variables to put our program state in, without caring where they actually are in memory.</p>
<p>Finally, it will be nice to enforce a minimum stack size, say 512 bytes, which will permit a simple recursion of depth 64. That should be enough.</p>
<p>So, let&#8217;s start. I&#8217;m doing this one in perl, which is my tool of choice when it comes to string manipulation.</p>
<p>And, here&#8217;s the tool&#8217;s output:</p>
<pre>; ------------------------------------------------
; malloc.x68 - RAM allocation and initialization
; Automatically generated on 24/6/2016 16:41
; ------------------------------------------------
defc RAMStart = $FF0000

; Namespace global
;--------------------------------
defc hScroll = RAMStart + $0

; Variable initialization macro
;--------------------------------
macro initializeVariables
move.l #0, hScroll
endmacro</pre>
<p>Now, let&#8217;s use the &#8216;hScroll&#8217; variable to hold our horizontal scroll value, instead of the register d1:</p>
<pre>(in app.x68)

; appUpdate is called once per frame, when the active scan starts
; ------------------------------------------------------------------------
appUpdate:
add.w #-2, hScroll

rts

; appRender is called once per frame, when VBlank starts
; ------------------------------------------------------------------------
appRender:
VDPPointToVRAM $F800
VDPWriteToData hScroll
rts</pre>
<h1>Handle DMA bit not being reset when DMA copy ends</h1>
<p>It&#8217;s a good thing I didn&#8217;t look into this right away, because during the previous day, I happened to catch a warning in the docs. It says that, due to a hardware &#8220;feature&#8221; (the nerve!), DMA transfers must be initiated from RAM. This means that you either start a transfer from code that is executed from RAM, or that the last write to the VDP control port is copied from a RAM address and not the ROM.</p>
<p>Now the easiest of course would be to try the second alternative. Since I don&#8217;t want to allocate a variable for this, I&#8217;m going to use the stack. Put the value I want to write to the stack, write it to the control port, and then pop it.</p>
<p>So, here&#8217;s how the last instruction ended up:</p>
<pre>; The last write that initiates the DMA transfer must be from RAM (go figure)
move.w #(((destinationAddress &gt;&gt; 14) &amp; 3) | $80), -(sp)
VDPWriteToControl (sp)
add.l #2, sp</pre>
<p>The DMA bit is not cleared though. This may mean more emulator bugs. I&#8217;m just going to reset it manually, and set the machine to our default state:</p>
<pre>; Reset #1 to the default value
VDPSetRegister 1, $4C ; R#01 : Display=1, VInterrupt=0, DMA=0, Vertical 30 Cell mode (PAL)</pre>
<p>Also, let us wrap the entire DMA copy thing in a macro, as it seems to have matured enough:</p>
<pre>; Fill VRAM with our cell data
VDPCopyRAMToVRAM GraphicsData, 0, $5000</pre>
<p>Now ain&#8217;t this cleaner <img src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h1>Better graphics creation tool</h1>
<p>From our previous experience while trying to create some graphics, it became obvious that we won&#8217;t get away by using bitmaps. Furthermore, I have checked some tile editors, but they are not exactly what you call &#8220;user friendly&#8221;. For better or worse, I don&#8217;t have artistically what it takes to fill the screen with beautiful graphics, and I certainly don&#8217;t think I can convince an artist to work with these tools.</p>
<p>I&#8217;m gravitating towards a new tool, which will take a carefully created bitmap (so that it reuses cells), and try to compress it, by identifying repeating cells.</p>
<p>As if this wasn&#8217;t hard enough to begin with, the tool must also identify mirrored cells (in both horizontal and vertical axes), and maybe it would also make sense to add some tolerance for one varying pixel or two, so we can trade image quality with compression rates.</p>
<p>I&#8217;m not going to make this tool right now, because it&#8217;s very complicated and I don&#8217;t currently have a use for it. I&#8217;m just leaving it as a note, in order to have the specs ready, when (if) I&#8217;m going to need a good amount of graphics.</p>
<p>And this takes care of our TO-DO list.</p>
<h1>Planning a demo effect</h1>
<p>I have been thinking about this for quite a while and it may look fantastic or horrible, but it&#8217;s worth a try.</p>
<p>This is a classic fake 3d effect I first saw in the arcades some time in late 80s. Sega&#8217;s &#8220;After Burner&#8221; had it in its intro. Here&#8217;s the mega drive version:</p>
<p><iframe class="youtube-player" width="670" height="377" src="https://www.youtube.com/embed/AcaXbr6l-54?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en&#038;autohide=2&#038;wmode=transparent" allowfullscreen="true" style="border:0;" sandbox="allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox"></iframe></p>
<div class="wpview-body">
<div class="wpview-content wpview-type-embed"></div>
</div>
<p>They apply 3D math to put the sprites in place, and also sort the sprites, so that the ones closer to the viewer cover the others.</p>
<p>My idea now, is to create a rotating sphere of spheres using this technique. But since that would be extremely lame as a demo effect (even for this hardware), I think I can make it a bit more interesting by adding a depth-of-field effect.</p>
<p>Basically, what we need in order to achieve this, is a series of ball sprites that are progressively blurred. So, the sprites that lie around our &#8220;camera&#8221; focal plane will be crisp, while the other sprites will be more and more blurred, as they move away from the focal plane.</p>
<p>Of course, this is going to look like shit, with just 1-bit transparency, so we must think of another way. Right now, my idea is to export a separate 8-bit alpha channel for each sprite. The alpha channel will be baked every frame to 1bpp by the cpu like this:</p>
<pre>Every frame:
    Every pixel:
        Generate a random 8-bit number
        Compare the random number with the 8-bit alpha value for the pixel.
        If the random number is greater, make the pixel transparent
        Update the cell in VRAM</pre>
<p>So, every sprite will appear slightly different in each frame. It will have a different dithering pattern, and the frequency of turning on and off semi-transparent pixels will be proportionate to their transparency.</p>
<p>It will obviously flicker as hell, but I think this is acceptable for this console generation. I remember the &#8220;Samurai Shodown&#8221; mega drive port used a flickery trick like this for player shadows. In that (awesome) game, the shadow from player 1 was visible in even frames and the shadow from player 2 in odd frames. The overall effect was that the shadows appeared to be semi-transparent, darkening the ground with a light flickering grey, but the region where the shadows overlapped appeared like a deforming pitch black shape, which made my heart skip a bit the first time I noticed it.</p>
<p>So, to make this effect we are going to need:</p>
<ul>
<li>Some progressively blurred sprites</li>
<li>A random number generator</li>
<li>The best framerate we can get</li>
</ul>
<p>Since we are extremely limited in terms of calculations per frame, and our plan involves per-pixel operations, I&#8217;m going to use extremely small sprites. Let&#8217;s start with 2&#215;2.</p>
<p>Furthermore, we will take advantage of the cell mirroring the VDP does for free, so that we can update just one quarter of each sprite per frame. I think I can blur a 2&#215;2 cell in GIMP and maintain the symmetry, by first blurring horizontally and then vertically. This way, the top-left cell will be symmetrical to the other 3 even after the blur.</p>
<p>We won&#8217;t need to do any pixel operations for the &#8220;focused&#8221;, crisp sprite. We can start with one blurred version and see how much of our frame time it consumes. Then we can add more blurred versions.</p>
<h1>Some early estimations</h1>
<p>Every blurred version will cost us 8&#215;8 = 64 repetitions of the above pseudocode. If I remember correctly, we have about 20K &#8220;empty loop&#8221; repetitions during the active scan, so if we estimate this repetition to be 10x the duration of the empty loop (it&#8217;s a random number generation and a comparison versus an addition), we get to perform pixel operations on about 30 cells per frame.</p>
<p>Here is how 2&#215;2 cell particles would appear on screen:</p>
<p><img data-attachment-id="1204" data-permalink="https://cdry.wordpress.com/2020/04/05/bfp-chapter-06-rivers-of-blood/2x2/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/2x2.png" data-orig-size="687,562" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="2&amp;#215;2" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/2x2.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/2x2.png?w=670" class="alignnone size-full wp-image-1204" src="https://cdry.wordpress.com/wp-content/uploads/2020/04/2x2.png?w=670" alt="2x2.png"   srcset="https://cdry.wordpress.com/wp-content/uploads/2020/04/2x2.png 687w, https://cdry.wordpress.com/wp-content/uploads/2020/04/2x2.png?w=150&amp;h=123 150w, https://cdry.wordpress.com/wp-content/uploads/2020/04/2x2.png?w=300&amp;h=245 300w" sizes="(max-width: 687px) 100vw, 687px" /></p>
<p>Which is ok-ish, but I&#8217;m not thrilled with the amount of pixels I can play with:</p>
<p><img data-attachment-id="1205" data-permalink="https://cdry.wordpress.com/2020/04/05/bfp-chapter-06-rivers-of-blood/nope/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/nope.png" data-orig-size="684,652" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="nope" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/nope.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/nope.png?w=670" class="alignnone size-full wp-image-1205" src="https://cdry.wordpress.com/wp-content/uploads/2020/04/nope.png?w=670" alt="nope.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2020/04/nope.png 684w, https://cdry.wordpress.com/wp-content/uploads/2020/04/nope.png?w=150&amp;h=143 150w, https://cdry.wordpress.com/wp-content/uploads/2020/04/nope.png?w=300&amp;h=286 300w" sizes="(max-width: 684px) 100vw, 684px" /></p>
<p>Since we estimate to have enough processing power, let&#8217;s try a larger sprite size. Say 4&#215;4 cells. This is 32&#215;32 pixels, and we only need to update the top-left 16&#215;16 region because of the symmetry. Using the same estimation, we can afford about 8 such sprites per frame. I think we can get away with fewer, say 4 blurred versions, and save the rest of the cpu time for 3D rotation and sorting, which is not at all trivial.</p>
<p>And here is how the 4&#215;4 sprites are supposed to look, in terms of size (blue blobs):</p>
<p><img data-attachment-id="1206" data-permalink="https://cdry.wordpress.com/2020/04/05/bfp-chapter-06-rivers-of-blood/4x4/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/4x4.png" data-orig-size="687,562" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="4&amp;#215;4" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/4x4.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/4x4.png?w=670" class="alignnone size-full wp-image-1206" src="https://cdry.wordpress.com/wp-content/uploads/2020/04/4x4.png?w=670" alt="4x4.png"   srcset="https://cdry.wordpress.com/wp-content/uploads/2020/04/4x4.png 687w, https://cdry.wordpress.com/wp-content/uploads/2020/04/4x4.png?w=150&amp;h=123 150w, https://cdry.wordpress.com/wp-content/uploads/2020/04/4x4.png?w=300&amp;h=245 300w" sizes="(max-width: 687px) 100vw, 687px" /></p>
<p>So, let&#8217;s hope our projections are correct (I think 10x is safe enough), and let&#8217;s create a symmetrical 4&#215;4 sprite:</p>
<p><img loading="lazy" data-attachment-id="1207" data-permalink="https://cdry.wordpress.com/2020/04/05/bfp-chapter-06-rivers-of-blood/crisp/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/crisp.png" data-orig-size="677,673" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="crisp" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/crisp.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/crisp.png?w=670" class="alignnone size-full wp-image-1207" src="https://cdry.wordpress.com/wp-content/uploads/2020/04/crisp.png?w=670" alt="crisp.png"   srcset="https://cdry.wordpress.com/wp-content/uploads/2020/04/crisp.png 677w, https://cdry.wordpress.com/wp-content/uploads/2020/04/crisp.png?w=150&amp;h=150 150w, https://cdry.wordpress.com/wp-content/uploads/2020/04/crisp.png?w=300&amp;h=298 300w" sizes="(max-width: 677px) 100vw, 677px" /></p>
<p>Aren&#8217;t I quite the artist! I added a hole in the center, which I think will make for a better depth-of-field effect.</p>
<p>Now that I look at it, maybe we should also update some transparent pixels on the &#8220;crisp&#8221; version of the sprite, just to add some antialiasing around the edges. That should make our render loop simpler as well.</p>
<p>Also let&#8217;s keep it as a note to add some palette rotation. The radial gradient just begs for it!</p>
<p>Luckily enough, our image came out to be symmetrical and divisible by 2 without any effort on our part. Now, we want to create the progressively blurred versions.</p>
<p>Since we are currently happy with the gradient (we don&#8217;t need any defined shapes in the sprite), I think I&#8217;ll skip blurring the color channels and just blur the alpha channel. For the gradient, the effect should look exactly the same.</p>
<p>And, after some tedious work of blurring and cropping, here are our bitmaps:</p>
<p><img loading="lazy" data-attachment-id="1209" data-permalink="https://cdry.wordpress.com/2020/04/05/bfp-chapter-06-rivers-of-blood/tedious/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/tedious.png" data-orig-size="903,471" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="tedious" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/tedious.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/tedious.png?w=670" class="alignnone size-full wp-image-1209" src="https://cdry.wordpress.com/wp-content/uploads/2020/04/tedious.png?w=670" alt="tedious.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2020/04/tedious.png 903w, https://cdry.wordpress.com/wp-content/uploads/2020/04/tedious.png?w=150&amp;h=78 150w, https://cdry.wordpress.com/wp-content/uploads/2020/04/tedious.png?w=300&amp;h=156 300w, https://cdry.wordpress.com/wp-content/uploads/2020/04/tedious.png?w=768&amp;h=401 768w" sizes="(max-width: 903px) 100vw, 903px" /></p>
<h1>And now, for some random numbers&#8230;</h1>
<p>Found this <a href="https://www.electro-tech-online.com/threads/ultra-fast-pseudorandom-number-generator-for-8-bit.124249/">fast 8bit random number generator</a>.</p>
<ul>
<li>3 additions</li>
<li>3 xors</li>
<li>1 shift</li>
</ul>
<p>I ran some tests on the rng:</p>
<p><img loading="lazy" data-attachment-id="1210" data-permalink="https://cdry.wordpress.com/2020/04/05/bfp-chapter-06-rivers-of-blood/uniformpass/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/uniformpass.png" data-orig-size="995,649" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="UniformPass" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/uniformpass.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/uniformpass.png?w=670" class="alignnone size-full wp-image-1210" src="https://cdry.wordpress.com/wp-content/uploads/2020/04/uniformpass.png?w=670" alt="UniformPass.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2020/04/uniformpass.png 995w, https://cdry.wordpress.com/wp-content/uploads/2020/04/uniformpass.png?w=150&amp;h=98 150w, https://cdry.wordpress.com/wp-content/uploads/2020/04/uniformpass.png?w=300&amp;h=196 300w, https://cdry.wordpress.com/wp-content/uploads/2020/04/uniformpass.png?w=768&amp;h=501 768w" sizes="(max-width: 995px) 100vw, 995px" /><br />
<img loading="lazy" data-attachment-id="1211" data-permalink="https://cdry.wordpress.com/2020/04/05/bfp-chapter-06-rivers-of-blood/rng/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/rng.jpg" data-orig-size="1024,1024" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="rng" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/rng.jpg?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/rng.jpg?w=670" class="alignnone size-full wp-image-1211" src="https://cdry.wordpress.com/wp-content/uploads/2020/04/rng.jpg?w=670" alt="rng.jpg"   srcset="https://cdry.wordpress.com/wp-content/uploads/2020/04/rng.jpg 1024w, https://cdry.wordpress.com/wp-content/uploads/2020/04/rng.jpg?w=150&amp;h=150 150w, https://cdry.wordpress.com/wp-content/uploads/2020/04/rng.jpg?w=300&amp;h=300 300w, https://cdry.wordpress.com/wp-content/uploads/2020/04/rng.jpg?w=768&amp;h=768 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>
<p>It spreads the random values uniformly, and it produces good noise, with no detectable patterns. The author states that it&#8217;s not good for cryptography, but it&#8217;s more than enough for our purpose.</p>
<p>So, here is the algorithm we need to implement:</p>
<pre>// initialize (s1,s2,s3 are 3 seed bytes)
x = 0;
a = s1;
b = s2;
c = s3;

// calculate next random
x++;
a = (a^c^x);
b = (b+a);
c = (c+(b&gt;&gt;1)^a);</pre>
<p>I&#8217;ll have to check again the C operator precedence (I&#8217;m not sure what comes first, addition or XOR), but before that, I realize this is a routine we&#8217;ll be calling too frequently.</p>
<p>Furthermore, the generator state is just 4 bytes, so we can fit it in our registers. I&#8217;m going to use 4 registers for now, even though we could fit the entire state in 1 32 bit register. Thankfully though, the 68K has plenty of registers and we are more interested in speed right now.</p>
<p>Another thing I want to achieve is make this a routine and not a macro, so it doesn&#8217;t get copied around multiple times, but, I don&#8217;t want to pay for pushing/popping the return address to/from the stack. The algorithm fits nicely in our registers and it would be a shame to spend 2 memory accesses for merely calling it!</p>
<p>Since the routine will not call any other routines, it&#8217;s safe to just put the return address to an address register (again, we&#8217;ve got plenty of them!). I think this is called a &#8220;pseudoroutine&#8221;. I may be wrong, but I like the name.</p>
<p>And because I may be creating more routines of this kind, I think I&#8217;m going to make a couple of macros, for calling/returning from &#8220;pseudoroutines&#8221;:</p>
<pre>macro pseudoCall, addr
move.l #pseudoCall\@ , a6 ; a6 will hold the return address. We use '#' to take the label as a number and not what it points to in memory
jmp \addr
pseudoCall\@: ; \@ will create a unique id "_nnnnnn" (n=number), unique for every instance of the macro
endmacro

macro pseudoRet
jmp a6 ; a6 better not have changed!
endMacro</pre>
<p>I&#8217;ve checked the <a href="http://en.cppreference.com/w/c/language/operator_precedence">C precedence.</a> So, addition comes first and then XORing follows.</p>
<p>We are now almost ready to implement our random number generator in 68K assembly. If only we knew how to XOR two values&#8230;</p>
<p><a href="http://mrjester.hapisan.com/04_MC68/Sect03Part04/Index.html" rel="nofollow">http://mrjester.hapisan.com/04_MC68/Sect03Part04/Index.html</a></p>
<p>&#8230; of course they had to name it EOR (Exlusive OR) &#8230;</p>
<p>Let&#8217;s start by assigning register names to the C variables. Following our &#8220;put stuff at the bottom&#8221; tradition, we&#8217;ll use d4, d5, d6 and d7. Also, we&#8217;re going to spread the calculations so that we have one operation per line:</p>
<pre>; Seed random number generator, using a 32 bit seed stored in d0
; This needn't be a pseudoroutine, but just for symmetry with the other routine, we'll make it one
rngSeed:
d4 = 0;
d5 = s1;
d6 = s2;
d7 = s3;
pseudoRet

; Calculate next random number, it will be stored in d7
rngNext:
d4++;
d5 ^= d7;
d5 ^= d4;
d6 += d5;

d4 &lt;&lt;= 9; ; Hack - shift d4 to the left, so we can use its 8 LSB for storing the intermediate value move.b d6, d4 ; - Tmp store d6 d4 &gt;&gt;= 1; ; - This will produce d6&gt;&gt;1 in the LSB

d7 +=.b d4; ; I wonder if I will be able to remember the symbolism later :)

d4 &gt;&gt;= 8; ; Hack - Revert d4 to what it was before

d7 ^= d5;
pseudoRet</pre>
<p>And&#8230; if we count bitwise operations to take as many cycles as additions, we are already past the 10x estimate. The compact C syntax can be misleading.</p>
<p>We could have made it a bit faster by using another register, say s3, to hold the intermediate value, but I&#8217;m already occupying half our data registers for the RNG and I don&#8217;t feel comfortable about it. For now I&#8217;m going to tell myself that the XORs cost less than the additions, and we&#8217;ll see later if that&#8217;s the case.</p>
<p>So, let&#8217;s convert that nonsense to 68K assembly. Bit shifts are restricted to a maximum of 8 bit positions per instruction, so I used another trick in place of the &gt;&gt;9. I shifted left 8 bits with instruction size of &#8216;w&#8217; (word), so that our byte rests safely in the second least significant byte of the register. Then to calculate the &gt;&gt;1, I shift with instruction size of &#8216;b&#8217; (byte), so that our stored byte remains intact. Good stuff.</p>
<pre>; Seed random number generator, using a 32 bit seed stored in d0 (s0s1s2s3 high to low bytes)
; This needn't be a pseudoroutine, but just for symmetry with the following routine, we'll make it one
rngSeed:
move.b d0, d4
lsr.l #8, d0
move.b d0, d5
lsr.l #8, d0
move.b d0, d6
lsr.l #8, d0
move.b d0, d7
pseudoRet

; Calculate next random number, it will be stored in the low byte of d7
rngNext:
add.l #1, d4
eor.b d7, d5
eor.b d4, d5
add.b d5, d6

lsl.w #8, d4 ; Hack - shift d4 to the left, so we can use its 8 LSB for storing the intermediate value
move.b d6, d4 ; - Tmp store d6
lsr.b #1, d4 ; - This will produce d6&gt;&gt;1 in the LSB

add.b d4, d7 ; Yup! I remebered it!

lsr.w #8, d4 ; Hack - Revert d4 to what it was before

eor.b d5, d7
pseudoRet</pre>
<p><img loading="lazy" data-attachment-id="1212" data-permalink="https://cdry.wordpress.com/2020/04/05/bfp-chapter-06-rivers-of-blood/hurray/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/hurray.png" data-orig-size="606,255" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="hurray" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/hurray.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2020/04/hurray.png?w=606" class="alignnone size-full wp-image-1212" src="https://cdry.wordpress.com/wp-content/uploads/2020/04/hurray.png?w=670" alt="hurray.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2020/04/hurray.png 606w, https://cdry.wordpress.com/wp-content/uploads/2020/04/hurray.png?w=150&amp;h=63 150w, https://cdry.wordpress.com/wp-content/uploads/2020/04/hurray.png?w=300&amp;h=126 300w" sizes="(max-width: 606px) 100vw, 606px" /></p>
<p>To test it, I produced 10 random numbers in both 68K assembly and the C implementation, using the same seed (all zeroes).</p>
<p>It may not seem like much, but this is actually the first algorithm I have ever implemented in an alien assembly, and I&#8217;m extremely happy my quick transcript from C worked right away!</p>
<p>Dawn is breaking.</p>
<p>Need&#8230; to&#8230; sleep&#8230;</p>
<h1>Afterword</h1>
<p>Lost you just yet? I hope not. Keep on reading because the real action is about to start!</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://cdry.wordpress.com/2020/04/05/bfp-chapter-06-rivers-of-blood/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1202</post-id>
		<media:content url="https://2.gravatar.com/avatar/571c802b5077513106d8a8eb429d60da2ef7d8027fc6b90454300a8da0d37a5e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mgeorgoulopoulos</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2020/04/2x2.png" medium="image">
			<media:title type="html">2x2.png</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2020/04/nope.png" medium="image">
			<media:title type="html">nope.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2020/04/4x4.png" medium="image">
			<media:title type="html">4x4.png</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2020/04/crisp.png" medium="image">
			<media:title type="html">crisp.png</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2020/04/tedious.png" medium="image">
			<media:title type="html">tedious.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2020/04/uniformpass.png" medium="image">
			<media:title type="html">UniformPass.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2020/04/rng.jpg" medium="image">
			<media:title type="html">rng.jpg</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2020/04/hurray.png" medium="image">
			<media:title type="html">hurray.PNG</media:title>
		</media:content>
	</item>
		<item>
		<title>1024 words</title>
		<link>https://cdry.wordpress.com/2018/09/15/1024-words/</link>
					<comments>https://cdry.wordpress.com/2018/09/15/1024-words/#respond</comments>
		
		<dc:creator><![CDATA[mgeorgoulopoulos]]></dc:creator>
		<pubDate>Sat, 15 Sep 2018 14:03:15 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">http://cdry.wordpress.com/?p=1186</guid>

					<description><![CDATA[I made this word list to make a utility that converts numbers to short (and memorable) word sequences. Here are some of the list&#8217;s properties: Got a list of English words, sorted by popularity. Kept only 3 to 10 letter words. Removed similar words. All words of equal size differ by at least 2 letters. [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>I made this word list to make a utility that converts numbers to short (and memorable) word sequences.<br />
<span id="more-1186"></span><br />
Here are some of the list&#8217;s properties:</p>
<ul>
<li>Got a list of English words, sorted by popularity.</li>
<li>Kept only 3 to 10 letter words.</li>
<li>Removed similar words. All words of equal size differ by at least 2 letters.</li>
<li>Removed plurals.</li>
<li>Removed verbs.</li>
<li>Kept the top 1024 remaining words. So each word can represent 10 bits.</li>
</ul>
<p>Here it is:</p>
<pre>able about above absolutely accept access accident according
account across act acting action actually adam address
admit advice afford afraid after afternoon again against
agent ago agree agreed ahead alarm alex alive
all allow allowed almost alone already alright also
although always amazing america among ancient and angel
angry animal anna another answer anybody anymore anyone
anything anyway anywhere apart apartment apologize apparently applause
appreciate area army around arrest arrested arrived ask
asleep attack attention audience aunt aware away awesome
awful baby back bar base based bathroom battle
beautiful beauty because become bed before begin beginning
behind being believe belonging besides best better between
beyond big bigger biggest bird birth birthday bitch
bite black blame blessed blind blood blow blue
board boat bobby body bomb both bottom boy
boyfriend boys brave break breakfast breaking breath breathing
brian bridge bright brilliant british bro broken brother
brown buddy building bullet buried business busy but
call calling camera camp can captain card career
careful cash castle cause center certain certainly chair
challenge change changed character charles charlie check checked
cheering cheers chef chest chicken chief child children
chinese choice chris christ christmas chuckles church city
class classic clear clearly client close closed clothes
club coach coffee college colonel color coming command
commander common company complete completely computer concerned condition
consider contact contest continue control cool cop corner
correct country couple course court cousin cover covered
crazy created credit crew crime criminal cross crowd
cute daddy damage dance danger dangerous daniel danny
dark darling daughter david day dead death decide
decided decision definitely department desk destroy destroyed detective
devil difference different difficult dinner director dirty discovered
discussion distance division doctor dog dollar done door
double doubt down draw dream dress dressed drink
drive driver drop dropped drug dude due dumb
during duty each earlier early earth easier easily
easy eddie either else emergency empty ended ending
enemy energy engine england english enough entire eric
escape especially even evening every everybody everyone everything
everywhere evidence evil exactly example excellent except excited
excuse exercise exist expected experience extra eyes face
faith family famous fans fantastic faster father fault
fbi feel feeling felt female field fighting figure
film final finally finding finger finish fire first
fish fix flat flight floor flowers fly focus
folks followed foot football for force forced forever
forgotten form forward france frank free freedom french
fresh friday friend from front full funeral funny
further future garden general gentlemen george german ghost
gift girl girlfriend given glad god good goodbye
government grace grand grandma great greatest green group
grow grunts guard guess guilty guy hair half
hand handle handsome happening happiness happy harder hardly
health heaven heavy hello help henry her here
herself high him himself history holy honest honestly
honor hope hoping horrible hospital hotel hour house
how however huge human hundred hungry hurt husband
ice idea idiot important impossible incredible indeed influence
innocent insane inside inspector instead interest interested interview
into invited involved island issue itself jail james
jane japanese jealous jeff jerry jesus jimmy job
john johnny joke judge just justice kenny kept
kevin key kill killed kinda king kitchen knife
knock ladies lady language large last late lately
later laugh law lawyer leader least left leg
less level lie light like little living local
london long longer look loose lord love lovely
luck lucky lunch machine madam magic majesty major
mama manager many market marriage married martin matter
max maybe mean meaning medical medicine meet meeting
member memory men mention message michael middle mile
military million minister minute miss missed missing mission
mistake model mom moment mommy money monster month
more morning most mother mountain movie moving much
murder murdered music myself name named narrator national
natural nature nearly necessary neck need needed neither
nephew nervous network never new next nice nobody
noise nonsense normal nose not nothing notice noticed
nowhere number obviously off offer office officer often
oil okay old once one only open opened
opening operation opinion order ordered original other otherwise
our ourselves outside over own pain pal papa
paper pardon parents paris part partner party pass
path patient paul people perfect perfectly perhaps person
personal pete peter phone photo pick picture piece
place plan planet playing please pleasure plenty plus
point police position possible power powerful prefer pregnant
prepared present president pressure pretty price prince princess
printer prison private probably problem process professor program
promise proof property protector proud public pudding purpose
purse queen question quick quickly quiet quit quite
radio raise ready real reality really reason recently
record relaxation release remember report research respect restaurant
return rich richard ridiculous risk river road robert
rock roger roll room ruined rule run running
russian ryan safe said sarah saturday sauce saved
saying scared scene school science scott scream screw
sea search seat second secret security sense serious
seriously server service seven several shallow share sheep
sheriff ship shirt shoe shoot shooting side signal
signed silence silly simon simple single sir sister
sitting situation skin slept slowly smell smile smoke
social society soldier somebody somehow someone something sometimes
somewhere soon sorry sort soul sound space spanish
special speech spirit split spot spread stand standard
start state station stayed steel steve stick still
stole stolen stomach stop stopped story straight strange
street strength strike strong student study stuff stupid
success sudden sugar suicide summer sunday super support
supposed sure surely surgery surprise surprised suspect sweet
sweetheart sweetie sword system table taken talk target
taste teacher team teeth terrible thanks that the
their themselves there they things third this thomas
those though thought thousand three throat through ticket
time tired today together tomorrow tonight tony too
total totally towards train training travel trial trip
trouble truck true truly trust truth try tunnel
turn turning twice type ugly uncle under understood
unit united universe unless until upon upset upstairs
used usual usually very victim video view village
visit voice vote waiting wanted warm watch weapon
weather wedding week weekend weight weird welcome well
whatever when whenever whether which while who whoever
whom wife wild window with within without witness
woman wonder wonderful work working worried worse worth
written wrong yeah yes yesterday you young yourself
</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://cdry.wordpress.com/2018/09/15/1024-words/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1186</post-id>
		<media:content url="https://2.gravatar.com/avatar/571c802b5077513106d8a8eb429d60da2ef7d8027fc6b90454300a8da0d37a5e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mgeorgoulopoulos</media:title>
		</media:content>
	</item>
		<item>
		<title>BFP Chapter 05: Scrolling</title>
		<link>https://cdry.wordpress.com/2018/07/07/bfp-chapter-05-scrolling/</link>
					<comments>https://cdry.wordpress.com/2018/07/07/bfp-chapter-05-scrolling/#comments</comments>
		
		<dc:creator><![CDATA[mgeorgoulopoulos]]></dc:creator>
		<pubDate>Sat, 07 Jul 2018 12:44:25 +0000</pubDate>
				<category><![CDATA[BFP Series]]></category>
		<category><![CDATA[mega drive]]></category>
		<category><![CDATA[scrolling]]></category>
		<category><![CDATA[VDP]]></category>
		<guid isPermaLink="false">http://cdry.wordpress.com/?p=1173</guid>

					<description><![CDATA[Download sources for this chapter Introduction In this chapter we&#8217;re going to handle timing and have our code run periodically. This will enable us to do such cool things as a scrolling background. We&#8217;re also going to ingest the super mushroom! We&#8217;ll start looking at what other people have done. Figuring out stuff by myself [&#8230;]]]></description>
										<content:encoded><![CDATA[<p style="text-align:justify;"><a href="http://mgeo.psyflex.com/BlastFromThePast/CH05.zip" target="_blank" rel="noopener"><em>Download sources for this chapter</em></a></p>
<h1 style="text-align:justify;">Introduction</h1>
<p style="text-align:justify;">In this chapter we&#8217;re going to handle timing and have our code run periodically. This will enable us to do such cool things as a scrolling background.<br />
We&#8217;re also going to ingest the super mushroom! We&#8217;ll start looking at what other people have done. Figuring out stuff by<span id="more-1173"></span> myself has been fun but it gets old real soon.</p>
<p style="text-align:justify;">So, let&#8217;s dig in!</p>
<h1 style="text-align:justify;">Creating a main loop</h1>
<p style="text-align:justify;">There are different kinds of things we can do during the active scan and the vertical retrace periods. During the active scan, VDP is really busy, so we&#8217;d better not mess with it too much. While the VDP is putting pixels on screen, the CPU has &#8220;limited access&#8221; to it. This means that the writes we do to data and control ports of the VDP will be consumed less frequently by the VDP, so the corresponding FIFO will become full quickly. I think I saw somewhere in the manual that the FIFO has enough space for 4 writes.</p>
<p style="text-align:justify;">It is both a good and bad thing that, after the FIFO becomes full, the CPU must wait before it can write again. Thank God for this, because otherwise we&#8217;d have to check if the FIFO is full before writing. This would complicate things a lot. The bad thing is that we get a performance penalty. Fortunately this is small (about 5-6 microseconds), but nevertheless we don&#8217;t want to perform frequent writes to the VDP during active scan. These small delays could stack up pretty easily.</p>
<p style="text-align:justify;">During the vertical retrace, we have unlimited access to the VDP.</p>
<p style="text-align:justify;">Given the above, the simplest way to organize our main loop would be:</p>
<pre>	Update (perform game logic etc)
	Wait for VBlank
	Render (push cells/indices/palettes to VDP)
	Wait for active scan
	Repeat
</pre>
<p style="text-align:justify;">Probably, this is the only way we can do it. There&#8217;s this VBlank interrupt that bugs me. I&#8217;m trying to figure out what its purpose is, and if we could somehow make use of it. For example, we could set a flag in the interrupt, so that our update routine terminates, but this requires that our update routine is periodically checking for that flag. And if we do that, we don&#8217;t even need to use the interrupt. The flag is already available to us in the VDP &#8220;status&#8221; register:</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1174" data-permalink="https://cdry.wordpress.com/2018/07/07/bfp-chapter-05-scrolling/statusregister/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/statusregister.png" data-orig-size="841,132" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="statusRegister" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/statusregister.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/statusregister.png?w=670" class="alignnone size-full wp-image-1174" src="https://cdry.wordpress.com/wp-content/uploads/2018/07/statusregister.png?w=670" alt="statusRegister.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2018/07/statusregister.png 841w, https://cdry.wordpress.com/wp-content/uploads/2018/07/statusregister.png?w=150&amp;h=24 150w, https://cdry.wordpress.com/wp-content/uploads/2018/07/statusregister.png?w=300&amp;h=47 300w, https://cdry.wordpress.com/wp-content/uploads/2018/07/statusregister.png?w=768&amp;h=121 768w" sizes="(max-width: 841px) 100vw, 841px" /></p>
<p style="text-align:justify;">The other thing that&#8217;s bugging me is that we have absolutely no way of controlling when a frame is displayed. Actually, the frame is controlling us. So, in case our update routine takes much time, we will end up waiting for the VBlank of a next frame. I guess the only way around this is to make sure that our update routine is short enough.</p>
<p style="text-align:justify;">Since there&#8217;s no practical way of enforcing this, the next best thing is to somehow display a warning if update exceeds its time limit. Let&#8217;s push that to our TO-DO stack and carry on with our main loop. We need two routines to implement it:</p>
<pre>	VDPWaitForVBlank
	VPDWaitForActiveScan
</pre>
<p style="text-align:justify;">Both routines will loop until a condition is met. They&#8217;re going to be similar to what we did for waiting for DMA completion:</p>
<pre>VDPWaitForDMA:
	VDPReadStatus d0
	btst #3, d0
	bne VDPWaitForDMA
	rts
</pre>
<p style="text-align:justify;">This time we&#8217;re looking at bit #2. Let&#8217;s start with waiting for the active scan, which will be nearly identical to waiting for DMA completion. We&#8217;re looking for a bit to be cleared:</p>
<pre>VDPWaitForActiveScan:
	VDPReadStatus d0
	btst #2, d0
	bne VDPWaitForActiveScan
	rts
</pre>
<p style="text-align:justify;">And then, we flip the condition to wait for VBlank (the things I&#8217;m capable of, to avoid understanding again what &#8220;bne&#8221; means):</p>
<pre>VDPWaitForVBlank:
	VDPReadStatus d0
	btst #2, d0
	beq VDPWaitForVBlank
	rts
</pre>
<h1 style="text-align:justify;">Ready to roll</h1>
<p style="text-align:justify;">We are now ready to make our main loop. But I&#8217;d really like to make it do something. Would scrolling our background be too much to ask? Let&#8217;s see what the manual has to say&#8230;</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1175" data-permalink="https://cdry.wordpress.com/2018/07/07/bfp-chapter-05-scrolling/ihadtoask/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/ihadtoask.png" data-orig-size="876,449" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="IhadToAsk" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/ihadtoask.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/ihadtoask.png?w=670" class="alignnone size-full wp-image-1175" src="https://cdry.wordpress.com/wp-content/uploads/2018/07/ihadtoask.png?w=670" alt="IhadToAsk.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2018/07/ihadtoask.png 876w, https://cdry.wordpress.com/wp-content/uploads/2018/07/ihadtoask.png?w=150&amp;h=77 150w, https://cdry.wordpress.com/wp-content/uploads/2018/07/ihadtoask.png?w=300&amp;h=154 300w, https://cdry.wordpress.com/wp-content/uploads/2018/07/ihadtoask.png?w=768&amp;h=394 768w" sizes="(max-width: 876px) 100vw, 876px" /></p>
<p style="text-align:justify;">I had to ask, hadn&#8217;t I?</p>
<p style="text-align:justify;">So, to scroll horizontally, essentially you fill this table with the same offset value. Even slots of the table will affect scroll A and odd will affect scroll B. We can also specify a different offset per scanline, which would enable us to do some nice effects. I never liked a game that didn&#8217;t have a fake perspective floor.</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1176" data-permalink="https://cdry.wordpress.com/2018/07/07/bfp-chapter-05-scrolling/sf2/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/sf2.png" data-orig-size="640,480" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="sf2" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/sf2.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/sf2.png?w=640" class="alignnone size-full wp-image-1176" src="https://cdry.wordpress.com/wp-content/uploads/2018/07/sf2.png?w=670" alt="sf2.png"   srcset="https://cdry.wordpress.com/wp-content/uploads/2018/07/sf2.png 640w, https://cdry.wordpress.com/wp-content/uploads/2018/07/sf2.png?w=150&amp;h=113 150w, https://cdry.wordpress.com/wp-content/uploads/2018/07/sf2.png?w=300&amp;h=225 300w" sizes="(max-width: 640px) 100vw, 640px" /></p>
<p style="text-align:justify;">But I&#8217;m not going to do it right now. Alternatively, you can only set the first table position for the entire scrool A and the second for B. I&#8217;m not in the mood right now to push 480 words to VRAM, during the VBlank, which, as we saw earlier, is severely limited. In order to do that, you set register #11 to be zero, which we already did during initialization.</p>
<p style="text-align:justify;">Like the sprite table, this table will be more or less permanent in VRAM. So, let&#8217;s put it right above the sprite table.</p>
<p style="text-align:justify;">We&#8217;re putting the HScroll table to F800, which is right above the sprite table, as tightly packed as the resolution permits (1KB increments &#8211; 6 most significant bits of the 16 bit address). We set those bits to register #13.</p>
<p style="text-align:justify;">By the way, here is the main program logic:</p>
<pre>	jsr appStart					; app initialization routine
	jsr VDPWaitForActiveScan		; wait for the next active scan
	
mainLoop:
	jsr appUpdate					; app update - during active scan
	jsr VDPWaitForVBlank
	jsr appRender					; app render - during VBlanking
	jsr VDPWaitForActiveScan
	jmp mainLoop
</pre>
<p style="text-align:justify;">I put the start/update/render routines in a separate file called &#8220;app.x68&#8221;. Now our previous test&#8217;s code is in appStart. We now have a cleaner main file. Actually let me rename it from &#8220;test.x68&#8221; to &#8220;entryPoint.x68&#8221;, to be more descriptive.</p>
<p style="text-align:justify;">Now, let&#8217;s actually do something in our update routine. I&#8217;m just going to increase d1 by one:</p>
<pre>appUpdate:
	add.l #1, d1	
	rts
</pre>
<p style="text-align:justify;">It&#8217;s going way too fast. I&#8217;ve put code in the wait functions as well, to increase other registers every time they loop. I then divided these registers with d1 to find out that we can do 6 iterations of adding and branching back per frame. I know the Mega Drive is a bit limited, but this is just ridiculous! Furthermore, my frame counter goes up really fast. In the order of thousands of frames per second.</p>
<p style="text-align:justify;">This can only mean one thing: we are not actually waiting for VSync.</p>
<p style="text-align:justify;">Maybe we&#8217;re not testing the right bit? Nope, I double-checked the docs.</p>
<h1 style="text-align:justify;">&#8211; Enough is enough. &#8211;</h1>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1177" data-permalink="https://cdry.wordpress.com/2018/07/07/bfp-chapter-05-scrolling/head-in-hands/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/facepalm.jpg" data-orig-size="1024,683" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;10&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;Canon EOS 5D&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1260822947&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;200&quot;,&quot;iso&quot;:&quot;400&quot;,&quot;shutter_speed&quot;:&quot;0.0008&quot;,&quot;title&quot;:&quot;Head in Hands&quot;,&quot;orientation&quot;:&quot;1&quot;}" data-image-title="Head in Hands" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/facepalm.jpg?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/facepalm.jpg?w=670" class="  wp-image-1177 alignleft" src="https://cdry.wordpress.com/wp-content/uploads/2018/07/facepalm.jpg?w=265&#038;h=177" alt="facepalm.jpg" width="265" height="177" srcset="https://cdry.wordpress.com/wp-content/uploads/2018/07/facepalm.jpg?w=265&amp;h=177 265w, https://cdry.wordpress.com/wp-content/uploads/2018/07/facepalm.jpg?w=530&amp;h=354 530w, https://cdry.wordpress.com/wp-content/uploads/2018/07/facepalm.jpg?w=150&amp;h=100 150w, https://cdry.wordpress.com/wp-content/uploads/2018/07/facepalm.jpg?w=300&amp;h=200 300w" sizes="(max-width: 265px) 100vw, 265px" /></p>
<p style="text-align:justify;">This is a major turning point in this series. From now on, I&#8217;m going to utilise every single bit of information I can get my hands on. Because otherwise we&#8217;re getting nowhere. I&#8217;ll find another way to add some adventure to it.</p>
<p style="text-align:justify;">There are far too many errors in the sega2 document. Look at this for example:</p>
<pre>WaitVBlankStart:
	move.w  vdp_control, d0	; Move VDP status word to d0
	andi.w  #0x0008, d0     ; AND with bit 4 (vblank), result in status register
	bne     WaitVBlankStart ; Branch if not equal (to zero)
	rts

WaitVBlankEnd:
	move.w  vdp_control, d0	; Move VDP status word to d0
	andi.w  #0x0008, d0     ; AND with bit 4 (vblank), result in status register
	beq     WaitVBlankEnd   ; Branch if equal (to zero)
	rts
</pre>
<p style="text-align:justify;">This is written by <a href="https://bigevilcorporation.co.uk/2012/05/30/sega-megadrive-9-maps-and-scrolling-planes/" target="_blank" rel="noopener">Matt, from BIG EVIL CORPORATION</a>.</p>
<p style="text-align:justify;">Again, I&#8217;m not going to try and trace where this info came from, but I&#8217;d really like to recursively thank everyone who helped solve this ridiculous mystery.</p>
<p style="text-align:justify;">Also, the BIG EVIL CORPORATION blog has a very nice wordpress theme. I&#8217;m stealing that as well.</p>
<p style="text-align:justify;">So, after all, we need to check for byte #3. Fine by me, but then, what on earth were we doing when we were waiting for DMA completion? We were actually waiting for the next frame&#8230;</p>
<p style="text-align:justify;">So, where is the DMA bit then? Aha:</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1178" data-permalink="https://cdry.wordpress.com/2018/07/07/bfp-chapter-05-scrolling/realstatusregister/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/realstatusregister.png" data-orig-size="956,553" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="realStatusRegister" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/realstatusregister.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/realstatusregister.png?w=670" class="alignnone size-full wp-image-1178" src="https://cdry.wordpress.com/wp-content/uploads/2018/07/realstatusregister.png?w=670" alt="realStatusRegister.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2018/07/realstatusregister.png 956w, https://cdry.wordpress.com/wp-content/uploads/2018/07/realstatusregister.png?w=150&amp;h=87 150w, https://cdry.wordpress.com/wp-content/uploads/2018/07/realstatusregister.png?w=300&amp;h=174 300w, https://cdry.wordpress.com/wp-content/uploads/2018/07/realstatusregister.png?w=768&amp;h=444 768w" sizes="(max-width: 956px) 100vw, 956px" /></p>
<p style="text-align:justify;">I&#8217;m keeping <a href="https://emu-docs.org/Genesis/Graphics/genvdp.txt" target="_blank" rel="noopener">this txt by Charles MacDonald.</a></p>
<p style="text-align:justify;">So we correct all 3 of our waiting routines. Now, frame counts are more believable.</p>
<h1 style="text-align:justify;">Some stats</h1>
<p style="text-align:justify;">Again, I let it run for a while and did the divisions in windows calculator. Seems like we can do about 2658 repetitions of &#8220;add, read status, check bit, branch&#8221; in our update routine, and about 242 in our render routine. Which totally sucks.</p>
<p style="text-align:justify;">So, VBlanking will take about 8% of our time. That&#8217;s an easier number to keep in mind.</p>
<h1 style="text-align:justify;">Back to scrolling</h1>
<p style="text-align:justify;">The only thing left to do now, is write the value of d1 to VRAM location F800. A macro would be handy for writing VRAM:</p>
<pre>	macro VDPPointToVRAM, addr
		VDPWriteToControl #(( \addr &amp; $3FFF) | $4000)
		VDPWriteToControl #(( \addr &gt;&gt; 14) &amp; 3)
	endmacro
</pre>
<p style="text-align:justify;">And then, here is how we scroll:</p>
<pre>	VDPPointToVRAM $F800
	VDPWriteToData d1
</pre>
<p style="text-align:justify;">And yes! It slides!</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1179" data-permalink="https://cdry.wordpress.com/2018/07/07/bfp-chapter-05-scrolling/itslides/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/itslides.png" data-orig-size="625,495" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="itslides" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/itslides.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/itslides.png?w=625" class="alignnone size-full wp-image-1179" src="https://cdry.wordpress.com/wp-content/uploads/2018/07/itslides.png?w=670" alt="itslides.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2018/07/itslides.png 625w, https://cdry.wordpress.com/wp-content/uploads/2018/07/itslides.png?w=150&amp;h=119 150w, https://cdry.wordpress.com/wp-content/uploads/2018/07/itslides.png?w=300&amp;h=238 300w" sizes="(max-width: 625px) 100vw, 625px" /></p>
<p style="text-align:justify;">I&#8217;m only scrolling plane A. It seems to be enough, as plane A is of higher priority than plane B, there are no transparent pixels in our image, so we can&#8217;t see plane B anyway.</p>
<p style="text-align:justify;">Now, I&#8217;d like it to scroll a bit faster and from right to left:</p>
<pre>	add.l #-4, d1
</pre>
<h1 style="text-align:justify;">Initializing the rest of the VDP (CRAM, VSRAM)</h1>
<p style="text-align:justify;">I&#8217;m adding a similar macro to point to CRAM, so we can easily change colors:</p>
<pre>	macro VDPPointToCRAM, addr
		VDPWriteToControl #(( \addr &amp; $3FFF) | $C000)
		VDPWriteToControl #(( \addr &gt;&gt; 14) &amp; 3)
	endmacro
</pre>
<p style="text-align:justify;">And one more for VSRAM (Vertical Scrolling RAM):</p>
<pre>	macro VDPPointToVSRAM, addr
		VDPWriteToControl #(( \addr &amp; $3FFF) | $4000)
		VDPWriteToControl #((( \addr &gt;&gt; 14) &amp; 3) | $1000)
	endmacro
</pre>
<p style="text-align:justify;">And here is how we clear both the CRAM and VSRAM to zero in our initialization routine:</p>
<pre>	VDPPointToCRAM 0
	repeat 64
		VDPWriteToData #0
	endrepeat
	
	VDPPointToVSRAM 0
	repeat 40
		VDPWriteToData #0
	endrepeat
</pre>
<h1 style="text-align:justify;">A little more housekeeping</h1>
<p style="text-align:justify;">Sprite table and Hscroll table will be constants, so let&#8217;s make some assembler symbols for them:</p>
<pre>	defc VDPSpriteTable = $FE00
	defc VDPHScrollTable = $F800
</pre>
<p style="text-align:justify;">Next item in my list is: &#8220;Optimize double writes to control with a long write&#8221;. I don&#8217;t know if this is going to save some cycles, but let&#8217;s do it anyway.</p>
<p style="text-align:justify;">I&#8217;m creating two more macros VDPWrite*L, with the &#8216;L&#8217; suffix:</p>
<pre>	macro VDPWriteToControlL, value
		move.l \value, $C00004
	endmacro
	
	macro VDPWriteToDataL, value
		move.l \value, $C00000
	endmacro
</pre>
<p style="text-align:justify;">And replacing my writes with packed long word writes throughout the code, like this:</p>
<pre>	VDPWriteToControlL #$40000080		; Destination address (0)
</pre>
<h1 style="text-align:justify;">Afterword</h1>
<p style="text-align:justify;">Having taken care of a lot of items in our list, it&#8217;s time to move to somewhat higher level programs. Stay tuned for next chapter where we&#8217;ll create a primitive memory manager that will let us refer to memory locations using variable names instead of numbers.</p>
<table>
<tbody>
<tr>
<td><a href="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/">Previous Chapter</a></td>
<td><a href="https://cdry.wordpress.com/2017/01/08/a-blast-from-the-past-toc/">TOC</a></td>
<td>Next Chapter</td>
</tr>
</tbody>
</table>
]]></content:encoded>
					
					<wfw:commentRss>https://cdry.wordpress.com/2018/07/07/bfp-chapter-05-scrolling/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1173</post-id>
		<media:thumbnail url="https://cdry.wordpress.com/wp-content/uploads/2018/07/itslides.png" />
		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2018/07/itslides.png" medium="image">
			<media:title type="html">itslides</media:title>
		</media:content>

		<media:content url="https://2.gravatar.com/avatar/571c802b5077513106d8a8eb429d60da2ef7d8027fc6b90454300a8da0d37a5e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mgeorgoulopoulos</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2018/07/statusregister.png" medium="image">
			<media:title type="html">statusRegister.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2018/07/ihadtoask.png" medium="image">
			<media:title type="html">IhadToAsk.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2018/07/sf2.png" medium="image">
			<media:title type="html">sf2.png</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2018/07/facepalm.jpg" medium="image">
			<media:title type="html">facepalm.jpg</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2018/07/realstatusregister.png" medium="image">
			<media:title type="html">realStatusRegister.PNG</media:title>
		</media:content>
	</item>
		<item>
		<title>Representative Image Color</title>
		<link>https://cdry.wordpress.com/2018/07/06/representative-image-color/</link>
					<comments>https://cdry.wordpress.com/2018/07/06/representative-image-color/#respond</comments>
		
		<dc:creator><![CDATA[mgeorgoulopoulos]]></dc:creator>
		<pubDate>Fri, 06 Jul 2018 09:44:20 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[average]]></category>
		<category><![CDATA[Color]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[quantization]]></category>
		<category><![CDATA[representative]]></category>
		<guid isPermaLink="false">http://cdry.wordpress.com/?p=1168</guid>

					<description><![CDATA[I have thought about this for years, in a very low-priority thread in my head. There are many situations where it could be useful. In this particular instance, we are making a rotoscoping application with Nuclear, where we draw polygons over a video, to create animations for a game we&#8217;re making. The polygons have uniform [&#8230;]]]></description>
										<content:encoded><![CDATA[<p style="text-align:justify;">I have thought about this for years, in a very low-priority thread in my head.</p>
<p style="text-align:justify;">There are many situations where it could be useful. In this particular instance, we are making a <a href="https://en.wikipedia.org/wiki/Rotoscoping" target="_blank" rel="noopener">rotoscoping </a>application with <a href="http://nuclear.mutantstargoat.com/" target="_blank" rel="noopener">Nuclear</a>, where we draw polygons over a video, to create animations for<span id="more-1168"></span> a game we&#8217;re making.</p>
<p style="text-align:justify;">The polygons have uniform fill, so it would be very useful to get a representative color automatically, based on the video pixels that lie underneath each polygon.</p>
<p style="text-align:justify;">The first thing that comes to mind is to just get the average color. This is a simple method, but it can lead to weird results.</p>
<p style="text-align:justify;">For instance, if we have an image that contains mostly blue, but a significant portion of the pixels is red, we&#8217;ll end up with a purple average, which is not at all representative of the picture.</p>
<p style="text-align:justify;">The next thing I tried was to select the pixel that is closest to average. This works better, but still, in most images you&#8217;ll find edge pixels that vary smoothly between the colors the edge separates. So, in the blue-red example above, it is possible that a purple pixel is found in the boundary between blue and red. In which case we&#8217;ll end up with purple, again.</p>
<p style="text-align:justify;">So, here is what I came up with:</p>
<ul>
<li>Get the average of all pixels.</li>
<li>Sort all pixels; from closest to more distant from average.</li>
<li>Discard the second half (the most distant pixels from average).</li>
<li>Repeat, until you end up with just 1 pixel.</li>
</ul>
<p>This works because it progressively gets rid of &#8220;impurities&#8221;.</p>
<p>So, for our blue-red example, in the first iteration, a purplish average will be calculated. This average will be more similar to blue though. So, we&#8217;ll discard all the red pixels in the first iteration.</p>
<p>The next iterations will progressively refine the blue average, until we end up with the most representative blue that can be found in the image.</p>
<p>Because this can be a bit slow, here is an optimization: calculate the representative color of each scanline first, then find the representative of the representatives.</p>
<p>Example output:</p>
<p><img loading="lazy" data-attachment-id="1169" data-permalink="https://cdry.wordpress.com/2018/07/06/representative-image-color/capture-3/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/capture.png" data-orig-size="887,534" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Capture" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/capture.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2018/07/capture.png?w=670" class=" size-full wp-image-1169 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2018/07/capture.png?w=670" alt="Capture"   srcset="https://cdry.wordpress.com/wp-content/uploads/2018/07/capture.png 887w, https://cdry.wordpress.com/wp-content/uploads/2018/07/capture.png?w=150&amp;h=90 150w, https://cdry.wordpress.com/wp-content/uploads/2018/07/capture.png?w=300&amp;h=181 300w, https://cdry.wordpress.com/wp-content/uploads/2018/07/capture.png?w=768&amp;h=462 768w" sizes="(max-width: 887px) 100vw, 887px" /></p>
<p>Doesn&#8217;t it seem like a person picked those colors? <img src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://cdry.wordpress.com/2018/07/06/representative-image-color/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1168</post-id>
		<media:thumbnail url="https://cdry.wordpress.com/wp-content/uploads/2018/07/capture.png" />
		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2018/07/capture.png" medium="image">
			<media:title type="html">Capture</media:title>
		</media:content>

		<media:content url="https://2.gravatar.com/avatar/571c802b5077513106d8a8eb429d60da2ef7d8027fc6b90454300a8da0d37a5e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mgeorgoulopoulos</media:title>
		</media:content>
	</item>
		<item>
		<title>Self-similarity Image Scaler</title>
		<link>https://cdry.wordpress.com/2018/05/07/self-similarity-image-scaler/</link>
					<comments>https://cdry.wordpress.com/2018/05/07/self-similarity-image-scaler/#respond</comments>
		
		<dc:creator><![CDATA[mgeorgoulopoulos]]></dc:creator>
		<pubDate>Mon, 07 May 2018 18:04:38 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[image scaling]]></category>
		<category><![CDATA[linear]]></category>
		<category><![CDATA[nearest]]></category>
		<category><![CDATA[self similarity]]></category>
		<guid isPermaLink="false">http://cdry.wordpress.com/?p=1164</guid>

					<description><![CDATA[I implemented a fancy image scaling algorithm for a uni project. I think of doing a proper blog post about this and add the code to github, when I get some free time. &#160; Meanwhile, I couldn&#8217;t resist posting this shot:]]></description>
										<content:encoded><![CDATA[<p>I implemented a<a href="http://www.cs.huji.ac.il/~raananf/projects/lss_upscale/paper.pdf" target="_blank" rel="noopener"> fancy image scaling algorithm </a>for a uni project. I think of doing a proper blog post about this and add the code to github, when I get some free time.</p>
<p>&nbsp;</p>
<p>Meanwhile, I couldn&#8217;t resist posting this shot:</p>
<p><img loading="lazy" data-attachment-id="1165" data-permalink="https://cdry.wordpress.com/2018/05/07/self-similarity-image-scaler/selfsim/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2018/05/selfsim.png" data-orig-size="1255,488" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="selfsim" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2018/05/selfsim.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2018/05/selfsim.png?w=670" class="alignnone size-full wp-image-1165" src="https://cdry.wordpress.com/wp-content/uploads/2018/05/selfsim.png?w=670" alt="selfsim.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2018/05/selfsim.png 1255w, https://cdry.wordpress.com/wp-content/uploads/2018/05/selfsim.png?w=150&amp;h=58 150w, https://cdry.wordpress.com/wp-content/uploads/2018/05/selfsim.png?w=300&amp;h=117 300w, https://cdry.wordpress.com/wp-content/uploads/2018/05/selfsim.png?w=768&amp;h=299 768w, https://cdry.wordpress.com/wp-content/uploads/2018/05/selfsim.png?w=1024&amp;h=398 1024w" sizes="(max-width: 1255px) 100vw, 1255px" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://cdry.wordpress.com/2018/05/07/self-similarity-image-scaler/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1164</post-id>
		<media:content url="https://2.gravatar.com/avatar/571c802b5077513106d8a8eb429d60da2ef7d8027fc6b90454300a8da0d37a5e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mgeorgoulopoulos</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2018/05/selfsim.png" medium="image">
			<media:title type="html">selfsim.PNG</media:title>
		</media:content>
	</item>
		<item>
		<title>BFP Chapter 04: Room for one texture</title>
		<link>https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/</link>
					<comments>https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/#comments</comments>
		
		<dc:creator><![CDATA[mgeorgoulopoulos]]></dc:creator>
		<pubDate>Sun, 17 Sep 2017 18:26:33 +0000</pubDate>
				<category><![CDATA[BFP Series]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[backgrounds]]></category>
		<category><![CDATA[cells]]></category>
		<category><![CDATA[genesis]]></category>
		<category><![CDATA[M68K]]></category>
		<category><![CDATA[mega drive]]></category>
		<category><![CDATA[sega]]></category>
		<category><![CDATA[sprites]]></category>
		<category><![CDATA[tiles]]></category>
		<guid isPermaLink="false">http://cdry.wordpress.com/?p=1065</guid>

					<description><![CDATA[Download sources for this chapter Introduction I&#8217;m starting to get some mixed feelings about this project. First of all, I find it unacceptable to have spent so much time for a blank screen. On the other hand, I know I&#8217;m so close to displaying something that I simply must do it today. And then, I [&#8230;]]]></description>
										<content:encoded><![CDATA[<p style="text-align:justify;"><a href="http://mgeo.psyflex.com/BlastFromThePast/CH04.zip" target="_blank" rel="noopener">Download sources for this chapter</a></p>
<h1 style="text-align:justify;">Introduction</h1>
<p style="text-align:justify;">I&#8217;m starting to get some mixed feelings about this project.</p>
<p style="text-align:justify;">First of all, I find it unacceptable to have spent so much time for a blank screen.</p>
<p style="text-align:justify;">On the other hand, I know I&#8217;m so close to displaying something that I simply must do it today.</p>
<p style="text-align:justify;">And then, I see thi<span id="more-1065"></span>s:</p>
<p><iframe class="youtube-player" width="670" height="377" src="https://www.youtube.com/embed/iQqJm14sHRY?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en&#038;autohide=2&#038;wmode=transparent" allowfullscreen="true" style="border:0;" sandbox="allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox"></iframe></p>
<p style="text-align:justify;">How on earth did they do it? I have my theories about most of the effects, but what about the giant rotating sprite?</p>
<p style="text-align:justify;">I ran it on the emulator (they provide separate binaries for each emulator cause they also have found bugs in them). I opened the VDP debugger to see how they organize their sprites. In the most interesting parts I see no sprites at all. Maybe they swap them in and out quickly so they don&#8217;t appear in the debugger.</p>
<p style="text-align:justify;">But enough thinking about the complex stuff. Back to today. Let&#8217;s put something on the screen!</p>
<h1 style="text-align:justify;">A world made of tiles</h1>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1071" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/tiles/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/tiles.png" data-orig-size="1280,830" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="tiles" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/tiles.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/tiles.png?w=670" class="  wp-image-1071 alignleft" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/tiles.png?w=183&#038;h=119" alt="tiles.png" width="183" height="119" srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/tiles.png?w=183&amp;h=119 183w, https://cdry.wordpress.com/wp-content/uploads/2017/09/tiles.png?w=366&amp;h=237 366w, https://cdry.wordpress.com/wp-content/uploads/2017/09/tiles.png?w=150&amp;h=97 150w, https://cdry.wordpress.com/wp-content/uploads/2017/09/tiles.png?w=300&amp;h=195 300w" sizes="(max-width: 183px) 100vw, 183px" /></p>
<p style="text-align:justify;">All graphics entities of the VDP (sprites and backgrounds) are much like textures. They have a programmable size, in powers of two cells.</p>
<p style="text-align:justify;">A &#8220;cell&#8221; is a 8&#215;8 pixel region where each pixel is 4 bits &#8211; a palette entry. Each cell must choose one of the 4 palettes, so we are restricted to 16 colors per cell. Or 15 colors if you don&#8217;t count the transparency (color 0 of every palette is transparent).</p>
<p style="text-align:justify;">VRAM contains cell information. You can organize the VRAM however you like. For every sprite and background, you write all the cells that compose it in VRAM one after another.</p>
<p style="text-align:justify;">Then you assign the graphical entity its starting VRAM address. The entity holds the information for its size (you have specific sizes to choose from for backgrounds and sprites), which implies how many cells it will occupy in VRAM.</p>
<h1 style="text-align:justify;">The sprite table</h1>
<p style="text-align:justify;">VRAM also holds the &#8220;sprite table&#8221;. This table has a fixed size (64 sprite records if I remember correctly) and it is actually a linked list. It starts with sprite 0, which is a record that contains sprite size, cell data offset in VRAM and a &#8216;link&#8217; field that will enable the next sprite. I&#8217;m guessing they did this so that the programmer could insert and delete sprites fast.</p>
<p style="text-align:justify;">You can put the sprite table wherever you like in VRAM and you tell the VDP where it is by setting register #5.</p>
<h1 style="text-align:justify;">A first approach</h1>
<p style="text-align:justify;">The first thing we could try here is to somehow create a file with our intended VRAM contents, embed that file in the ROM and then copy from ROM to VRAM using a DMA transfer.</p>
<p style="text-align:justify;">So, first of all, we need some 16-color paletted images. BMP files have a 4bpp mode so let&#8217;s try to make such a file with GIMP. In order to fill the screen, we need a 32&#215;32 cell background, or a 256&#215;256 bitmap. I like this one:<img loading="lazy" data-attachment-id="1079" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/test/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/test.png" data-orig-size="256,256" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="test" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/test.png?w=256" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/test.png?w=256" class=" size-full wp-image-1079 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/test.png?w=670" alt="test.png"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/test.png 256w, https://cdry.wordpress.com/wp-content/uploads/2017/09/test.png?w=150&amp;h=150 150w" sizes="(max-width: 256px) 100vw, 256px" /></p>
<p style="text-align:justify;">In GIMP, I changed the mode to indexed, using an optimized palette of 15 colors. Then I added one more color for transparency (purple is commonly used for masks), which is not used in the image, and I put it at index 0, so the useful colors will be from 1 on. Manipulating the palette in GIMP is not that easy. Maybe there are better tools for the job, but right now, we have something to work with.</p>
<p style="text-align:justify;">I exported the image as a 4bpp bmp. I then wrote a tool in C (bmp2cell) that reads the file and spits out a .cell file containing what I think is the correct layout of bytes to fill the VRAM with.</p>
<p style="text-align:justify;">I then embedded the .cell file in my assemby source, using the &#8220;binary&#8221; directive. There&#8217;s also the &#8220;even&#8221; directive that will make sure what follows is at an even address, so as to avoid odd addressing issues:</p>
<pre>	even
VRAMData:
	binary "test.cell"
</pre>
<h1 style="text-align:justify;">DMA transfer</h1>
<p style="text-align:justify;">And now, to copy the data to VRAM. Here&#8217;s how you do a DMA memory to VRAM transfer, according to the manual:</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1086" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/dmatransfer/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/dmatransfer.png" data-orig-size="1515,477" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="DMATransfer" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/dmatransfer.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/dmatransfer.png?w=670" class=" size-full wp-image-1086 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/dmatransfer.png?w=670" alt="DMATransfer.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/dmatransfer.png 1515w, https://cdry.wordpress.com/wp-content/uploads/2017/09/dmatransfer.png?w=150&amp;h=47 150w, https://cdry.wordpress.com/wp-content/uploads/2017/09/dmatransfer.png?w=300&amp;h=94 300w, https://cdry.wordpress.com/wp-content/uploads/2017/09/dmatransfer.png?w=768&amp;h=242 768w, https://cdry.wordpress.com/wp-content/uploads/2017/09/dmatransfer.png?w=1024&amp;h=322 1024w, https://cdry.wordpress.com/wp-content/uploads/2017/09/dmatransfer.png?w=1440&amp;h=453 1440w" sizes="(max-width: 1515px) 100vw, 1515px" /></p>
<p style="text-align:justify;">An image is missing (probably register #22), but I have all the information I need from the register descriptions above. It took some experiments, but I think this is it:</p>
<pre>	
	VDPSetRegister  1, $5C		; Turn on "DMA" bit (M1)
	VDPSetRegister 15, $02		; R#15 : Auto-increment by 2 bytes
	VDPSetRegister 20, $40		; Copy length: 3FFF will copy 4000 words - NOT!
	VDPSetRegister 19, $00
	
	; bits 8-1 of source address (bit 0 ignored)
	VDPSetRegister 21, ((VRAMData &gt;&gt; 1) &amp; $FF)
	VDPSetRegister 22, ((VRAMData &gt;&gt; 9) &amp; $FF)	; bits 16-9
	; Rest of the bits (MSB must be 0 for memory-&gt;VRAM transfer)
	VDPSetRegister 23, ((VRAMData &gt;&gt; 17) &amp; $7F)
	; 2 Writes to control, with splattered destination address
	defc destinationAddress = 0
	VDPWriteToControl #((((destinationAddress &gt;&gt; 8) &amp; $3F) | $40) &lt;&lt; 8) 	VDPWriteToControl #(((destinationAddress &gt;&gt; 14) &amp; 3) | $80)
</pre>
<p style="text-align:justify;">There are a million things to worry about here. First of all, after the <img loading="lazy" data-attachment-id="1090" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/epictetus/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/epictetus.jpg" data-orig-size="590,1000" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Picasa 2.7&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Epictetus" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/epictetus.jpg?w=177" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/epictetus.jpg?w=590" class="  wp-image-1090 alignright" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/epictetus.jpg?w=99&#038;h=168" alt="Epictetus.jpg" width="99" height="168" srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/epictetus.jpg?w=99&amp;h=168 99w, https://cdry.wordpress.com/wp-content/uploads/2017/09/epictetus.jpg?w=198&amp;h=336 198w, https://cdry.wordpress.com/wp-content/uploads/2017/09/epictetus.jpg?w=89&amp;h=150 89w, https://cdry.wordpress.com/wp-content/uploads/2017/09/epictetus.jpg?w=177&amp;h=300 177w" sizes="(max-width: 99px) 100vw, 99px" />transfer is complete, the &#8220;DMA&#8221; bit of register #1 is not automatically turned off as promised. The transfer did not work like in VRAM fill, where we put one increment less than we need. Now it seems to just copy the exact amount of words we specify. Finally, the word&#8217;s byte order is reversed (again). This might be normal.</p>
<p style="text-align:justify;">I stoically reversed the byte order in my C program.</p>
<p style="text-align:justify;">Also, before exporting the palette from the .bmp, I just wanted to test the DMA transfer, so I just made a grayscale palette by repeating the same set of instructions 16 times:</p>
<pre>	VDPWriteToControl #$C000	; Give the command to write to palette entry 0
	VDPWriteToControl #$0000
	VDPWriteToData #$0000		; Write color to the data port
</pre>
<p style="text-align:justify;">I really expected to see something recognizable on screen after running this. We had everything initialized to zero, so the backgrounds must already point to VRAM location 0. Here&#8217;s what I saw:</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1096" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/giberrish/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/giberrish.png" data-orig-size="668,536" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="giberrish" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/giberrish.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/giberrish.png?w=668" class="aligncenter size-full wp-image-1096" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/giberrish.png?w=670" alt="giberrish.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/giberrish.png 668w, https://cdry.wordpress.com/wp-content/uploads/2017/09/giberrish.png?w=150&amp;h=120 150w, https://cdry.wordpress.com/wp-content/uploads/2017/09/giberrish.png?w=300&amp;h=241 300w" sizes="(max-width: 668px) 100vw, 668px" /></p>
<p style="text-align:justify;">Also, there was a cool flickering effect, I&#8217;m guessing that some frames pass while I change the palette.</p>
<h1 style="text-align:justify;">Moving the sprite table out of the way</h1>
<p style="text-align:justify;">I think what we&#8217;re seeing here is the sprites. We set everything to zero indeed, so the sprite table must also be at VRAM 0, where we also put our bitmap data. So the sprites are just garbage. Let&#8217;s try putting the sprite table elsewhere. The best place I can think of right now is the bottom of VRAM.</p>
<p style="text-align:justify;">The sprite table seems to occupy 512 bytes. So the sprite record must be 8 bytes, for 64 sprites. We must set the 7 most significant bits of the sprite table address to register #5, like this:</p>
<pre>	0XXXXXXX (X = A15-A9)
</pre>
<p style="text-align:justify;">So, to put the table at the bottom, we need to:</p>
<pre>	VDPSetRegister 5, $7F</pre>
<p style="text-align:justify;">Let&#8217;s put that in our initialization routine.</p>
<p style="text-align:justify;">Nothing happens, except the sprites have been cleared. Not all sprites. The emulator shows more than 64 sprite records. The manual mentions more sprites in case we use the &#8220;40 cell mode&#8221;, which I don&#8217;t remember what is at the moment. So let&#8217;s try to clear bit 9 of the sprite table address, effectively putting the table a little above the bottom so that more sprites will be cleared.</p>
<p style="text-align:justify;">Again, no visible changes, but the sprite table has been fully cleared. I shouldn&#8217;t have expected more though. Clearing everything to zero will not set up the backgrounds to our liking.</p>
<p style="text-align:justify;">So let&#8217;s keep on reading the damn manual&#8230;</p>
<h1 style="text-align:justify;">Cell indices</h1>
<p style="text-align:justify;">It should have dawned on me sooner. We&#8217;ve already filled half our VRAM with the contents of just one background. There&#8217;s no room left for the second background, let alone sprites. So we can&#8217;t possibly define a background by laying out one cell after the other in VRAM. There&#8217;s simply not enough space.</p>
<p style="text-align:justify;">The cells that we write in VRAM are reusable and you create backgrounds by laying out indices to cells.</p>
<p style="text-align:justify;">To complicate things a tiny bit more, the indices are packed with other kinds of stuff, like priority, flipping and color palette selection. So we are constrained to 16 colors per referenced cell (and not per background) after all:</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1107" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/cellindexing/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/cellindexing.png" data-orig-size="895,393" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="cellIndexing" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/cellindexing.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/cellindexing.png?w=670" class="alignnone size-full wp-image-1107" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/cellindexing.png?w=670" alt="cellIndexing.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/cellindexing.png 895w, https://cdry.wordpress.com/wp-content/uploads/2017/09/cellindexing.png?w=150&amp;h=66 150w, https://cdry.wordpress.com/wp-content/uploads/2017/09/cellindexing.png?w=300&amp;h=132 300w, https://cdry.wordpress.com/wp-content/uploads/2017/09/cellindexing.png?w=768&amp;h=337 768w" sizes="(max-width: 895px) 100vw, 895px" /></p>
<p style="text-align:justify;">So we must somehow reuse our cells. Too bad I&#8217;ve spent time on this tool that exports unique cells from a bmp. We&#8217;re going to have to search for another tool for graphics, but right now I will display what I have.</p>
<p style="text-align:justify;">The situation is like this:</p>
<ul style="text-align:justify;">
<li>We have occupied the top half of our VRAM with our wonderful texture.</li>
<li>Therefore we need to fill backgrounds A and B with indices to those cells, in order from 0 to 1023 (32&#215;32 cells for each background)</li>
<li>So every background occupies 2 KB in VRAM</li>
</ul>
<p style="text-align:justify;">The quickest thing I can think of right now is to have my bmp2cell tool output this index buffer along with the cell data and then point both backgrounds to the start of the index buffer. That should do the trick.</p>
<pre>	/* This tool is going to die soon - add some dumb cell indices for my viewing pleasure */
	for (i = 0; i&lt;1024; i++) {
 		dst[0] = i &amp; 0xFF;
 		dst[1] = (i &gt;&gt; 8) &amp; 7;
		dst += 2;
	}
</pre>
<p style="text-align:justify;">Note: We point the backgrounds to look for indices in the upper half of the VRAM by setting VDP registers #2, #3 and #4<img loading="lazy" data-attachment-id="1114" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/progress/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/progress.png" data-orig-size="564,423" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="progress" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/progress.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/progress.png?w=564" class=" size-full wp-image-1114 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/progress.png?w=670" alt="progress.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/progress.png 564w, https://cdry.wordpress.com/wp-content/uploads/2017/09/progress.png?w=150&amp;h=113 150w, https://cdry.wordpress.com/wp-content/uploads/2017/09/progress.png?w=300&amp;h=225 300w" sizes="(max-width: 564px) 100vw, 564px" /></p>
<p style="text-align:justify;">Now, can I find it in my heart to call this &#8220;progress&#8221;? I wish I could&#8230; Putting aside the urge to give up, let&#8217;s mess around with stuff a little bit more&#8230;</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1118" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/morelikeit/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/morelikeit.png" data-orig-size="639,527" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="moreLikeIt" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/morelikeit.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/morelikeit.png?w=639" class=" size-full wp-image-1118 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/morelikeit.png?w=670" alt="moreLikeIt"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/morelikeit.png 639w, https://cdry.wordpress.com/wp-content/uploads/2017/09/morelikeit.png?w=150&amp;h=124 150w, https://cdry.wordpress.com/wp-content/uploads/2017/09/morelikeit.png?w=300&amp;h=247 300w" sizes="(max-width: 639px) 100vw, 639px" /></p>
<p style="text-align:justify;">I seriously have no clue how I reached to that point. I kept flipping things around, but I&#8217;m almost certain, the current situation is exactly the same as before. Spooky.</p>
<p style="text-align:justify;">Now, the only thing remaining is to flip high and low bytes in the image data and we&#8217;re done. Maybe I was being stoic for no good reason earlier:</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1122" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/finally/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/finally.png" data-orig-size="657,526" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Finally" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/finally.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/finally.png?w=657" class="aligncenter size-full wp-image-1122" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/finally.png?w=670" alt="Finally.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/finally.png 657w, https://cdry.wordpress.com/wp-content/uploads/2017/09/finally.png?w=150&amp;h=120 150w, https://cdry.wordpress.com/wp-content/uploads/2017/09/finally.png?w=300&amp;h=240 300w" sizes="(max-width: 657px) 100vw, 657px" /></p>
<p style="text-align:justify;">From heaven to hell and back!</p>
<p style="text-align:justify;">Also, we get to discover that the scrolling backgrounds wrap around. And what a happy coincidence that I chose a tilable texture for my first test!</p>
<h1 style="text-align:justify;">The ROM header</h1>
<p style="text-align:justify;">Now that I&#8217;m satisfied to see my pixels on-screen, I think it&#8217;s time to revisit stuff I&#8217;ve been neglecting. And first of all, let&#8217;s make a proper header instead of copying Sonic&#8217;s:</p>
<p style="text-align:justify;"><a href="http://www.zophar.net/fileuploads/2/10614uauyw/Genesis_ROM_Format.txt" rel="nofollow">http://www.zophar.net/fileuploads/2/10614uauyw/Genesis_ROM_Format.txt</a></p>
<p style="text-align:justify;">Here&#8217;s a visual representation of the header:</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1128" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/romheader/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/romheader.png" data-orig-size="1138,276" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="ROMHeader" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/romheader.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/romheader.png?w=670" class="alignnone size-full wp-image-1128" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/romheader.png?w=670" alt="ROMHeader.png"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/romheader.png 1138w, https://cdry.wordpress.com/wp-content/uploads/2017/09/romheader.png?w=150&amp;h=36 150w, https://cdry.wordpress.com/wp-content/uploads/2017/09/romheader.png?w=300&amp;h=73 300w, https://cdry.wordpress.com/wp-content/uploads/2017/09/romheader.png?w=768&amp;h=186 768w, https://cdry.wordpress.com/wp-content/uploads/2017/09/romheader.png?w=1024&amp;h=248 1024w" sizes="(max-width: 1138px) 100vw, 1138px" /></p>
<p style="text-align:justify;">I&#8217;ll assume the top half part of the ROM header is given to the programmer to use it however he likes. For instance, check out the funny header TITAN put in their &#8220;Overdrive&#8221; demo.<br />
Then there&#8217;s a series of strings (the green region) that are more or less self-explanatory.</p>
<p style="text-align:justify;">Then we have the product type. We&#8217;ll use GM for &#8220;game&#8221;.</p>
<p style="text-align:justify;">Then follows product code and version number. I&#8217;ll set them to ascii &#8216;0&#8217;.</p>
<p style="text-align:justify;">After that, we have the checksum we already calculated in the romtool, in yellow.</p>
<p style="text-align:justify;">Then we have a large string that will start with &#8216;J&#8217;, for &#8220;joypad support&#8221;, and the rest of it will be ascii spaces as we&#8217;re not going to use other peripherals.</p>
<p style="text-align:justify;">Then, there&#8217;s the ROM capacity we already handle, in green (start address &#8211; always zero) and red (end address).</p>
<p style="text-align:justify;">For the following two long-words, I&#8217;ll just copy the values from sonic. I don&#8217;t know what they do.</p>
<p style="text-align:justify;">From then on, we have a large string of ascii spaces, with &#8216;JUE&#8217; near the end, which means the game can be played in Japan, USA, Europe. I&#8217;m going to go with that as well.</p>
<p style="text-align:justify;">So, let&#8217;s create a separate assembly file, where we&#8217;ll put the header data. How do we put ascii and binary data in vasm?</p>
<p style="text-align:justify;">We do both with the same directive: &#8220;byte&#8221;. It takes any number of arguments that can be either integers or strings.</p>
<p style="text-align:justify;">So let&#8217;s do that:</p>
<pre>	; Think of something clever to say here
	ORG $0
	byte "IGNORED IGNORED IGNORED IGNORED "
	byte "IGNORED IGNORED IGNORED IGNORED "
	byte "IGNORED IGNORED IGNORED IGNORED "
	byte "IGNORED IGNORED IGNORED IGNORED "
	byte "IGNORED IGNORED IGNORED IGNORED "
	byte "IGNORED IGNORED IGNORED IGNORED "
	byte "IGNORED IGNORED IGNORED IGNORED "
	byte "IGNORED IGNORED IGNORED IGNORED "
	
	ORG $100
	byte "SEGA MEGA DRIVE "
	
	ORG $110
	byte "PSYFLEX 2016.JUL"
	
	ORG $120
	byte "GAME NAME                             (DOMESTIC)"
	
	ORG $150
	byte "GAME NAME                             (OVERSEAS)"

	; "GM" for "game", "AI" for "educational"
	ORG $180
	byte "GM"
	
	; Product ID and version
	ORG $182
	byte " 0000000-00"
	
	; Checksum
	ORG $18E
	byte 0, 0
	
	; I/O Support - 'J' = joypad
	ORG $190
	byte "J               "	
	
	; ROM capacity - this is handled by the romtool
	ORG $1A0
	byte $00, $00, $00, $00, $FF, $FF, $FF, $FF
	
	; RAM - No idea what this is - I'm putting the values from sonic
	ORG $1A8
	byte $00, $FF, $00, $00, $00, $FF, $FF, $FF
	
	; MODEM, MEMO and Country - Mostly spaces
	byte "                                "
	byte "                                "
	byte "JUE             "
</pre>
<p style="text-align:justify;">I&#8217;m including the file ROMHeader.x68 on top of my main program.</p>
<p style="text-align:justify;">Now let&#8217;s see how we tell vasm to produce a flat binary. You do that with the -Fbin argument.</p>
<p style="text-align:justify;">Let&#8217;s try to create a binary first.</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1133" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/headerlooksok/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/headerlooksok.png" data-orig-size="1155,391" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="headerLooksOk" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/headerlooksok.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/headerlooksok.png?w=670" class=" size-full wp-image-1133 alignright" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/headerlooksok.png?w=670" alt="headerLooksOk.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/headerlooksok.png 1155w, https://cdry.wordpress.com/wp-content/uploads/2017/09/headerlooksok.png?w=150&amp;h=51 150w, https://cdry.wordpress.com/wp-content/uploads/2017/09/headerlooksok.png?w=300&amp;h=102 300w, https://cdry.wordpress.com/wp-content/uploads/2017/09/headerlooksok.png?w=768&amp;h=260 768w, https://cdry.wordpress.com/wp-content/uploads/2017/09/headerlooksok.png?w=1024&amp;h=347 1024w" sizes="(max-width: 1155px) 100vw, 1155px" /></p>
<p style="text-align:justify;">The header looks ok, but regen shuts down (crashes?) when I load the binary. Maybe it&#8217;s the broken checksum/rom capacity. Let&#8217;s fix that with a new tool. I&#8217;m going to call this romtool2 and put it in its own folder, because I want to leave romtool intact so that previous chapter setups will work too.</p>
<p style="text-align:justify;">I&#8217;m considering creating a git repository for the whole project, but it&#8217;s going to be troublesome when (if) I post the series online. I will then have to go to specific revisions to grab the version of the tool that will work with a specific day&#8217;s test.</p>
<p style="text-align:justify;">Wait a minute&#8230; That&#8217;s not troublesome at all!</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1136" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/gitinit/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/gitinit.png" data-orig-size="586,260" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="gitInit" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/gitinit.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/gitinit.png?w=586" class=" size-full wp-image-1136 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/gitinit.png?w=670" alt="gitInit.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/gitinit.png 586w, https://cdry.wordpress.com/wp-content/uploads/2017/09/gitinit.png?w=150&amp;h=67 150w, https://cdry.wordpress.com/wp-content/uploads/2017/09/gitinit.png?w=300&amp;h=133 300w" sizes="(max-width: 586px) 100vw, 586px" /></p>
<p style="text-align:justify;">So, now that we have a git repository, we can just modify the romtool to only calculate the checksum and rom capacity.</p>
<p style="text-align:justify;">And so we did. But regen still crashes.</p>
<p style="text-align:justify;">I&#8217;m starting to believe that the first 256 bytes contain something important. Not so funny after all. Also I think I remember the first bytes in the address space are used for interrupt vectors. Yup, they are.</p>
<h1 style="text-align:justify;">Interrupts</h1>
<p style="text-align:justify;">And now it&#8217;s time for our first cheat. I&#8217;m going to copy <a href="http://darkdust.net/writings/megadrive/initializing" target="_blank" rel="noopener">this guy&#8217;s code</a> for the first 256 bytes:</p>
<p style="text-align:justify;">Credit for this goes to <a href="http://darkdust.net/index.php/" target="_blank" rel="noopener">Marc Haisenko</a>. There are other people involved in figuring this out, whose names are listed in the link above. A recursive &#8220;thank you&#8221; to you all!</p>
<p style="text-align:justify;">I&#8217;m adding a new file called &#8220;interrupts.x68&#8221;. Our handlers for now will be empty subroutines.</p>
<p style="text-align:justify;">This also takes care of another item in our TO-DO list: &#8220;Assumption that nop-nop-bra is mandatory&#8221;. It isn&#8217;t. I know now because the second long word of the ROM is the code starting address. Sonic actually starts at $206, which means we can now safely remove the nop-nop-bra nonsense.</p>
<p style="text-align:justify;">Unfortunately, vasm does not seem to have a &#8220;long&#8221; directive, so our version of the first 256 bytes is going to be as ugly as:</p>
<pre>	word   ((genericInterrupt &gt;&gt; 16) &amp; $FFFF), (genericInterrupt &amp; $FFFF)		; Bus error
</pre>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1148" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/4leafclover/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/4leafclover.png" data-orig-size="640,625" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="4leafClover" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/4leafclover.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/4leafclover.png?w=640" class="  wp-image-1148 alignleft" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/4leafclover.png?w=86&#038;h=83" alt="4leafClover.png" width="86" height="83" srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/4leafclover.png?w=86&amp;h=84 86w, https://cdry.wordpress.com/wp-content/uploads/2017/09/4leafclover.png?w=172&amp;h=168 172w, https://cdry.wordpress.com/wp-content/uploads/2017/09/4leafclover.png?w=150&amp;h=146 150w" sizes="(max-width: 86px) 100vw, 86px" />And now, let us pause for a moment to reflect upon our monumental luck, that the random addresses we copied from sonic&#8217;s ROM just HAPPENED to be somewhere safe.<br />
Unbelievable. It&#8217;s a good thing we put that out of the way.</p>
<h1 style="text-align:justify;">Trying other emulators</h1>
<p style="text-align:justify;">Next item in our list is to make sure we haven&#8217;t based our code on false assumptions, based on our reference &#8220;hardware&#8221;, which is actually an emulator. I tried the binary in 3 emulators just in case:</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="1152" data-permalink="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/variousemulators/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/variousemulators.png" data-orig-size="1889,517" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="variousEmulators" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/variousemulators.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/variousemulators.png?w=670" class="aligncenter size-full wp-image-1152" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/variousemulators.png?w=670" alt="variousEmulators.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/variousemulators.png 1889w, https://cdry.wordpress.com/wp-content/uploads/2017/09/variousemulators.png?w=150&amp;h=41 150w, https://cdry.wordpress.com/wp-content/uploads/2017/09/variousemulators.png?w=300&amp;h=82 300w, https://cdry.wordpress.com/wp-content/uploads/2017/09/variousemulators.png?w=768&amp;h=210 768w, https://cdry.wordpress.com/wp-content/uploads/2017/09/variousemulators.png?w=1024&amp;h=280 1024w, https://cdry.wordpress.com/wp-content/uploads/2017/09/variousemulators.png?w=1440&amp;h=394 1440w" sizes="(max-width: 1889px) 100vw, 1889px" /></p>
<h1 style="text-align:justify;">Afterword</h1>
<p style="text-align:justify;">It might not be much, but this little demo brings us closer to our goal&#8230; which is kind of undefined&#8230; but definitely closer! Stay tuned as we are now almost ready to do some basic animation!</p>
<p>&nbsp;</p>
<table>
<tbody>
<tr>
<td><a href="https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/">Previous Chapter</a></td>
<td style="text-align:center;"><a href="https://cdry.wordpress.com/2017/01/08/a-blast-from-the-past-toc/">TOC</a></td>
<td style="text-align:right;">Next Chapter</td>
</tr>
</tbody>
</table>
]]></content:encoded>
					
					<wfw:commentRss>https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1065</post-id>
		<media:content url="https://2.gravatar.com/avatar/571c802b5077513106d8a8eb429d60da2ef7d8027fc6b90454300a8da0d37a5e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mgeorgoulopoulos</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/tiles.png" medium="image">
			<media:title type="html">tiles.png</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/test.png" medium="image">
			<media:title type="html">test.png</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/dmatransfer.png" medium="image">
			<media:title type="html">DMATransfer.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/epictetus.jpg" medium="image">
			<media:title type="html">Epictetus.jpg</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/giberrish.png" medium="image">
			<media:title type="html">giberrish.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/cellindexing.png" medium="image">
			<media:title type="html">cellIndexing.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/progress.png" medium="image">
			<media:title type="html">progress.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/morelikeit.png" medium="image">
			<media:title type="html">moreLikeIt</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/finally.png" medium="image">
			<media:title type="html">Finally.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/romheader.png" medium="image">
			<media:title type="html">ROMHeader.png</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/headerlooksok.png" medium="image">
			<media:title type="html">headerLooksOk.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/gitinit.png" medium="image">
			<media:title type="html">gitInit.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/4leafclover.png" medium="image">
			<media:title type="html">4leafClover.png</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/variousemulators.png" medium="image">
			<media:title type="html">variousEmulators.PNG</media:title>
		</media:content>
	</item>
		<item>
		<title>Around the World in 8.5 megabytes</title>
		<link>https://cdry.wordpress.com/2017/09/12/around-the-world-in-8-5-megabytes/</link>
					<comments>https://cdry.wordpress.com/2017/09/12/around-the-world-in-8-5-megabytes/#respond</comments>
		
		<dc:creator><![CDATA[mgeorgoulopoulos]]></dc:creator>
		<pubDate>Tue, 12 Sep 2017 13:06:04 +0000</pubDate>
				<category><![CDATA[Qurashee]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[propagation]]></category>
		<category><![CDATA[signal]]></category>
		<category><![CDATA[speed of light]]></category>
		<guid isPermaLink="false">http://cdry.wordpress.com/?p=1025</guid>

					<description><![CDATA[It all started with a friend of mine bragging about his internet connection. Apparently he runs at 360 Mbps, which means that one bit is being transmitted for the duration of 1/360th of a microsecond. On the other hand, light travels in fibre optics at 200 m / microsecond. That&#8217;s all we need to know [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>It all started with a friend of mine bragging about his internet connection.<img loading="lazy" data-attachment-id="1049" data-permalink="https://cdry.wordpress.com/2017/09/12/around-the-world-in-8-5-megabytes/browser-98386_640/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/browser-98386_640.png" data-orig-size="631,640" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="browser-98386_640" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/browser-98386_640.png?w=296" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/browser-98386_640.png?w=631" class="  wp-image-1049 alignright" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/browser-98386_640.png?w=162&#038;h=164" alt="browser-98386_640" width="162" height="164" srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/browser-98386_640.png?w=162&amp;h=164 162w, https://cdry.wordpress.com/wp-content/uploads/2017/09/browser-98386_640.png?w=324&amp;h=329 324w, https://cdry.wordpress.com/wp-content/uploads/2017/09/browser-98386_640.png?w=148&amp;h=150 148w, https://cdry.wordpress.com/wp-content/uploads/2017/09/browser-98386_640.png?w=296&amp;h=300 296w" sizes="(max-width: 162px) 100vw, 162px" /></p>
<p>Apparently he runs at 360 Mbps, which means that one bit is being transmitted for the duration of 1/360th of a microsecond.</p>
<p>On the other hand, light travels in fibre optics at 200 m / microsecond.<span id="more-1025"></span></p>
<p>That&#8217;s all we need to know to calculate the length of a bit, ie: how much of the cable a single bit occupies if we freeze time and examine the beginning and end of the signal that represents the bit. Its width is propagation velocity times signal duration.</p>
<p>So it is 200/360 = 0.56 m. A bit is about as wide as one of the chairs below:</p>
<p><img loading="lazy" data-attachment-id="1033" data-permalink="https://cdry.wordpress.com/2017/09/12/around-the-world-in-8-5-megabytes/chairs/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/chairs.jpg" data-orig-size="540,360" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="chairs" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/chairs.jpg?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/chairs.jpg?w=540" class=" size-full wp-image-1033 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/chairs.jpg?w=670" alt="chairs.jpg"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/chairs.jpg 540w, https://cdry.wordpress.com/wp-content/uploads/2017/09/chairs.jpg?w=150&amp;h=100 150w, https://cdry.wordpress.com/wp-content/uploads/2017/09/chairs.jpg?w=300&amp;h=200 300w" sizes="(max-width: 540px) 100vw, 540px" /></p>
<p>This is far from what I&#8217;ve expected. I thought bits were tiny. A few mm at most. I was visualizing them as tiny little spots traveling through the wire extremely fast.</p>
<p>Now that I&#8217;ve done the math, I find it fascinating because I&#8217;m developing a new appreciation of the speed of light.</p>
<p>Everyone has some intuition about how fast a megabyte travels. Now imagine some 8 millions of the above chairs traveling from Brazil to Japan, as fast as a selfie does, and you get a taste of the speed of light. Well 2/3rds of it, but you get the idea&#8230;</p>
<p><img loading="lazy" data-attachment-id="1192" data-permalink="https://cdry.wordpress.com/2017/09/12/around-the-world-in-8-5-megabytes/madness/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/madness.png" data-orig-size="512,314" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="madness" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/madness.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/madness.png?w=512" class=" size-full wp-image-1192 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/madness.png?w=670" alt="madness"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/madness.png 512w, https://cdry.wordpress.com/wp-content/uploads/2017/09/madness.png?w=150&amp;h=92 150w, https://cdry.wordpress.com/wp-content/uploads/2017/09/madness.png?w=300&amp;h=184 300w" sizes="(max-width: 512px) 100vw, 512px" /></p>
<p>Now the only thing remaining is to somehow develop intuition about what a &#8220;million&#8221; means, which I&#8217;m not going to say I possess. Nevertheless, I&#8217;m still tempted to do some calculations:</p>
<table>
<tbody>
<tr>
<td> Olympic size swimming pool</td>
<td> 11 bytes</td>
</tr>
<tr>
<td> The Great Wall of China</td>
<td> 4.6 kilobytes</td>
</tr>
<tr>
<td>Circumference of the Earth</td>
<td>8.5 megabytes</td>
</tr>
<tr>
<td>Distance to the Sun</td>
<td>31 gigabytes</td>
</tr>
</tbody>
</table>
<p><img loading="lazy" data-attachment-id="1052" data-permalink="https://cdry.wordpress.com/2017/09/12/around-the-world-in-8-5-megabytes/chinese-wall-305132_640/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/chinese-wall-305132_640.png" data-orig-size="522,640" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="chinese-wall-305132_640" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/chinese-wall-305132_640.png?w=245" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/09/chinese-wall-305132_640.png?w=522" class="  wp-image-1052 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2017/09/chinese-wall-305132_640.png?w=182&#038;h=223" alt="chinese-wall-305132_640.png" width="182" height="223" srcset="https://cdry.wordpress.com/wp-content/uploads/2017/09/chinese-wall-305132_640.png?w=182&amp;h=223 182w, https://cdry.wordpress.com/wp-content/uploads/2017/09/chinese-wall-305132_640.png?w=364&amp;h=446 364w, https://cdry.wordpress.com/wp-content/uploads/2017/09/chinese-wall-305132_640.png?w=122&amp;h=150 122w, https://cdry.wordpress.com/wp-content/uploads/2017/09/chinese-wall-305132_640.png?w=245&amp;h=300 245w" sizes="(max-width: 182px) 100vw, 182px" /></p>
<p>&nbsp;</p>
<div class="itanywhere-activator" style="left:300px;top:344px;display:none;" title="Google Translator Anywhere"></div>
<div class="itanywhere-activator" style="left:209px;top:1138px;display:none;" title="Google Translator Anywhere"></div>
<div class="itanywhere-activator" style="left:236px;top:555px;display:none;" title="Google Translator Anywhere"></div>
]]></content:encoded>
					
					<wfw:commentRss>https://cdry.wordpress.com/2017/09/12/around-the-world-in-8-5-megabytes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1025</post-id>
		<media:content url="https://2.gravatar.com/avatar/571c802b5077513106d8a8eb429d60da2ef7d8027fc6b90454300a8da0d37a5e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mgeorgoulopoulos</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/browser-98386_640.png" medium="image">
			<media:title type="html">browser-98386_640</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/chairs.jpg" medium="image">
			<media:title type="html">chairs.jpg</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/madness.png" medium="image">
			<media:title type="html">madness</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/09/chinese-wall-305132_640.png" medium="image">
			<media:title type="html">chinese-wall-305132_640.png</media:title>
		</media:content>
	</item>
		<item>
		<title>Apocalypse Cow cover art</title>
		<link>https://cdry.wordpress.com/2017/06/01/apocalypse-cow-cover-art/</link>
					<comments>https://cdry.wordpress.com/2017/06/01/apocalypse-cow-cover-art/#respond</comments>
		
		<dc:creator><![CDATA[mgeorgoulopoulos]]></dc:creator>
		<pubDate>Thu, 01 Jun 2017 09:29:20 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Apocalypse Cow]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[Humour]]></category>
		<category><![CDATA[Monsters]]></category>
		<category><![CDATA[Platformer]]></category>
		<guid isPermaLink="false">http://cdry.wordpress.com/?p=1011</guid>

					<description><![CDATA[Here&#8217;s some cover art for what will probably be the funniest game you&#8217;ve ever played: Check out the Monsters website and facebook page for more news, release dates etc.]]></description>
										<content:encoded><![CDATA[<p>Here&#8217;s some cover art for what will probably be the funniest game you&#8217;ve ever played:</p>
<p><a href="https://www.facebook.com/monstershq/" target="_blank" rel="noopener noreferrer"><img loading="lazy" data-attachment-id="1014" data-permalink="https://cdry.wordpress.com/2017/06/01/apocalypse-cow-cover-art/18879927_1407711982655387_86237203548505584_o/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/06/18879927_1407711982655387_86237203548505584_o.jpg" data-orig-size="1600,1000" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="18879927_1407711982655387_86237203548505584_o" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/06/18879927_1407711982655387_86237203548505584_o.jpg?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/06/18879927_1407711982655387_86237203548505584_o.jpg?w=670" class="alignnone size-full wp-image-1014" src="https://cdry.wordpress.com/wp-content/uploads/2017/06/18879927_1407711982655387_86237203548505584_o.jpg?w=670" alt="18879927_1407711982655387_86237203548505584_o.jpg"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/06/18879927_1407711982655387_86237203548505584_o.jpg 1600w, https://cdry.wordpress.com/wp-content/uploads/2017/06/18879927_1407711982655387_86237203548505584_o.jpg?w=150&amp;h=94 150w, https://cdry.wordpress.com/wp-content/uploads/2017/06/18879927_1407711982655387_86237203548505584_o.jpg?w=300&amp;h=188 300w, https://cdry.wordpress.com/wp-content/uploads/2017/06/18879927_1407711982655387_86237203548505584_o.jpg?w=768&amp;h=480 768w, https://cdry.wordpress.com/wp-content/uploads/2017/06/18879927_1407711982655387_86237203548505584_o.jpg?w=1024&amp;h=640 1024w, https://cdry.wordpress.com/wp-content/uploads/2017/06/18879927_1407711982655387_86237203548505584_o.jpg?w=1440&amp;h=900 1440w" sizes="(max-width: 1600px) 100vw, 1600px" /></a></p>
<p>Check out the <a href="http://monstersarehere.com/" target="_blank" rel="noopener noreferrer">Monsters website</a> and <a href="https://www.facebook.com/monstershq/" target="_blank" rel="noopener noreferrer">facebook page</a> for more news, release dates etc.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://cdry.wordpress.com/2017/06/01/apocalypse-cow-cover-art/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1011</post-id>
		<media:content url="https://2.gravatar.com/avatar/571c802b5077513106d8a8eb429d60da2ef7d8027fc6b90454300a8da0d37a5e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mgeorgoulopoulos</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/06/18879927_1407711982655387_86237203548505584_o.jpg" medium="image">
			<media:title type="html">18879927_1407711982655387_86237203548505584_o.jpg</media:title>
		</media:content>
	</item>
		<item>
		<title>Unity Render Queues vs Sorting Layers</title>
		<link>https://cdry.wordpress.com/2017/04/28/unity-render-queues-vs-sorting-layers/</link>
					<comments>https://cdry.wordpress.com/2017/04/28/unity-render-queues-vs-sorting-layers/#respond</comments>
		
		<dc:creator><![CDATA[mgeorgoulopoulos]]></dc:creator>
		<pubDate>Fri, 28 Apr 2017 21:05:00 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[order]]></category>
		<category><![CDATA[render queues]]></category>
		<category><![CDATA[sorting layers]]></category>
		<category><![CDATA[unity]]></category>
		<guid isPermaLink="false">http://cdry.wordpress.com/?p=983</guid>

					<description><![CDATA[This one should have been a matter of a simple google search, but it isn&#8217;t. Also I can&#8217;t find something in the docs. The question is: what happens first? Render queue sorting or Sorting layer sorting? So, I put 3 sprites at the exact same z: A is in render queue 3000 B is 3100 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>This one should have been a matter of a simple google search, but it isn&#8217;t. Also I can&#8217;t find something in the docs.</p>
<p>The question is: what happens first? Render queue sorting or Sorting layer sorting?</p>
<p>So, I put 3 sprites at the exact same z:</p>
<ul>
<li>A is in render queue 3000</li>
<li>B is 3100</li>
<li>C is 3200</li>
</ul>
<p><img loading="lazy" data-attachment-id="995" data-permalink="https://cdry.wordpress.com/2017/04/28/unity-render-queues-vs-sorting-layers/1-2/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/04/1.png" data-orig-size="349,353" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="1" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/04/1.png?w=297" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/04/1.png?w=349" class="alignnone size-full wp-image-995" src="https://cdry.wordpress.com/wp-content/uploads/2017/04/1.png?w=670" alt="1"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/04/1.png 349w, https://cdry.wordpress.com/wp-content/uploads/2017/04/1.png?w=148&amp;h=150 148w, https://cdry.wordpress.com/wp-content/uploads/2017/04/1.png?w=297&amp;h=300 297w" sizes="(max-width: 349px) 100vw, 349px" /></p>
<p><span id="more-983"></span>So far, so good. Then, we move sprite &#8216;A&#8217; to &#8220;order in layer&#8221; = 1:</p>
<p><img loading="lazy" data-attachment-id="1000" data-permalink="https://cdry.wordpress.com/2017/04/28/unity-render-queues-vs-sorting-layers/2-2/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/04/2.png" data-orig-size="383,340" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="2" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/04/2.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/04/2.png?w=383" class="alignnone size-full wp-image-1000" src="https://cdry.wordpress.com/wp-content/uploads/2017/04/2.png?w=670" alt="2"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/04/2.png 383w, https://cdry.wordpress.com/wp-content/uploads/2017/04/2.png?w=150&amp;h=133 150w, https://cdry.wordpress.com/wp-content/uploads/2017/04/2.png?w=300&amp;h=266 300w" sizes="(max-width: 383px) 100vw, 383px" /></p>
<p>&#8216;A&#8217; goes on top, which means that sorting layer sorting takes place first, and then render queue sorting is performed among objects of the same sorting layer.</p>
<div class="itanywhere-activator" style="display:none;left:408px;top:1124px;" title="Google Translator Anywhere"></div>
]]></content:encoded>
					
					<wfw:commentRss>https://cdry.wordpress.com/2017/04/28/unity-render-queues-vs-sorting-layers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">983</post-id>
		<media:content url="https://2.gravatar.com/avatar/571c802b5077513106d8a8eb429d60da2ef7d8027fc6b90454300a8da0d37a5e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mgeorgoulopoulos</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/04/1.png" medium="image">
			<media:title type="html">1</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/04/2.png" medium="image">
			<media:title type="html">2</media:title>
		</media:content>
	</item>
		<item>
		<title>BFP Chapter 03: Taking out the trash</title>
		<link>https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/</link>
					<comments>https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/#respond</comments>
		
		<dc:creator><![CDATA[mgeorgoulopoulos]]></dc:creator>
		<pubDate>Sat, 21 Jan 2017 21:19:28 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">http://cdry.wordpress.com/?p=848</guid>

					<description><![CDATA[Download sources for this chapter Edit: The beginning of chapter 03 was written in past tense, because the corresponding notes were just summarizing how I found an assembler. There were lots of back and forth most of which I deemed too useless to take notes about. Introduction I had no idea that finding a decent [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a href="http://mgeo.psyflex.com/BlastFromThePast/CH03.zip">Download sources for this chapter</a></p>
<p><span id="more-848"></span></p>
<p style="text-align:justify;"><em>Edit: The beginning of chapter 03 was written in past tense, because the corresponding notes were just summarizing how I found an assembler. There were lots of back and forth most of which I deemed too useless to take notes about.</em></p>
<h1 style="text-align:justify;">Introduction</h1>
<p style="text-align:justify;">I had no idea that finding a decent 68k assembler for windows would have been that difficult (at least if you are not a cygwin fan). I encountered so many dead-ends that I seriously considered making my own.</p>
<p>Among the assemblers I found, the most promising was &#8220;vasm&#8221;, a multi-cpu, multi-syntax, multi-host, multi-output assembler that clearly stated it supported m68k and motorola S-REC files. The only problem being that despite its multi-host capabilities, it didn&#8217;t support windows.</p>
<p>As a last resort before writing my own assembler, I decided to try and compile it myself. After all it was written in C, so there was a chance it would compile on windows.</p>
<h1 style="text-align:justify;">Migrating to vasm</h1>
<p>So, I used <a href="http://www.mingw.org/wiki/getting_started" target="_blank" rel="noopener">mingw/msys</a> and tried to compile with the following options:</p>
<pre>make CPU=m68k SYNTAX=mot</pre>
<p>This failed because msys has no &#8216;CC&#8217; variable defined, so I corrected this as follows:</p>
<pre>make CPU=m68k SYNTAX=mot CC=gcc</pre>
<p style="text-align:justify;">Which worked like a charm.</p>
<p>I now had a standalone assembler executable. But that didn&#8217;t assemble my test files. Maybe the &#8220;mot&#8221; (motorola) syntax was not the correct one. I&#8217;ve tried them all and finally the &#8220;oldstyle&#8221; seemed to somehow work:</p>
<pre style="text-align:justify;">make CPU=m68k SYNTAX=oldstyle CC=gcc</pre>
<p>That is, excluding the &#8216;*&#8217; comments which it didn&#8217;t recognize and the END directive that worked somehow differently (the END START line produced an error that said the &#8220;START&#8221; was garbage, so I removed it and it worked).</p>
<p>Now the only thing left was to make the assembler produce an S-REC file, instead of the &#8220;a.out&#8221; weird listing it produced by default. Here are the command line options to do that:</p>
<pre>vasmm68k_oldstyle.exe -Fsrec -s19 -o test.s68 test.x68</pre>
<p style="text-align:justify;">I compiled my previous blue-screen test, and a red-screen variant just for fun and they seem to work fine.</p>
<p>Now I&#8217;d like to find out how can we include files using this assembler. Taking a look at <a href="http://sun.hasenbraten.de/vasm/release/vasm.html" target="_blank" rel="noopener">the docs</a>.</p>
<p>It simply is:</p>
<pre style="text-align:justify;">    include file</pre>
<p style="text-align:justify;">Let&#8217;s try that. I&#8217;m going to make a separate assembly file that sets the background to green this time and include it right after setting to blue:</p>
<p><img loading="lazy" data-attachment-id="872" data-permalink="https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/included/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/included.png" data-orig-size="953,286" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="included" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/included.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/included.png?w=670" class="alignnone size-full wp-image-872" src="https://cdry.wordpress.com/wp-content/uploads/2017/01/included.png?w=670" alt="included.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/01/included.png 953w, https://cdry.wordpress.com/wp-content/uploads/2017/01/included.png?w=150&amp;h=45 150w, https://cdry.wordpress.com/wp-content/uploads/2017/01/included.png?w=300&amp;h=90 300w, https://cdry.wordpress.com/wp-content/uploads/2017/01/included.png?w=768&amp;h=230 768w" sizes="(max-width: 953px) 100vw, 953px" /></p>
<p>I needed to indent the &#8220;include&#8221; directive, otherwise it produced an error, and the filename must have been in quotes, but otherwise, it worked.</p>
<p>Also I found out something useful: if you just write to VDP&#8217;s data port a second color, it&#8217;s not going to replace the first one, but it will increment the target color by 1 (2 bytes) so you can write multiple entries in the palette with one command to the control port and multiple writes to the data port. Useful.</p>
<h1 style="text-align:justify;">Automatic build</h1>
<p style="text-align:justify;">Now, to somehow automate the building process. We now have a command line assembler, but it doesn&#8217;t help if we invoke it by hand. To summarize, the current situation looks like this:</p>
<ul>
<li>We have a .x68 file, containing our assembly code, plus as many include files as we want, in the same directory.</li>
<li>The assembler searches for include files in the current directory, so we must run it from there.</li>
<li>The assembler produces a .s68 S-REC file with our program, in machine code</li>
<li>Our &#8220;romtool&#8221; reads the .s68, plus a &#8220;header&#8221; binary located in its own directory (so it must be run from there) and produces the final ROM binary.</li>
<li>Regen (emulator) can load games from the command line!</li>
</ul>
<p style="text-align:justify;">The only problematic thing here is the &#8220;header&#8221; binary the romtool reads, which prevents it from working in any path other than its own. Maybe its time to ditch the external dependency. After all, it&#8217;s just 512 bytes, so why not embed them directly in the source?</p>
<p>I swear I&#8217;ve made this bin2c program a thousand times, but it&#8217;s nowhere to be found. I&#8217;m just going to modify the existing romtool to spit out the bytes in hex&#8230;</p>
<p>Done. We got rid of the dependency and romtool can now be called from anywhere. Now let&#8217;s create a bat file to automate the process&#8230;</p>
<p>I made a quick bat for now, that just calls the 3 programs (assembler, romtool, emulator) one after another. Maybe I&#8217;ll somehow make it stop if anything fails in this sequence, but for now, I&#8217;m just happy to be able to test my code instantly.</p>
<pre style="text-align:justify;">..\..\tools\vasm\vasmm68k_oldstyle.exe -Fsrec -s19 -o test.s68 test.x68
..\..\tools\romtool\romtool.exe test.s68 test.bin
..\..\tools\regen\regen.exe %~dp0test.bin</pre>
<p style="text-align:justify;">I promised this chapter would have been boring, didn&#8217;t I?</p>
<h1 style="text-align:justify;">Now what?</h1>
<p style="text-align:justify;">I&#8217;m a bit unsure about how to proceed.</p>
<p>Now that we have an acceptable development system, the next t<img loading="lazy" data-attachment-id="891" data-permalink="https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/questionmark/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/questionmark.png" data-orig-size="640,416" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="questionmark" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/questionmark.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/questionmark.png?w=640" class="  wp-image-891 alignright" src="https://cdry.wordpress.com/wp-content/uploads/2017/01/questionmark.png?w=182&#038;h=118" alt="QuestionMark.png" width="182" height="118" srcset="https://cdry.wordpress.com/wp-content/uploads/2017/01/questionmark.png?w=182&amp;h=118 182w, https://cdry.wordpress.com/wp-content/uploads/2017/01/questionmark.png?w=364&amp;h=237 364w, https://cdry.wordpress.com/wp-content/uploads/2017/01/questionmark.png?w=150&amp;h=98 150w, https://cdry.wordpress.com/wp-content/uploads/2017/01/questionmark.png?w=300&amp;h=195 300w" sizes="(max-width: 182px) 100vw, 182px" />hing I was planning to do was to fully initialize the VDP. I&#8217;m pretty sure this is going to involve a ton of repeating code, for initializing the various registers and memory locations of the VDP, using the same two (data + control) ports.</p>
<p>So we could surely use some structure in our code. Maybe create some subroutines to perform frequent tasks and see if the assembler provides macros. That could help too.</p>
<h1>Subroutines</h1>
<p style="text-align:justify;">Back to the 68K tutorial:<br />
<a href="http://mrjester.hapisan.com/04_MC68/Sect05Part04/Index.html" target="_blank" rel="noopener">http://mrjester.hapisan.com/04_MC68/Sect05Part04/Index.html</a><br />
<a href="http://mrjester.hapisan.com/04_MC68/Sect05Part05/Index.html" target="_blank" rel="noopener">http://mrjester.hapisan.com/04_MC68/Sect05Part05/Index.html</a></p>
<p>So CALL and RET in 68k are JSR (jump to subroutine) and RTS (return from subroutine). Before we can use them, we must initialize the stack first.</p>
<p>My decision to allocate a7 for the VDP was unfortunate, as the 68K treats it as the stack pointer. Identifiers &#8216;sp&#8217; and &#8216;a7&#8217; refer to the exact same register, so we&#8217;ll need to put the VDP&#8217;s port elsewhere, maybe a5. But first we need to decide where to put the stack. Let&#8217;s review the memory map in sega2 document again&#8230;</p>
<p><img loading="lazy" data-attachment-id="966" data-permalink="https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/memorymap-2/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/memorymap1.png" data-orig-size="426,460" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="memorymap" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/memorymap1.png?w=278" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/memorymap1.png?w=426" class="alignnone size-full wp-image-966 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2017/01/memorymap1.png?w=670" alt="memoryMap.png"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/01/memorymap1.png 426w, https://cdry.wordpress.com/wp-content/uploads/2017/01/memorymap1.png?w=139&amp;h=150 139w, https://cdry.wordpress.com/wp-content/uploads/2017/01/memorymap1.png?w=278&amp;h=300 278w" sizes="(max-width: 426px) 100vw, 426px" /></p>
<p>Am I stupid for not understanding this? The text above the image says this image displays the difference between the two modes: 16-bit mode and master system compatibility mode. I&#8217;m going to guess that the right hand side is for the 16-bit mode because it has a mapped area for the z80. So our work RAM must be in the range FF0000-FFFFFF, which gives us 64K to play with.</p>
<p>So let&#8217;s put our stack pointer to FFFFFF:</p>
<pre>    move.l #$FFFFFF, sp</pre>
<p style="text-align:justify;">And let&#8217;s create a separate assembly file for system initialization. I&#8217;ll approach this by creating a subroutine there that will perform the initialization, then include the file at the bottom of our program and call the subroutine from our main program, so that the interesting stuff remains on top of the binary.</p>
<p>Silly me, you cannot initialize the stack pointer in a subroutine! This must be the first thing we must do in our program. Let&#8217;s just do that.</p>
<p>And&#8230; all hell broke loose.</p>
<p>Remember what we said about odd addressing? FFFFFF is odd, isn&#8217;t it? So maybe, just maybe we shouldn&#8217;t have put the stack pointer there! I put it at $1000000 and the subroutine works just fine. a7 is decremented by 4 bytes first and then the return address is copied on top of stack:</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="904" data-permalink="https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/topofstack/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/topofstack.png" data-orig-size="454,57" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="topofstack" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/topofstack.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/topofstack.png?w=454" class="alignnone size-full wp-image-904" src="https://cdry.wordpress.com/wp-content/uploads/2017/01/topofstack.png?w=670" alt="topOfStack.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/01/topofstack.png 454w, https://cdry.wordpress.com/wp-content/uploads/2017/01/topofstack.png?w=150&amp;h=19 150w, https://cdry.wordpress.com/wp-content/uploads/2017/01/topofstack.png?w=300&amp;h=38 300w" sizes="(max-width: 454px) 100vw, 454px" /></p>
<p style="text-align:justify;">This is also the last memory location that regen allows me to see, which means our assumption was correct: Mega Drive&#8217;s working memory is from FF0000 to FFFFFF. The emulator shows only the last 4 hex digits of the address though. Strange, but then again those are the important ones.</p>
<h1 style="text-align:justify;">Macros</h1>
<p style="text-align:justify;">Because I&#8217;m extremely lazy, I checked the vasm documentation hoping to find some macro mechanism that will facilitate certain tasks (like packing a word to set a VDP register).</p>
<p style="text-align:justify;">I wrote the following as a joke, but I&#8217;m truly amazed the assembler could actually parse it:</p>
<pre style="text-align:justify;">    macro VDPSetRegister, register, value
        move.w #((( $80 | \register) &lt;&lt; 8 ) | \value), (a5)
    endmacro</pre>
<p style="text-align:justify;">So now, we can easily set a VDP register by writing the following, and all the packing required happens during assembly time:</p>
<pre style="text-align:justify;">    VDPSetRegister registerIndex, value</pre>
<p style="text-align:justify;">Now, since we have the luxury of macros, we needn&#8217;t allocate our precious address registers to point to the VDP ports. We can simply hard-code the port addresses in the macro:</p>
<pre style="text-align:justify;">    macro VDPSetRegister, register, value
        move.w #((( $80 | \register) &lt;&lt; 8 ) | \value), $C00004
    endmacro</pre>
<p style="text-align:justify;">Not sure which method is optimal. I guess using an address register will generate a more compact instruction, but I wont let myself worry about that.</p>
<p>Even better, we can use a nested macro for writing a register:</p>
<pre style="text-align:justify;">    macro VDPWriteToControl, value
        move.w \value, $C00004
    endmacro

    macro VDPSetRegister, register, value
        VDPWriteToControl #((( $80 | \register) &lt;&lt; 8 ) | \value)
    endmacro</pre>
<p style="text-align:justify;">A little voice in my head says this is not at all how they programmed the console back in the 90s, and it goes on to say that if that&#8217;s the way it&#8217;s gonna be, I&#8217;d be better off using a C compiler or something.</p>
<p>I&#8217;m ignoring the voice.</p>
<p>After adding one more macro to write to the data port, here is how our previous &#8220;blue screen&#8221; test looks like:</p>
<pre style="text-align:justify;">    VDPWriteToControl #$C000    ; Give the command to write to palette entry 0
    VDPWriteToControl #$0000
    VDPWriteToData #$0E00        ; Write BLUE to the data port
    
    VDPSetRegister 7, 0         ; Set VDP register #07 (BGColor) to 0
    
    VDPSetRegister 0, $04        ; Set r#00 as the manual says
    VDPSetRegister 1, $44        ; Set bit 7 of VDP r#01, to enable display

</pre>
<h1 style="text-align:justify;">Initializing the VDP</h1>
<p style="text-align:justify;">After spending about half an hour making sure there&#8217;s absolutely nothing left to do to make my life easier, it&#8217;s time to do this full VDP initialization we talked about in <a href="https://cdry.wordpress.com/2017/01/08/bfp-chapter-02-the-blue-screen-of-birth/">Chapter 2</a>.</p>
<p style="text-align:justify;"><img loading="lazy" data-attachment-id="919" data-permalink="https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/procrastination/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/procrastination.png" data-orig-size="768,768" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="procrastination" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/procrastination.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/procrastination.png?w=670" class="  wp-image-919 alignleft" src="https://cdry.wordpress.com/wp-content/uploads/2017/01/procrastination.png?w=149&#038;h=149" alt="Procrastination.png" width="149" height="149" srcset="https://cdry.wordpress.com/wp-content/uploads/2017/01/procrastination.png?w=149 149w, https://cdry.wordpress.com/wp-content/uploads/2017/01/procrastination.png?w=298 298w" sizes="(max-width: 149px) 100vw, 149px" /></p>
<p style="text-align:justify;">OR, I can take a break and go for a walk! After all, it&#8217;s better to do this kind of thing with a clear head, isn&#8217;t it?</p>
<p>Ok, I&#8217;m back! I&#8217;m so bored to do the following that I took one of my long walks today.</p>
<p>The &#8220;long walk&#8221; protocol is simple: I choose a random direction and walk as far away from home as possible, taking random turns as I go, until I get lost. Then I use my (often perfect) sense of direction to guide me home.</p>
<p>This randomness makes the walk a bit more adventurous. You never know what lies after the next corner (mostly houses though). It also is an interesting way to burn some calories&#8230; At least more interesting than the treadmill.</p>
<p>Let us first set all the VDP&#8217;s registers, since we already have the relevant macros. I&#8217;m going to do this by looking at the docs, see what a specific register does, and then de-activate all features that I don&#8217;t understand or that I&#8217;m not going to use.</p>
<p>Here is a first draft:</p>
<pre>systemInitVDP:
    VDPSetRegister  0, $04        ; R#00 : HInterrupt=0
    VDPSetRegister  1, $4C        ; R#01 : Display=1, VInterrupt=0, DMA=0, Vertical 30 Cell mode (PAL)
    VDPSetRegister  2, $00        ; R#02 : Pattern name for SCROLL A
    VDPSetRegister  3, $00        ; R#03 : Pattern name for WINDOW
    VDPSetRegister  4, $00        ; R#04 : Pattern name for SCROLL B
    VDPSetRegister  5, $00        ; R#05 : Sprite Attribute Table
    VDPSetRegister  6, $00        ; R#06 : Should be zero
    VDPSetRegister  7, $00        ; R#07 : Background Color: 00PP CCCC
    VDPSetRegister  8, $00        ; R#08 : Should be zero
    VDPSetRegister  9, $00        ; R#09 : Should be zero
    VDPSetRegister 10, $01        ; R#10 : HInterrupt timing - probably '1' means "every scanline"
    VDPSetRegister 11, $00        ; R#11 : ExternalInterrupt=0, Vscroll=Hscroll="full"
    VDPSetRegister 12, $81        ; R#12 : Horizontal 40 cell mode, no interlace, no shadow/highlight
    VDPSetRegister 13, $00        ; R#13 : HScroll data table
    VDPSetRegister 14, $00        ; R#14 : Should be zero
    VDPSetRegister 15, $02        ; R#15 : Auto-increment by 2 bytes each RAM access
    VDPSetRegister 16, $00        ; R#16 : Scroll size: H32, V32 cell
    VDPSetRegister 17, $00        ; R#17 : WINDOW 0 cells from left
    VDPSetRegister 18, $00        ; R#18 : WINDOW 0 cells from bottom
    VDPSetRegister 19, $00        ; R#19 : DMA Length counter low
    VDPSetRegister 20, $00        ; R#20 : DMA Length counter high
    VDPSetRegister 21, $00        ; R#21 : DMA Source Address Low
    VDPSetRegister 22, $00        ; R#22 : DMA Source Address Medium
    VDPSetRegister 23, $00        ; R#23 : DMA Source Address High
    rts</pre>
<h1>DMA</h1>
<p>Then, it would make sense to zero the VDP&#8217;s memory. What would be the best way to do this? We can access VRAM through the VDP ports, or we can use a &#8220;DMA Fill&#8221;, which sounds better. Here is the procedure to perform a DMA Fill:</p>
<p><img loading="lazy" data-attachment-id="930" data-permalink="https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/dmafill/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/dmafill.png" data-orig-size="833,470" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="dmafill" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/dmafill.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/dmafill.png?w=670" class="alignnone size-full wp-image-930" src="https://cdry.wordpress.com/wp-content/uploads/2017/01/dmafill.png?w=670" alt="dmaFill.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/01/dmafill.png 833w, https://cdry.wordpress.com/wp-content/uploads/2017/01/dmafill.png?w=150&amp;h=85 150w, https://cdry.wordpress.com/wp-content/uploads/2017/01/dmafill.png?w=300&amp;h=169 300w, https://cdry.wordpress.com/wp-content/uploads/2017/01/dmafill.png?w=768&amp;h=433 768w" sizes="(max-width: 833px) 100vw, 833px" /></p>
<p>We first need to turn on the DMA bit (M1) of register #1. After the DMA operation is complete, it will be reset to 0 automatically. Let&#8217;s first make a few more macros to read back VDP registers and automate toggling specific bits.</p>
<p>Nope, the registers are write-only, except from the &#8220;status&#8221; register which is read-only. For now, we&#8217;re just going to refill r#01:</p>
<pre>    VDPSetRegister  1, $5C</pre>
<p>Next, we need to specify the fill length in registers 19 and 20. The VDP has 64K of VRAM, so I&#8217;m a bit unsure of what to put in there. I can&#8217;t fit a value greater than FFFF, so maybe a value of 0 will fill the entire thing.</p>
<p>This will be my second experiment, but for now, let&#8217;s just fill FFFE bytes:</p>
<pre>    VDPSetRegister 20, $FF
    VDPSetRegister 19, $FE</pre>
<p>Then, we need to fill register #23 with the pattern MMSS SSSS. First two bits is &#8220;DMA mode&#8221;, which in our case is &#8217;10&#8217; (VRAM fill) and rest of the bits are for source address (in case of DMA copy), which we&#8217;ll set to zero:</p>
<pre>    VDPSetRegister 23, $80</pre>
<p>Next, we have two writes to the control port, containing the destination address, scattered. Fortunately, our destination is zero, so:</p>
<pre>    VDPWriteToControl #$4000
    VDPWriteToControl #$0080
</pre>
<p>Finally (omg!) we must write the fill pattern to the data port (let&#8217;s try ABCD as a test fill):</p>
<pre>    VDPWriteToData #$ABCD</pre>
<p>Note that this ain&#8217;t the whole story. Register #15 contains the &#8220;increment&#8221; value. We set that to &#8216;2&#8217; previously while initializing, so this is going to be a 16 bit fill. Let&#8217;s leave it at that and do the test&#8230;</p>
<p>Hehehehe &#8230;.</p>
<p><img loading="lazy" data-attachment-id="938" data-permalink="https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/hehehe/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/hehehe.png" data-orig-size="685,538" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="hehehe" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/hehehe.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/hehehe.png?w=670" class=" size-full wp-image-938 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2017/01/hehehe.png?w=670" alt="hehehe.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/01/hehehe.png 685w, https://cdry.wordpress.com/wp-content/uploads/2017/01/hehehe.png?w=150&amp;h=118 150w, https://cdry.wordpress.com/wp-content/uploads/2017/01/hehehe.png?w=300&amp;h=236 300w" sizes="(max-width: 685px) 100vw, 685px" /></p>
<p>Something was accomplished here. Probably not what we wanted though. The blue stripes are of color 0, so we might have filled zeroes every other byte or so. Let&#8217;s take a look at the contents of VRAM:</p>
<p><img loading="lazy" data-attachment-id="940" data-permalink="https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/vram/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/vram.png" data-orig-size="482,242" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="vram" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/vram.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/vram.png?w=482" class=" size-full wp-image-940 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2017/01/vram.png?w=670" alt="VRAM.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/01/vram.png 482w, https://cdry.wordpress.com/wp-content/uploads/2017/01/vram.png?w=150&amp;h=75 150w, https://cdry.wordpress.com/wp-content/uploads/2017/01/vram.png?w=300&amp;h=151 300w" sizes="(max-width: 482px) 100vw, 482px" /></p>
<p>It&#8217;s filled allright, but only with &#8220;AB&#8221;. What happened to &#8220;CD&#8221;? Also, why is it filled all the way to the end (FFFF)? Maybe the fill length is in &#8220;increments&#8221; and not in bytes. Let&#8217;s try to fill fewer bytes, say the first 256:</p>
<pre>    VDPSetRegister 20, $01
    VDPSetRegister 19, $00</pre>
<p style="text-align:justify;">Fuck&#8230;</p>
<p><img loading="lazy" data-attachment-id="944" data-permalink="https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/fuck/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/fuck.png" data-orig-size="458,149" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="fuck" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/fuck.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/fuck.png?w=458" class="alignnone size-full wp-image-944 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2017/01/fuck.png?w=670" alt="fuck.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/01/fuck.png 458w, https://cdry.wordpress.com/wp-content/uploads/2017/01/fuck.png?w=150&amp;h=49 150w, https://cdry.wordpress.com/wp-content/uploads/2017/01/fuck.png?w=300&amp;h=98 300w" sizes="(max-width: 458px) 100vw, 458px" /></p>
<p>Now we know the length is in &#8216;increments&#8217;. It fills about 512 bytes. 514 to be exact, and only the first two bytes contain my data. Inverted.</p>
<p>&#8230;</p>
<p>Ok. The inversion is normal. The filler starts with the lower byte. Also the &#8220;increments+1&#8221; is normal-ish. That is if you interpret the &#8220;DMA starts at after (E)&#8221; to mean that. I&#8217;m buying it and say I want to fill 255 bytes instead.</p>
<p>Also, for my psychological well-being, I&#8217;m going to assume that the zeroes are just a silent emulator bug. Maybe there aren&#8217;t any games that try to fill VRAM with words (you normally fill with zeroes anyway). I&#8217;ll set the auto-increment (register 15) to be 1 by default and both high and low bytes to be the same &#8216;AA&#8221; and it better work.</p>
<p>Respect my authorita:</p>
<p><img loading="lazy" data-attachment-id="947" data-permalink="https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/authorita/#main" data-orig-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/authorita.png" data-orig-size="480,315" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="authorita" data-image-description="" data-image-caption="" data-medium-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/authorita.png?w=300" data-large-file="https://cdry.wordpress.com/wp-content/uploads/2017/01/authorita.png?w=480" class=" size-full wp-image-947 aligncenter" src="https://cdry.wordpress.com/wp-content/uploads/2017/01/authorita.png?w=670" alt="authorita.PNG"   srcset="https://cdry.wordpress.com/wp-content/uploads/2017/01/authorita.png 480w, https://cdry.wordpress.com/wp-content/uploads/2017/01/authorita.png?w=150&amp;h=98 150w, https://cdry.wordpress.com/wp-content/uploads/2017/01/authorita.png?w=300&amp;h=197 300w" sizes="(max-width: 480px) 100vw, 480px" /></p>
<p>Ok. Now we have some sense of control over the VRAM fill thingy. Also, this &#8220;increments + 1&#8221; allows us to fill the entire VRAM. Let&#8217;s now fill it with zeroes and forget about it.</p>
<p>Not so fast! Literally. Before we&#8217;re through with this, we must first wait for the operation to finish. I don&#8217;t feel that comfortable issuing more commands to the VDP while it&#8217;s filling its memory.</p>
<p>To do that, we must first read the VDP &#8220;status&#8221; register.</p>
<p>To do this, we just read the &#8220;control port&#8221; address (C00004). Bit 3 is the DMA bit. DMA is active while this bit is &#8216;1&#8217;.</p>
<p>Let&#8217;s first create a macro to read the register:</p>
<pre style="text-align:justify;">    macro VDPReadStatus, value
        move.w $C00004, \value
    endmacro</pre>
<p>The macro takes a register as its argument, and copies the status byte to that register.</p>
<p>Now, how do we loop with a specific bit as a break condition? The 68K has a handy BTST instruction that checks bits, and another one that branches based on the zero flag:</p>
<pre>systemInitVDP_loop0:
    VDPReadStatus d0
    btst #3, d0
    bne systemInitVDP_loop0        ; wait until bit 3 (DMA) of status register is cleared</pre>
<p>Here is how the condition works:</p>
<ul>
<li>btst will test bit #3 (DMA)</li>
<li>If bit 3 is set, the zero flag will be zero</li>
<li>This means &#8220;not equal&#8221; (because when doing comparisons, you subtract, and if the result is zero, this sets the zero flag, which in turn means equal)</li>
<li>We branch back on &#8220;not equal&#8221; with bne</li>
</ul>
<p style="text-align:justify;">I would really prefer the instruction to be jnz. This extra step of &#8220;equality&#8221; took the best of me.</p>
<p>Since we may do some more DMA in the future, let&#8217;s make a subroutine to wait for DMA:</p>
<pre style="text-align:justify;">VDPWaitForDMA:
    VDPReadStatus d0
    btst #3, d0
    bne VDPWaitForDMA
    rts</pre>
<p style="text-align:justify;">I put that in a separate file called &#8220;VDP.x68&#8221;, which will be the place to put all VDP related routines.</p>
<h1 style="text-align:justify;">Afterword</h1>
<p>In this chapter we achieved significant progress. We learned about subroutines and macros, we performed a DMA operation and we more or less put the VDP in a defined state.</p>
<p>That&#8217;s not the kind of progress that I like. I very much prefer the visual stimulation of new stuff appearing on the screen, but nevertheless we covered a lot of ground towards that.</p>
<p>Next time, I want a sprite on the screen!</p>
<table>
<tbody>
<tr>
<td><a href="https://cdry.wordpress.com/2017/01/08/bfp-chapter-02-the-blue-screen-of-birth/">Previous Chapter</a></td>
<td style="text-align:center;"><a href="https://cdry.wordpress.com/2017/01/08/a-blast-from-the-past-toc/">TOC</a></td>
<td style="text-align:right;"><a href="https://cdry.wordpress.com/2017/09/17/bfp-chapter-04-room-for-one-texture/">Next Chapter</a></td>
</tr>
</tbody>
</table>
<div class="itanywhere-activator" style="left:36px;top:12847px;display:none;" title="Google Translator Anywhere"></div>
<div class="itanywhere-activator" style="left:231px;top:642px;display:none;" title="Google Translator Anywhere"></div>
]]></content:encoded>
					
					<wfw:commentRss>https://cdry.wordpress.com/2017/01/21/bfp-chapter-03-taking-out-the-trash/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">848</post-id>
		<media:content url="https://2.gravatar.com/avatar/571c802b5077513106d8a8eb429d60da2ef7d8027fc6b90454300a8da0d37a5e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mgeorgoulopoulos</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/01/included.png" medium="image">
			<media:title type="html">included.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/01/questionmark.png" medium="image">
			<media:title type="html">QuestionMark.png</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/01/memorymap1.png" medium="image">
			<media:title type="html">memoryMap.png</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/01/topofstack.png" medium="image">
			<media:title type="html">topOfStack.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/01/procrastination.png?w=288" medium="image">
			<media:title type="html">Procrastination.png</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/01/dmafill.png" medium="image">
			<media:title type="html">dmaFill.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/01/hehehe.png" medium="image">
			<media:title type="html">hehehe.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/01/vram.png" medium="image">
			<media:title type="html">VRAM.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/01/fuck.png" medium="image">
			<media:title type="html">fuck.PNG</media:title>
		</media:content>

		<media:content url="https://cdry.wordpress.com/wp-content/uploads/2017/01/authorita.png" medium="image">
			<media:title type="html">authorita.PNG</media:title>
		</media:content>
	</item>
	</channel>
</rss>
