Advertisement
Kitomas

crappy cc65 demo for ezr6502

Jun 28th, 2024
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.26 KB | None | 0 0
  1. //CC65 VERSION
  2. #ifndef _EZR6502_MISC_H
  3. #define _EZR6502_MISC_H
  4.  
  5.  
  6.  
  7. //naming convention notes that i may or may not follow:
  8. //local variables      = lowerCamelCase
  9. //global variables     = g_lowerCamelCase
  10. //macros and constants = ALL_CAPS
  11. //pointer variables    = p_XXXXXX
  12. //non struct typedefs  = lowercase_t
  13. //structs                    = UpperCamelCase
  14. //enumurations         = UpperCamelCase
  15. //c source functions   = UpperCamelCase
  16. //asm source functions = lowerCamelCase
  17. //enum members         = Capitalized or ALL_CAPS
  18. //__asm__ format specifiers
  19. //%b - Numerical 8-bit value
  20. //%w - Numerical 16-bit value
  21. //%l - Numerical 32-bit value
  22. //%v - Assembler name of a global variable or function
  23. //%o - Stack offset of a local variable
  24. //%g - Assembler name of a C label
  25. //%s - The argument is converted to a string
  26. //%% - The % sign itself
  27.  
  28. //-- SEGMENTS --
  29. //(Example: #pragma data_seg(Code))
  30. //Zeropage   : $00  -> $fd  (virtual; $fe,$ff are reserved for rng and kb input)
  31. //Stack      : $100 -> $1ff (virtual)
  32. //VideoMemory: $200 -> $5ff (virtual)
  33. //Code       : $600 -> $ffff
  34.  
  35.  
  36.  
  37. typedef        signed char s8;
  38. typedef        signed int  s16;
  39. typedef long   signed int  s32;
  40. typedef      unsigned char u8;
  41. typedef      unsigned int  u16;
  42. typedef long unsigned int  u32;
  43.  
  44. typedef union r16 {
  45.   u16 v;
  46.   struct { u8 l, h; };
  47. } r16;
  48.  
  49. #define  S8_MIN        -128
  50. #define S16_MIN      -32768
  51. #define S32_MIN -2147483648
  52. #define  S8_MAX         127
  53. #define S16_MAX       32767
  54. #define S32_MAX  2147483647
  55. #define  U8_MAX         255
  56. #define U16_MAX       65535
  57. #define U32_MAX  4294967295
  58.  
  59. #define MSb_8        0x80
  60. #define MSb_16     0x8000
  61. #define MSb_32 0x80000000
  62.  
  63.  
  64.  
  65. #define BITS_OFF8( var,bits) var &= (bits^0xff  )
  66. #define BITS_OFF16(var,bits) var &= (bits^0xffff)
  67. #define BITS_ON(   var,bits) var |=  bits;
  68.  
  69. #define TO_LOWER(chr) (c|( 0x20))
  70. #define TO_UPPER(chr) (c&(~0x20))
  71.  
  72.  
  73. //($80-$fd are reserved for compiler usage)
  74. #define RANDOM (*((char*)0xfe))
  75. #define KEY    (*((char*)0xff))
  76.  
  77. #define PIXELS ((char*)0x200)
  78.  
  79. #define DISABLE_BRK_PAUSE __asm__("sei")
  80. #define ENABLE_BRK_PAUSE  __asm__("cli")
  81. #define BREAK_POINT       __asm__("brk")
  82. #define UPDATE_SCREEN     __asm__("nop")
  83.  
  84. //first 16 colors
  85. #define COLOR_BLACK     0
  86. #define COLOR_DRED      1
  87. #define COLOR_DGREEN    2
  88. #define COLOR_DYELLOW   3
  89. #define COLOR_DBLUE     4
  90. #define COLOR_DMAGENTA  5
  91. #define COLOR_DCYAN     6
  92. #define COLOR_LGRAY     7
  93. #define COLOR_DGRAY     8
  94. #define COLOR_LRED      9
  95. #define COLOR_LGREEN   10
  96. #define COLOR_LYELLOW  11
  97. #define COLOR_LBLUE    12
  98. #define COLOR_LMAGENTA 13
  99. #define COLOR_LCYAN    14
  100. #define COLOR_WHITE    15
  101.  
  102.  
  103.  
  104. char g_char0;
  105. char g_char1;
  106. char g_char2;
  107.  
  108.  
  109.  
  110. #endif /* _EZR6502_MISC_H */
  111.  
  112.  
  113. //CC65 VERSION
  114. //each increment is equal to exactly 1.40625 degrees (about 0.0245436926 radians)
  115. //precision is about 1/255th (255 instead of 256 because -128 is not used)
  116. #ifndef _SIN8S_H
  117. #define _SIN8S_H
  118.  
  119.  
  120.  
  121. const signed char
  122. #ifdef SIN8S_MIN //64-wide lookup table
  123. #ifdef SIN8S_ALIGN
  124. //__align(64)
  125. #endif /* ifdef SIN8S_ALIGN */
  126. _sin8s_table[64] =
  127.  
  128. #else //256-wide lookup table
  129. #ifdef SIN8S_ALIGN
  130. //__align(256)
  131. #endif /* ifdef SIN8S_ALIGN */
  132. _sin8s_table[256] =
  133.  
  134. #endif /* ifndef SIN8S_MIN */
  135. { //(effective range of -127 -> 127)
  136.      0,    3,    6,    9,   12,   16,   19,   22,   25,   28,   31,   34,   37,   40,   43,   46,
  137.     49,   51,   54,   57,   60,   63,   65,   68,   71,   73,   76,   78,   81,   83,   85,   88,
  138.     90,   92,   94,   96,   98,  100,  102,  104,  106,  107,  109,  111,  112,  113,  115,  116,
  139.    117,  118,  120,  121,  122,  122,  123,  124,  125,  125,  126,  126,  126,  127,  127,  127,
  140. #ifndef SIN8S_MIN
  141. //this is redundant if you make the output index 64-(<input index>&63) (result = 127 if <input index>&127 = 64)
  142.    127,  127,  127,  127,  126,  126,  126,  125,  125,  124,  123,  122,  122,  121,  120,  118,
  143.    117,  116,  115,  113,  112,  111,  109,  107,  106,  104,  102,  100,   98,   96,   94,   92,
  144.     90,   88,   85,   83,   81,   78,   76,   73,   71,   68,   65,   63,   60,   57,   54,   51,
  145.     49,   46,   43,   40,   37,   34,   31,   28,   25,   22,   19,   16,   12,    9,    6,    3,
  146. //this is redundant if you just flip result if input index is > 127
  147.      0,   -3,   -6,   -9,  -12,  -16,  -19,  -22,  -25,  -28,  -31,  -34,  -37,  -40,  -43,  -46,
  148.    -49,  -51,  -54,  -57,  -60,  -63,  -65,  -68,  -71,  -73,  -76,  -78,  -81,  -83,  -85,  -88,
  149.    -90,  -92,  -94,  -96,  -98, -100, -102, -104, -106, -107, -109, -111, -112, -113, -115, -116,
  150.   -117, -118, -120, -121, -122, -122, -123, -124, -125, -125, -126, -126, -126, -127, -127, -127,
  151.   -127, -127, -127, -127, -126, -126, -126, -125, -125, -124, -123, -122, -122, -121, -120, -118,
  152.   -117, -116, -115, -113, -112, -111, -109, -107, -106, -104, -102, -100,  -98,  -96,  -94,  -92,
  153.    -90,  -88,  -85,  -83,  -81,  -78,  -76,  -73,  -71,  -68,  -65,  -63,  -60,  -57,  -54,  -51,
  154.    -49,  -46,  -43,  -40,  -37,  -34,  -31,  -28,  -25,  -22,  -19,  -16,  -12,   -9,   -6,   -3,
  155. #endif /* ifndef SIN8S_MIN */
  156. };
  157.  
  158.  
  159. extern char g_char0;
  160.  
  161.  
  162. #ifdef SIN8S_INLINE
  163. inline
  164. #endif /* ifdef SIN8S_INLINE */
  165. signed char __fastcall__ sin8s(const char x){
  166.   g_char0 = x;
  167.  
  168. #ifdef SIN8S_MIN //slower, but takes up less space (lookup table included)
  169.   __asm__("lda %v", g_char0);
  170.   __asm__("tay");      //store original value of a in y
  171.   __asm__("and #127"); //a %= 128
  172.  
  173.  
  174.   //a = 0-63: index = a
  175.   __asm__("cmp #64");               //branch if a<64
  176.   __asm__("bcc %g", _index_result);  //^^
  177.  
  178.   //a = 65-127: index = 64-(a%64)
  179.   __asm__("cmp #65");             //branch if a<65
  180.   __asm__("bcc %g", _not_65_127);  //^^
  181.     __asm__("and #63");         //a %= 64
  182.     __asm__("sta %v", g_char0); //tmp = a
  183.     __asm__("lda #64");         //a = 64
  184.     __asm__("sec");             //a -= tmp
  185.     __asm__("sbc %v", g_char0);  //^^
  186.     __asm__("jmp %g", _index_result);
  187.   _not_65_127:
  188.  
  189.   //result will either be 127 or -127
  190.    //if input was 64 or 192 respectively
  191.   __asm__("lda #127");
  192.   __asm__("jmp %g", _dont_index_result);
  193.  
  194.  
  195.   _index_result:
  196.   __asm__("tax"    );                //put index into x
  197.   __asm__("lda %v,x", _sin8s_table); //get result
  198.   _dont_index_result:
  199.  
  200.  
  201.   __asm__("tax");                  //store result into x
  202.   __asm__("tya");                  //branch if input was positive
  203.   __asm__("bpl %g", _not_negative); //^^
  204.     __asm__("txa");      //get result back
  205.     __asm__("eor #$ff"); //result = -result (if input was negative)
  206.     __asm__("clc");       //^^
  207.     __asm__("adc #1");    //^^
  208.     __asm__("tax");      //store new result in x
  209.   _not_negative:
  210.  
  211.   //put (potentially altered) result into return variable
  212.   __asm__("stx %v", g_char0);
  213.  
  214.  
  215. #else
  216.   //faster, but takes up more space (lookup table included)
  217.   __asm__("ldx %v", g_char0);
  218.   __asm__("lda %v,x", _sin8s_table);
  219.   __asm__("sta %v", g_char0);
  220.  
  221.  
  222. #endif /* ifdef SIN8S_MIN */
  223.   return g_char0;
  224. }
  225.  
  226. #define cos8s(_v) (sin8s((_v)+64))
  227.  
  228.  
  229.  
  230.  
  231.  
  232. #endif /* ifndef _SIN8S_H */
  233.  
  234.  
  235.  
  236.  
  237.  
  238. s8 abs8(s8 n){ return (n<0) ? -n : n; }
  239.  
  240. u8 getgray_u(u8 n){ return 232+n/11; }
  241. u8 getgray_s(s8 n){ return 232+abs8(n)/6; }
  242.  
  243. /*
  244. s8 shift_s8r(s8 n, u8 s){
  245.   s8 _n = n>>s;
  246.   //bit extend if negative
  247.   if(n<0) return (0xff<<(8-s)) | _n;
  248.   else    return _n;
  249. }
  250. */
  251. #define shift_s8r(_n,_s) ( ((_n)<0) ? (0xff<<(8-(_s)))|(_n>>(_s)) : (_n>>(_s)) )
  252.  
  253.  
  254. /*
  255. void putpixel(s8 p_x, s8 p_y, u8 color){
  256.   if((p_x&0b11100000) || (p_y&0b11100000)) return;
  257.   PIXELS[p_x | ((int)p_y)<<5] = color;
  258. }
  259. */
  260. #define putpixel(p_x, p_y, color) PIXELS[p_x | ((int)p_y)<<5] = color
  261.  
  262.  
  263.  
  264. s8 min(s8 a, s8 b){ return (a< b) ? a : b; }
  265. s8 max(s8 a, s8 b){ return (a>=b) ? a : b; }
  266.  
  267. s8 edge_start[32];
  268. s8 edge_end  [32];
  269. s8 edge_mod  [32];
  270. s8 grayscale[256];
  271.  
  272.  
  273. void main(){
  274. /*
  275.   s8 p_x0, p_x1, p_x, p_y;
  276.   s8 i0, i1, color;
  277.   s8 shift = 0;
  278.   _START:
  279.   i0 = -65,  i1 = -64;
  280.  
  281.   do {
  282.     p_x0 = 16 + shift_s8r(cos8s(i0), 3);
  283.     p_x1 = 16 + shift_s8r(cos8s(i1), 3);
  284.     p_y  = 16 + shift_s8r(sin8s(i1), 3);
  285.    
  286.     for(p_x = p_x0; p_x <= p_x1; ++p_x){
  287.       color = getgray_s(cos8s(i1+(shift+=3)));
  288.       putpixel(p_x, p_y, color);
  289.     }
  290.    
  291.    
  292.     --i0;
  293.   } while(++i1 != 51);
  294.  
  295.  
  296.   UPDATE_SCREEN;
  297.   BREAK_POINT;
  298.   goto _START;
  299. */
  300.  
  301.   u16 addr;
  302.   s8 p_x0, p_x1, p_x, p_y;
  303.   s8 i0, i1, color;
  304.   s8 shift = 0;
  305.   //_INIT:
  306.   i0 = -65,  i1 = -64;
  307.  
  308.   do {
  309.     p_x0 = 16 + shift_s8r(cos8s(i0), 3);
  310.     p_x1 = 16 + shift_s8r(cos8s(i1), 3);
  311.     p_y  = 16 + shift_s8r(sin8s(i1), 3);
  312.    
  313.     edge_start[p_y] = min(p_x0  , p_x1  );
  314.     edge_end  [p_y] = max(p_x0+1, p_x1+1);
  315.     edge_mod  [p_y] = i1;
  316.     //(+1 so i can use < when looping through x)
  317.    
  318.     --i0;
  319.   } while(++i1 != 51);
  320.  
  321.  
  322.  
  323.   //generate grayscale lookup
  324.   i0 = 0;
  325.   do {
  326.     //grayscale[i0] = 16+i0/43;//getgray_s(i0);
  327.     grayscale[i0] = getgray_s(i0);
  328.   } while(++i0);
  329.  
  330.  
  331.  
  332.  
  333.   _START:
  334.   i1 = -64;
  335.   p_y = 0;
  336.   _LOOPY:
  337.  
  338.  
  339.   p_x0 = edge_start[p_y];
  340.   p_x1 = edge_end  [p_y];
  341.   addr = p_x0 | ((u16)p_y)<<5;
  342.   for(p_x = p_x0; p_x <= p_x1; ++p_x){
  343.     color = grayscale[ sin8s(i1+(shift+=5)) ];
  344.     PIXELS[addr] = color;
  345.     ++addr;
  346.   }
  347.   ++i1;
  348.  
  349.   if(++p_y != 32) goto _LOOPY;
  350.  
  351.   UPDATE_SCREEN;
  352.   //BREAK_POINT;
  353.   goto _START;
  354. }
  355.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement