Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //CC65 VERSION
- #ifndef _EZR6502_MISC_H
- #define _EZR6502_MISC_H
- //naming convention notes that i may or may not follow:
- //local variables = lowerCamelCase
- //global variables = g_lowerCamelCase
- //macros and constants = ALL_CAPS
- //pointer variables = p_XXXXXX
- //non struct typedefs = lowercase_t
- //structs = UpperCamelCase
- //enumurations = UpperCamelCase
- //c source functions = UpperCamelCase
- //asm source functions = lowerCamelCase
- //enum members = Capitalized or ALL_CAPS
- //__asm__ format specifiers
- //%b - Numerical 8-bit value
- //%w - Numerical 16-bit value
- //%l - Numerical 32-bit value
- //%v - Assembler name of a global variable or function
- //%o - Stack offset of a local variable
- //%g - Assembler name of a C label
- //%s - The argument is converted to a string
- //%% - The % sign itself
- //-- SEGMENTS --
- //(Example: #pragma data_seg(Code))
- //Zeropage : $00 -> $fd (virtual; $fe,$ff are reserved for rng and kb input)
- //Stack : $100 -> $1ff (virtual)
- //VideoMemory: $200 -> $5ff (virtual)
- //Code : $600 -> $ffff
- typedef signed char s8;
- typedef signed int s16;
- typedef long signed int s32;
- typedef unsigned char u8;
- typedef unsigned int u16;
- typedef long unsigned int u32;
- typedef union r16 {
- u16 v;
- struct { u8 l, h; };
- } r16;
- #define S8_MIN -128
- #define S16_MIN -32768
- #define S32_MIN -2147483648
- #define S8_MAX 127
- #define S16_MAX 32767
- #define S32_MAX 2147483647
- #define U8_MAX 255
- #define U16_MAX 65535
- #define U32_MAX 4294967295
- #define MSb_8 0x80
- #define MSb_16 0x8000
- #define MSb_32 0x80000000
- #define BITS_OFF8( var,bits) var &= (bits^0xff )
- #define BITS_OFF16(var,bits) var &= (bits^0xffff)
- #define BITS_ON( var,bits) var |= bits;
- #define TO_LOWER(chr) (c|( 0x20))
- #define TO_UPPER(chr) (c&(~0x20))
- //($80-$fd are reserved for compiler usage)
- #define RANDOM (*((char*)0xfe))
- #define KEY (*((char*)0xff))
- #define PIXELS ((char*)0x200)
- #define DISABLE_BRK_PAUSE __asm__("sei")
- #define ENABLE_BRK_PAUSE __asm__("cli")
- #define BREAK_POINT __asm__("brk")
- #define UPDATE_SCREEN __asm__("nop")
- //first 16 colors
- #define COLOR_BLACK 0
- #define COLOR_DRED 1
- #define COLOR_DGREEN 2
- #define COLOR_DYELLOW 3
- #define COLOR_DBLUE 4
- #define COLOR_DMAGENTA 5
- #define COLOR_DCYAN 6
- #define COLOR_LGRAY 7
- #define COLOR_DGRAY 8
- #define COLOR_LRED 9
- #define COLOR_LGREEN 10
- #define COLOR_LYELLOW 11
- #define COLOR_LBLUE 12
- #define COLOR_LMAGENTA 13
- #define COLOR_LCYAN 14
- #define COLOR_WHITE 15
- char g_char0;
- char g_char1;
- char g_char2;
- #endif /* _EZR6502_MISC_H */
- //CC65 VERSION
- //each increment is equal to exactly 1.40625 degrees (about 0.0245436926 radians)
- //precision is about 1/255th (255 instead of 256 because -128 is not used)
- #ifndef _SIN8S_H
- #define _SIN8S_H
- const signed char
- #ifdef SIN8S_MIN //64-wide lookup table
- #ifdef SIN8S_ALIGN
- //__align(64)
- #endif /* ifdef SIN8S_ALIGN */
- _sin8s_table[64] =
- #else //256-wide lookup table
- #ifdef SIN8S_ALIGN
- //__align(256)
- #endif /* ifdef SIN8S_ALIGN */
- _sin8s_table[256] =
- #endif /* ifndef SIN8S_MIN */
- { //(effective range of -127 -> 127)
- 0, 3, 6, 9, 12, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46,
- 49, 51, 54, 57, 60, 63, 65, 68, 71, 73, 76, 78, 81, 83, 85, 88,
- 90, 92, 94, 96, 98, 100, 102, 104, 106, 107, 109, 111, 112, 113, 115, 116,
- 117, 118, 120, 121, 122, 122, 123, 124, 125, 125, 126, 126, 126, 127, 127, 127,
- #ifndef SIN8S_MIN
- //this is redundant if you make the output index 64-(<input index>&63) (result = 127 if <input index>&127 = 64)
- 127, 127, 127, 127, 126, 126, 126, 125, 125, 124, 123, 122, 122, 121, 120, 118,
- 117, 116, 115, 113, 112, 111, 109, 107, 106, 104, 102, 100, 98, 96, 94, 92,
- 90, 88, 85, 83, 81, 78, 76, 73, 71, 68, 65, 63, 60, 57, 54, 51,
- 49, 46, 43, 40, 37, 34, 31, 28, 25, 22, 19, 16, 12, 9, 6, 3,
- //this is redundant if you just flip result if input index is > 127
- 0, -3, -6, -9, -12, -16, -19, -22, -25, -28, -31, -34, -37, -40, -43, -46,
- -49, -51, -54, -57, -60, -63, -65, -68, -71, -73, -76, -78, -81, -83, -85, -88,
- -90, -92, -94, -96, -98, -100, -102, -104, -106, -107, -109, -111, -112, -113, -115, -116,
- -117, -118, -120, -121, -122, -122, -123, -124, -125, -125, -126, -126, -126, -127, -127, -127,
- -127, -127, -127, -127, -126, -126, -126, -125, -125, -124, -123, -122, -122, -121, -120, -118,
- -117, -116, -115, -113, -112, -111, -109, -107, -106, -104, -102, -100, -98, -96, -94, -92,
- -90, -88, -85, -83, -81, -78, -76, -73, -71, -68, -65, -63, -60, -57, -54, -51,
- -49, -46, -43, -40, -37, -34, -31, -28, -25, -22, -19, -16, -12, -9, -6, -3,
- #endif /* ifndef SIN8S_MIN */
- };
- extern char g_char0;
- #ifdef SIN8S_INLINE
- inline
- #endif /* ifdef SIN8S_INLINE */
- signed char __fastcall__ sin8s(const char x){
- g_char0 = x;
- #ifdef SIN8S_MIN //slower, but takes up less space (lookup table included)
- __asm__("lda %v", g_char0);
- __asm__("tay"); //store original value of a in y
- __asm__("and #127"); //a %= 128
- //a = 0-63: index = a
- __asm__("cmp #64"); //branch if a<64
- __asm__("bcc %g", _index_result); //^^
- //a = 65-127: index = 64-(a%64)
- __asm__("cmp #65"); //branch if a<65
- __asm__("bcc %g", _not_65_127); //^^
- __asm__("and #63"); //a %= 64
- __asm__("sta %v", g_char0); //tmp = a
- __asm__("lda #64"); //a = 64
- __asm__("sec"); //a -= tmp
- __asm__("sbc %v", g_char0); //^^
- __asm__("jmp %g", _index_result);
- _not_65_127:
- //result will either be 127 or -127
- //if input was 64 or 192 respectively
- __asm__("lda #127");
- __asm__("jmp %g", _dont_index_result);
- _index_result:
- __asm__("tax" ); //put index into x
- __asm__("lda %v,x", _sin8s_table); //get result
- _dont_index_result:
- __asm__("tax"); //store result into x
- __asm__("tya"); //branch if input was positive
- __asm__("bpl %g", _not_negative); //^^
- __asm__("txa"); //get result back
- __asm__("eor #$ff"); //result = -result (if input was negative)
- __asm__("clc"); //^^
- __asm__("adc #1"); //^^
- __asm__("tax"); //store new result in x
- _not_negative:
- //put (potentially altered) result into return variable
- __asm__("stx %v", g_char0);
- #else
- //faster, but takes up more space (lookup table included)
- __asm__("ldx %v", g_char0);
- __asm__("lda %v,x", _sin8s_table);
- __asm__("sta %v", g_char0);
- #endif /* ifdef SIN8S_MIN */
- return g_char0;
- }
- #define cos8s(_v) (sin8s((_v)+64))
- #endif /* ifndef _SIN8S_H */
- s8 abs8(s8 n){ return (n<0) ? -n : n; }
- u8 getgray_u(u8 n){ return 232+n/11; }
- u8 getgray_s(s8 n){ return 232+abs8(n)/6; }
- /*
- s8 shift_s8r(s8 n, u8 s){
- s8 _n = n>>s;
- //bit extend if negative
- if(n<0) return (0xff<<(8-s)) | _n;
- else return _n;
- }
- */
- #define shift_s8r(_n,_s) ( ((_n)<0) ? (0xff<<(8-(_s)))|(_n>>(_s)) : (_n>>(_s)) )
- /*
- void putpixel(s8 p_x, s8 p_y, u8 color){
- if((p_x&0b11100000) || (p_y&0b11100000)) return;
- PIXELS[p_x | ((int)p_y)<<5] = color;
- }
- */
- #define putpixel(p_x, p_y, color) PIXELS[p_x | ((int)p_y)<<5] = color
- s8 min(s8 a, s8 b){ return (a< b) ? a : b; }
- s8 max(s8 a, s8 b){ return (a>=b) ? a : b; }
- s8 edge_start[32];
- s8 edge_end [32];
- s8 edge_mod [32];
- s8 grayscale[256];
- void main(){
- /*
- s8 p_x0, p_x1, p_x, p_y;
- s8 i0, i1, color;
- s8 shift = 0;
- _START:
- i0 = -65, i1 = -64;
- do {
- p_x0 = 16 + shift_s8r(cos8s(i0), 3);
- p_x1 = 16 + shift_s8r(cos8s(i1), 3);
- p_y = 16 + shift_s8r(sin8s(i1), 3);
- for(p_x = p_x0; p_x <= p_x1; ++p_x){
- color = getgray_s(cos8s(i1+(shift+=3)));
- putpixel(p_x, p_y, color);
- }
- --i0;
- } while(++i1 != 51);
- UPDATE_SCREEN;
- BREAK_POINT;
- goto _START;
- */
- u16 addr;
- s8 p_x0, p_x1, p_x, p_y;
- s8 i0, i1, color;
- s8 shift = 0;
- //_INIT:
- i0 = -65, i1 = -64;
- do {
- p_x0 = 16 + shift_s8r(cos8s(i0), 3);
- p_x1 = 16 + shift_s8r(cos8s(i1), 3);
- p_y = 16 + shift_s8r(sin8s(i1), 3);
- edge_start[p_y] = min(p_x0 , p_x1 );
- edge_end [p_y] = max(p_x0+1, p_x1+1);
- edge_mod [p_y] = i1;
- //(+1 so i can use < when looping through x)
- --i0;
- } while(++i1 != 51);
- //generate grayscale lookup
- i0 = 0;
- do {
- //grayscale[i0] = 16+i0/43;//getgray_s(i0);
- grayscale[i0] = getgray_s(i0);
- } while(++i0);
- _START:
- i1 = -64;
- p_y = 0;
- _LOOPY:
- p_x0 = edge_start[p_y];
- p_x1 = edge_end [p_y];
- addr = p_x0 | ((u16)p_y)<<5;
- for(p_x = p_x0; p_x <= p_x1; ++p_x){
- color = grayscale[ sin8s(i1+(shift+=5)) ];
- PIXELS[addr] = color;
- ++addr;
- }
- ++i1;
- if(++p_y != 32) goto _LOOPY;
- UPDATE_SCREEN;
- //BREAK_POINT;
- goto _START;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement