Sieve of Eratosthenes MD

Discussion in 'Technical Discussion' started by snkenjoi, Aug 5, 2009.

  1. snkenjoi

    snkenjoi

    Tech Member
    85
    0
    0
    This is the first MD ROM I've written from scratch. It's a simple <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" target="_blank">Sieve of Eratosthenes</a> implemented in C.

    <img src="http://snk.digibase.ca/hacks/sieve.png" border="0" class="linked-image" />

    <a href="http://snk.digibase.ca/hacks/sieve.bin" target="_blank">Download</a>

    I've gotta say thanks to Stef for the <a href="http://gendev.spritesmind.net/forum/viewtopic.php?t=14" target="_blank">great DevKit</a> I used to make this. It's the first one I've come across that I've been actually able to compile shit with :)

    Have some source;

    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->#include "genesis.h"
    #include "x.h"

    const u16 pal_yellow[16] =
    {
    &nbsp;&nbsp;&nbsp;&nbsp;0x0000,
    &nbsp;&nbsp;&nbsp;&nbsp;0x0022,
    &nbsp;&nbsp;&nbsp;&nbsp;0x0044,
    &nbsp;&nbsp;&nbsp;&nbsp;0x0066,
    &nbsp;&nbsp;&nbsp;&nbsp;0x0088,
    &nbsp;&nbsp;&nbsp;&nbsp;0x00AA,
    &nbsp;&nbsp;&nbsp;&nbsp;0x00CC,
    &nbsp;&nbsp;&nbsp;&nbsp;0x00EE,

    &nbsp;&nbsp;&nbsp;&nbsp;0x0EEE,
    &nbsp;&nbsp;&nbsp;&nbsp;0x0EEE,
    &nbsp;&nbsp;&nbsp;&nbsp;0x0EEE,
    &nbsp;&nbsp;&nbsp;&nbsp;0x0EEE,
    &nbsp;&nbsp;&nbsp;&nbsp;0x0EEE,
    &nbsp;&nbsp;&nbsp;&nbsp;0x0EEE,
    &nbsp;&nbsp;&nbsp;&nbsp;0x0EEE,
    &nbsp;&nbsp;&nbsp;&nbsp;0x0EEE
    };

    static void eratosthenes();

    int main()
    {
    &nbsp;&nbsp;&nbsp;&nbsp;VDP_setPalette(0, pal_yellow);
    &nbsp;&nbsp;&nbsp;&nbsp;VDP_drawText(APLAN, "Sieve of Eratosthenes MD", 0x8000, 1, 1);
    &nbsp;&nbsp;&nbsp;&nbsp;VDP_drawText(APLAN, "by&nbsp;&nbsp;snkenjoi", 0x8000, 24, 25);
    &nbsp;&nbsp;&nbsp;&nbsp;setBitmapMode(BITMAP_WIDTH, BITMAP_HEIGHT);
    &nbsp;&nbsp;&nbsp;&nbsp;loadGenBmp16ToBitmap(x, 21, 10, 0x10);
    &nbsp;&nbsp;&nbsp;&nbsp;eratosthenes();
    &nbsp;&nbsp;&nbsp;&nbsp;return 0;
    }

    void eratosthenes()
    {
    &nbsp;&nbsp;&nbsp;&nbsp;char str[255];
    &nbsp;&nbsp;&nbsp;&nbsp;int I,k,j = 0;
    &nbsp;&nbsp;&nbsp;&nbsp;int arr[15999]; //15999
    &nbsp;&nbsp;&nbsp;&nbsp;int max = (sizeof(arr)/sizeof(arr[0]));


    &nbsp;&nbsp;&nbsp;&nbsp;for (I = 0;I<max;I+=2) {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr = 1;
    &nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;VDP_drawText(APLAN, "2", 0x8000, 1, 2);
    &nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;for (k = 0;k<max;k++){
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (I = 0;I<max;I++) {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(arr == 0) {arr = 1; j = I;wait(5000);intToStr(I+2, str, 1);VDP_drawText(APLAN, str, 0x8000, 1, 2);break;}
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (I = j;I<max;I+=j+2) { arr = 1; }
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (j*j>max+2){break;}
    &nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;for (I = 0;I<max;I++) {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(arr == 0) {arr = 1;wait(5000);intToStr(I+2, str, 1);VDP_drawText(APLAN, str, 0x8000, 1, 2);};
    &nbsp;&nbsp;&nbsp;&nbsp;}
    }<!--c2--></div><!--ec2-->

    Maybe I'll make something useful next time ;)
     
  2. Andlabs

    Andlabs

    「いっきまーす」 Wiki Sysop
    2,175
    0
    0
    Writing my own MD/Genesis sound driver :D
    You're welcome (for linking you to the devkit on IRC). And this is interesting. Perhaps now the Genesis can be used for cryptography? :v:

    And shameless self-advertising, thanks to Doug McIllroy: <a href="http://plan9.bell-labs.com/sources/contrib/pietro/sieve.c" target="_blank">http://plan9.bell-labs.com/sources/contrib/pietro/sieve.c</a>. Put that on the Genesis :v:
     
  3. TmEE

    TmEE

    Master of OPL3-SA2/3 Tech Member
    1,726
    0
    16
    Estonia, Rapla City
    T-04YBSC-A !
    well, this works nicely on real HW :)
     
  4. Overlord

    Overlord

    ...did we just switch realities? Moderator
    17,430
    12
    18
    Berkshire, England
    Learning Cymraeg
    A C devkit for Megadrive? Nice.

    Good work on the ROM, too =P Is it supposed to just count up to 15991 in the top left, though, or do a list like the wiki page?
     
  5. Andlabs

    Andlabs

    「いっきまーす」 Wiki Sysop
    2,175
    0
    0
    Writing my own MD/Genesis sound driver :D
    <!--quoteo(post=338480:date=Aug 5 2009, 04:34 PM:name=Overlord)--><div class='quotetop'>QUOTE (Overlord @ Aug 5 2009, 04:34 PM) <a href="index.php?act=findpost&pid=338480"><img src="public/style_images/retro/snapback.png"></a></div><div class='quotemain'><!--quotec-->A C devkit for Megadrive? Nice.

    Good work on the ROM, too =P Is it supposed to just count up to 15991 in the top left, though, or do a list like the wiki page?<!--QuoteEnd--></div><!--QuoteEEnd-->
    C devkits have existed for a while now: SGCC and XGCC have been around for years. But this is the first to have a full programming API.
     
  6. Sik

    Sik

    Sik is pronounced as "seek", not as "sick". Tech Member
    6,719
    0
    0
    being an asshole =P
    Moreover, there's a C devkit for MD from back to 1994. Not to mention that Sega also had its own C devkit (Sonic Spinball, anyone?).
     
  7. Overlord

    Overlord

    ...did we just switch realities? Moderator
    17,430
    12
    18
    Berkshire, England
    Learning Cymraeg
    <!--quoteo(post=339704:date=Aug 11 2009, 05:39 PM:name=Sik)--><div class='quotetop'>QUOTE (Sik @ Aug 11 2009, 05:39 PM) <a href="index.php?act=findpost&pid=339704"><img src="public/style_images/retro/snapback.png"></a></div><div class='quotemain'><!--quotec-->Moreover, there's a C devkit for MD from back to 1994. Not to mention that Sega also had its own C devkit (Sonic Spinball, anyone?).<!--QuoteEnd--></div><!--QuoteEEnd-->
    Well, yes. But did the public have access to that? No =P

    <!--quoteo(post=339654:date=Aug 11 2009, 02:07 PM:name=Andlabs)--><div class='quotetop'>QUOTE (Andlabs @ Aug 11 2009, 02:07 PM) <a href="index.php?act=findpost&pid=339654"><img src="public/style_images/retro/snapback.png"></a></div><div class='quotemain'><!--quotec-->C devkits have existed for a while now: SGCC and XGCC have been around for years. But this is the first to have a full programming API.<!--QuoteEnd--></div><!--QuoteEEnd-->I must admit I was unaware of those 2. But by the sound of it this one is better anyway :)
     
  8. Sik

    Sik

    Sik is pronounced as "seek", not as "sick". Tech Member
    6,719
    0
    0
    being an asshole =P
    <!--quoteo(post=339789:date=Aug 11 2009, 05:30 PM:name=Overlord)--><div class='quotetop'>QUOTE (Overlord @ Aug 11 2009, 05:30 PM) <a href="index.php?act=findpost&pid=339789"><img src="public/style_images/retro/snapback.png"></a></div><div class='quotemain'><!--quotec--><!--quoteo(post=339704:date=Aug 11 2009, 05:39 PM:name=Sik)--><div class='quotetop'>QUOTE (Sik @ Aug 11 2009, 05:39 PM) <a href="index.php?act=findpost&pid=339704"><img src="public/style_images/retro/snapback.png"></a></div><div class='quotemain'><!--quotec-->Moreover, there's a C devkit for MD from back to 1994. Not to mention that Sega also had its own C devkit (Sonic Spinball, anyone?).<!--QuoteEnd--></div><!--QuoteEEnd-->Well, yes. But did the public have access to that? No =P<!--QuoteEnd--></div><!--QuoteEEnd-->The one from 1994? Yes. Spinball was released on 1993 so that should make it obvious that it wasn't the one I was talking about :P
     
  9. Overlord

    Overlord

    ...did we just switch realities? Moderator
    17,430
    12
    18
    Berkshire, England
    Learning Cymraeg
    HUSH >=P

    Fair point, but this one still stands as the most fully featured.
     
  10. Krigo

    Krigo

    Robotics;Notes shill Member
    I wonder what C dev kit Megaman: The Wily Wars used. According to Protoman the game was coded in C(he gave up disassembling it for that reason) and was released in late 1994 for all regions.
     
  11. Sik

    Sik

    Sik is pronounced as "seek", not as "sick". Tech Member
    6,719
    0
    0
    being an asshole =P
    <!--quoteo(post=339849:date=Aug 11 2009, 07:15 PM:name=Krigo)--><div class='quotetop'>QUOTE (Krigo @ Aug 11 2009, 07:15 PM) <a href="index.php?act=findpost&pid=339849"><img src="public/style_images/retro/snapback.png"></a></div><div class='quotemain'><!--quotec-->I wonder what C dev kit Megaman: The Wily Wars used. According to Protoman the game was coded in C(he gave up disassembling it for that reason) and was released in late 1994 for all regions.<!--QuoteEnd--></div><!--QuoteEEnd-->Once Oerg was messing up with his Mega Drive hardware (overclocking I think) and he tried a game from 1995. The game threw out a "not enough memory available" error and was emitted by some kind of library made by Sega (I don't remember the exact message right now). So I guess Sega made a C library for the Mega Drive.