Advertisement
Kitomas

work for 2025-01-31 (4/4)

Jan 31st, 2025 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 29.55 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"sokoban_and_kit32_2025-01-31\sokoban\src\utils_misc.cpp":
  4. #include <include_all.hpp>
  5.  
  6. #include <filesystem>
  7.  
  8.  
  9. namespace kit { extern size_t numAllocations; }
  10.  
  11. static char errFmt[512];
  12.  
  13.  
  14. namespace fs = std::filesystem;
  15. using namespace kit;
  16.  
  17.  
  18.  
  19.  
  20.  
  21. file_list::file_list(const char* searchPath,
  22.                      u32         signature)
  23. {
  24.   if(valid) return;
  25.  
  26.  
  27.  
  28.   try {
  29.     //get number of files
  30.     files_len = 0;
  31.     for(const auto& entry : fs::recursive_directory_iterator(searchPath))
  32.       if(entry.is_regular_file()) ++files_len;
  33.  
  34.  
  35.  
  36.     //allocate space for file strings
  37.     if(!files_len) return;
  38.     files = (std::string**)memory::alloc2(sizeof(std::string**) * files_len);
  39.  
  40.  
  41.  
  42.     //add files to list
  43.     u32 i=0;
  44.     for(const auto& entry : fs::recursive_directory_iterator(searchPath)){
  45.       if(entry.is_regular_file()){
  46.         if(i >= files_len)
  47.           throw "file count changed in the middle of querying levels directory";
  48.  
  49.         std::string pathstr = entry.path().string();
  50.  
  51.         u32 magic;
  52.         if(File(pathstr.c_str(),"rb").read(&magic,sizeof(magic))<sizeof(magic))
  53.         {
  54.           snPrintf(errFmt, sizeof(errFmt),
  55.                    "failed to read file signature of level \"%s\"",
  56.                    pathstr.c_str());
  57.           throw (const char*)errFmt;
  58.         }
  59.  
  60.         if(signature != 0  &&  magic != signature) continue;
  61.  
  62.         files[i++] = new std::string(pathstr);
  63.         ++numAllocations;
  64.  
  65.       }
  66.  
  67.     }
  68.  
  69.  
  70.  
  71.     //trim any unused space
  72.     files_len = i;
  73.     memory::realloc2(&files, sizeof(std::string**) * files_len);
  74.  
  75.  
  76.  
  77.   } catch(const char* errorText){
  78.     throw errorText;
  79.   } catch(...){ //catch(const fs::filesystem_error& _){
  80.     throw "failed to query file list";
  81.   }
  82.  
  83.  
  84.  
  85.   valid = true;
  86.  
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93. file_list::~file_list(){
  94.   if(!valid) return;
  95.   valid = false;
  96.  
  97.   if(files == nullptr) return;
  98.  
  99.   for(u32 i=0; i<files_len; ++i){
  100.     NULLDELETE(files[i], std::string);
  101.     --numAllocations;
  102.   }
  103.  
  104.   memory::free(&files);
  105.   files_len = 0;
  106.  
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113. void file_list::draw(s32 x, s32 y, u32 max_len,
  114.                      u32 first, u32 last)
  115. {
  116.   if(last == KIT_U32_MAX) last = files_len-1;
  117.  
  118.   bool neg_pos_margin = text->neg_pos_margin;
  119.   text->neg_pos_margin = false;
  120.  
  121.  
  122.  
  123.   for(u32 i=0,n=first; n<=last; ++i,++n){
  124.     std::string* name = files[n];
  125.     s32 end = name->length();
  126.  
  127.     for(s32 c=end-1; c>=0; --c){
  128.       if((*name)[c] == '.'){
  129.         end = c; break;
  130.       }
  131.     }
  132.  
  133.     s32 start = end-(max_len);
  134.     s32 len   = end-start;
  135.  
  136.     bool longName = start > 0;
  137.     if(longName){
  138.       start += 3;
  139.       len   -= 3;
  140.     }
  141.  
  142.     start = MAX(start, 0);
  143.     len   = MAX(len,   0);
  144.     text->draw(x, y + i*8*text->scale.y,
  145.                "%s%s", 0, (longName) ? "..." : "",
  146.                name->substr(start, len).c_str());
  147.  
  148.   }
  149.  
  150.  
  151.  
  152.   text->neg_pos_margin = neg_pos_margin;
  153.  
  154. }
  155. /******************************************************************************/
  156. /******************************************************************************/
  157. //"sokoban_and_kit32_2025-01-31\sokoban\src\utils_tile.cpp":
  158. #include <include_all.hpp>
  159.  
  160.  
  161. using namespace kit;
  162.  
  163.  
  164.  
  165.  
  166.  
  167. #define UV_STEP(_amount) ((1.0f/16)*(_amount))
  168.  
  169.  
  170.  
  171.  
  172.  
  173. shape::fpoint tile_camera = {0, 0};
  174. s32           tile_layer = 32;
  175.  
  176. u32 player_which = TILE_ID_PLAYER_BACK;
  177.  
  178. Texture* tile_tex = nullptr;
  179. Vertex   tile_verts[4];
  180. s32      tile_indices[6];
  181.  
  182. void tile_init(){ //inits all but tile_tex itself
  183.   static bool isInit = false;
  184.   if(isInit) return;
  185.  
  186.   clrs::ABGR color = 0xFFFFFFFF;
  187.   tile_verts[0].c = color;
  188.   tile_verts[1].c = color;
  189.   tile_verts[2].c = color;
  190.   tile_verts[3].c = color;
  191.  
  192.   tile_verts[0].v = 0.0f;
  193.   tile_verts[1].v = 0.0f;
  194.   tile_verts[2].v = 1.0f;
  195.   tile_verts[3].v = 1.0f;
  196.  
  197.   tile_indices[0] = 0;
  198.   tile_indices[1] = 1;
  199.   tile_indices[2] = 2;
  200.   tile_indices[3] = 3;
  201.   tile_indices[4] = 1;
  202.   tile_indices[5] = 2;
  203.  
  204.   isInit = true;
  205.  
  206. }
  207.  
  208.  
  209.  
  210.  
  211.  
  212. ATTR_ALWAYS_INLINE
  213. static inline void _draw_tile(s32 x, s32 y, s32 z, u32 which){
  214.   tile_verts[0].x  = 16*x + tile_camera.x;
  215.   tile_verts[0].y  =  8*x + tile_camera.y;
  216.  
  217.   tile_verts[0].y -= 16*y;
  218.  
  219.   tile_verts[0].x += 16*z;
  220.   tile_verts[0].y +=  8*z;
  221.  
  222.  
  223.   tile_verts[1].x = tile_verts[0].x + 32;
  224.   tile_verts[1].y = tile_verts[0].y;
  225.  
  226.   tile_verts[2].x = tile_verts[0].x;
  227.   tile_verts[2].y = tile_verts[0].y + 32;
  228.  
  229.   tile_verts[3].x = tile_verts[1].x;
  230.   tile_verts[3].y = tile_verts[2].y;
  231.  
  232.  
  233.   tile_verts[0].u = UV_STEP(which);
  234.   tile_verts[1].u = tile_verts[0].u + UV_STEP(1);
  235.   tile_verts[2].u = tile_verts[0].u;
  236.   tile_verts[3].u = tile_verts[1].u;
  237.  
  238.  
  239.   rndr->renderGeometry(tile_verts, 4, tile_tex, tile_indices, 6);
  240.  
  241. }
  242.  
  243. //4 to make them appear as 8x8; 8 to appear as 4x4
  244. #define TINY_SCALE(_normal_size) ((_normal_size)/8)
  245.  
  246. static inline void _draw_tile_tiny(s32 x, s32 y, s32 z, u32 which){
  247.   tile_verts[0].x  = TINY_SCALE(16)*x + tile_camera.x;
  248.   tile_verts[0].y  = TINY_SCALE( 8)*x + tile_camera.y;
  249.  
  250.   tile_verts[0].y -= TINY_SCALE(16)*y;
  251.  
  252.   tile_verts[0].x += TINY_SCALE(16)*z;
  253.   tile_verts[0].y += TINY_SCALE( 8)*z;
  254.  
  255.  
  256.   tile_verts[1].x = tile_verts[0].x + TINY_SCALE(32);
  257.   tile_verts[1].y = tile_verts[0].y;
  258.  
  259.   tile_verts[2].x = tile_verts[0].x;
  260.   tile_verts[2].y = tile_verts[0].y + TINY_SCALE(32);
  261.  
  262.   tile_verts[3].x = tile_verts[1].x;
  263.   tile_verts[3].y = tile_verts[2].y;
  264.  
  265.  
  266.   tile_verts[0].u = UV_STEP(which);
  267.   tile_verts[1].u = tile_verts[0].u + UV_STEP(1);
  268.   tile_verts[2].u = tile_verts[0].u;
  269.   tile_verts[3].u = tile_verts[1].u;
  270.  
  271.  
  272.   rndr->renderGeometry(tile_verts, 4, tile_tex, tile_indices, 6);
  273.  
  274. }
  275.  
  276.  
  277.  
  278.  
  279.  
  280. void draw_tile(s32 x, s32 y, s32 z, u32 which, f32 alpha){
  281.   u8 alpha_u8 = 255*alpha;
  282.   tile_verts[0].c.a = alpha_u8;
  283.   tile_verts[1].c.a = alpha_u8;
  284.   tile_verts[2].c.a = alpha_u8;
  285.   tile_verts[3].c.a = alpha_u8;
  286.  
  287.   _draw_tile(x, y, z, which);
  288.  
  289. }
  290.  
  291.  
  292.  
  293.  
  294.  
  295. void draw_tiles(s32 size_x, s32 size_y, s32 size_z, tile_array* tiles){
  296.   if(tiles == nullptr) return;
  297.   u32 i = 0;
  298.   size_y = MIN(size_y, tile_layer);
  299.  
  300.   for(s32 y=0; y<size_y; ++y)
  301.   for(s32 x=0; x<size_x; ++x)
  302.   for(s32 z=0; z<size_z; ++z)
  303.   {
  304.     tile_array tle = tiles[i++];
  305.  
  306.     if(tle.tile){
  307.       u8 alpha = (tle.value&0b10000000) | 0b01111111;
  308.       tile_verts[0].c.a = alpha;
  309.       tile_verts[1].c.a = alpha;
  310.       tile_verts[2].c.a = alpha;
  311.       tile_verts[3].c.a = alpha;
  312.  
  313.       _draw_tile_tiny(x, y, z, tle.tile);
  314.  
  315.     }
  316.  
  317.     if(tle.has_player){
  318.       tile_verts[0].c.a = 255;
  319.       tile_verts[1].c.a = 255;
  320.       tile_verts[2].c.a = 255;
  321.       tile_verts[3].c.a = 255;
  322.  
  323.       _draw_tile_tiny(x, y, z, player_which);
  324.  
  325.     }
  326.  
  327.   }
  328.  
  329. }
  330.  
  331.  
  332.  
  333.  
  334.  
  335. void draw_tiles_tiny(s32 size_x, s32 size_y, s32 size_z, tile_array* tiles){
  336.   if(tiles == nullptr) return;
  337.   u32 i = 0;
  338.   size_y = MIN(size_y, tile_layer);
  339.  
  340.   for(s32 y=0; y<size_y; ++y)
  341.   for(s32 x=0; x<size_x; ++x)
  342.   for(s32 z=0; z<size_z; ++z)
  343.   {
  344.     tile_array tle = tiles[i++];
  345.  
  346.     if(tle.tile){
  347.       u8 alpha = (tle.value&0b10000000) | 0b01111111;
  348.       tile_verts[0].c.a = alpha;
  349.       tile_verts[1].c.a = alpha;
  350.       tile_verts[2].c.a = alpha;
  351.       tile_verts[3].c.a = alpha;
  352.  
  353.       _draw_tile_tiny(x, y, z, tle.tile);
  354.  
  355.     }
  356.  
  357.     if(tle.has_player){
  358.       tile_verts[0].c.a = 255;
  359.       tile_verts[1].c.a = 255;
  360.       tile_verts[2].c.a = 255;
  361.       tile_verts[3].c.a = 255;
  362.  
  363.       _draw_tile_tiny(x, y, z, player_which);
  364.  
  365.     }
  366.  
  367.   }
  368.  
  369. }
  370. /******************************************************************************/
  371. /******************************************************************************/
  372. //"sokoban_and_kit32_2025-01-31\kit32\simulator\include\include_all.hpp":
  373. #ifndef _INCLUDE_ALL_HPP
  374. #define _INCLUDE_ALL_HPP
  375.  
  376. #include <kit/all.hpp>
  377.  
  378. #define loghere kit_LogInfo("%s: %3i", __FILE__, __LINE__);
  379.  
  380. /********************************* "main.cpp" *********************************/
  381.  
  382. #define LOGI_W (256)
  383. #define LOGI_H (144)
  384. #define WIN_MUL (4)
  385.  
  386. //(window is hidden initially, use setVisibility() to change that)
  387. #define WIN_TITLE "kit-32 simulator"
  388. #define WIN_W     (LOGI_W*WIN_MUL)
  389. #define WIN_H     (LOGI_H*WIN_MUL)
  390. #define WIN_FLAGS 0
  391.  
  392. #define TEXTCOLORED_LEN 16
  393.  
  394. extern kit::Window*        wndw;
  395. extern kit::Renderer*      rndr;
  396. extern kit::BFont_Texture* text;
  397. extern kit::BFont_Texture* textColored[TEXTCOLORED_LEN];
  398.  
  399. kit::f64 frand  (); // 0.0  -> 1.0
  400. kit::f64 frand2 (); //-1.0  -> 1.0
  401. kit::f32 frandf (); // 0.0f -> 1.0f
  402. kit::f32 frandf2(); //-1.0f -> 1.0f
  403.  
  404. //only uses a single call to std::rand()
  405. kit::f32 frandf_b (); // 0.0f -> 1.0f
  406. kit::f32 frandf2_b(); //-1.0f -> 1.0f
  407.  
  408. /****************************** "callbacks.cpp" *******************************/
  409.  
  410. //...
  411.  
  412. /****************************** "utils_core.cpp" ******************************/
  413.  
  414. //...
  415.  
  416. /****************************** "kit_32_cpu.cpp" ******************************/
  417.  
  418. #include "kit_32_cpu.hpp"
  419.  
  420. /******************************************************************************/
  421.  
  422. #endif /* _INCLUDE_ALL_HPP */
  423. /******************************************************************************/
  424. /******************************************************************************/
  425. //"sokoban_and_kit32_2025-01-31\kit32\simulator\include\kit_32_cpu.hpp":
  426. #ifndef _KIT_32_CPU_HPP
  427. #define _KIT_32_CPU_HPP
  428.  
  429. #include <kit/commondef.hpp>
  430.  
  431. namespace kit {
  432.  
  433. //must be a power of 2, minus 1
  434. #define ADDRESS_LIMIT (256*256*256 - 1) //for 16MiB
  435.  
  436. #define MEMSIZE (ADDRESS_LIMIT+1)
  437. #define MEMSIZEh (MEMSIZE/2)
  438.  
  439. #define ADDR(_type, _byte) \
  440.   ( (_type *)(memory+((_byte)&ADDRESS_LIMIT)) )
  441.  
  442.  
  443.  
  444.  
  445.  
  446. /* TWOS' COMPLEMENT COMPARISON CHEAT SHEET:
  447.  
  448.   un/signed !=: !zero
  449.   un/signed ==:  zero
  450.  
  451.   unsigned < : !carry
  452.   unsigned > :  carry && !zero
  453.   unsigned <=: !carry ||  zero
  454.   unsigned >=:  carry
  455.  
  456.   signed < :  negative
  457.   signed > : !negative && !zero
  458.   signed <=:  negative ||  zero
  459.   signed >=: !negative
  460.  
  461. */
  462.  
  463. #define CMP_ZC (!flags_zero)  // !=
  464. #define CMP_ZS ( flags_zero)  // ==
  465.  
  466. #define CMP_NE (!flags_equal) // !=
  467. #define CMP_EQ ( flags_equal) // ==
  468.  
  469. #define CMP_LT ( flags_less                )  // <  (sign agnostic)
  470. #define CMP_GT (!flags_less && !flags_equal)  // >  (sign agnostic)
  471. #define CMP_LE ( flags_less ||  flags_equal)  // <= (sign agnostic)
  472. #define CMP_GE (!flags_less                )  // >= (sign agnostic)
  473.  
  474. #define CMP_LT_U (!flags_carry               )  // <  (unsigned)
  475. #define CMP_GT_U ( flags_carry && !flags_zero)  // >  (unsigned)
  476. #define CMP_LE_U (!flags_carry ||  flags_zero)  // <= (unsigned)
  477. #define CMP_GE_U ( flags_carry               )  // >= (unsigned)
  478.  
  479. #define CMP_LT_S ( flags_negative               )  // <  (signed)
  480. #define CMP_GT_S (!flags_negative && !flags_zero)  // >  (signed)
  481. #define CMP_LE_S ( flags_negative ||  flags_zero)  // <= (signed)
  482. #define CMP_GE_S (!flags_negative               )  // >= (signed)
  483.  
  484.  
  485.  
  486.  
  487.  
  488. enum opdata_enum {
  489.   OPDATA_U8  = 0b000 << 5,
  490.   OPDATA_S8  = 0b100 << 5,
  491.   OPDATA_U16 = 0b001 << 5,
  492.   OPDATA_S16 = 0b101 << 5,
  493.   OPDATA_U32 = 0b010 << 5,
  494.   OPDATA_S32 = 0b110 << 5,
  495.   OPDATA_IMP = 0b011 << 5, //reserved for op types with implied data types
  496.   OPDATA_F32 = 0b111 << 5,
  497.  
  498.   OPDATA_MASK = OPDATA_F32,
  499. };
  500.  
  501.  
  502.  
  503. //operations with implied data types (if any are even applicable)
  504. enum optypes_implied_enum {
  505.   OPTYPE_NOP, // 0: no operation
  506.  
  507.   OPTYPE_SRT, // 1: D = sqrtf(S)
  508.  
  509.   OPTYPE_CPU, // 2: cpuid basically, u16 implied
  510.   OPTYPE_SYS, // 3: system call; u16 implied
  511.  
  512.   //BRANCHES HAVE IMPLIED S16 TYPE
  513.   OPTYPE_BNC, // 4: branch to source if !negative
  514.   OPTYPE_BNS, // 5: branch to source if  negative
  515.   OPTYPE_BZC, // 6: branch to source if !zero
  516.   OPTYPE_BZS, // 7: branch to source if  zero
  517.    //the thing about these 4 ops (and the jump equivalents) is that
  518.     //these are only useful if preceeded by a cmp operation.
  519.     //the comparison between D[estination] and S[ource] refers to that cmp,
  520.     //not the branch (otherwise the source operands would conflict!)
  521.   OPTYPE_BLT, // 8: branch to source if D <  S
  522.   OPTYPE_BGT, // 9: branch to source if D >  S
  523.   OPTYPE_BLE, //10: branch to source if D <= S
  524.   OPTYPE_BGE, //11: branch to source if D >= S
  525.   OPTYPE_BNE, //12: branch to source if D != S
  526.   OPTYPE_BEQ, //13: branch to source if D == S
  527.  
  528.   //CONDITIONAL JUMPS HAVE IMPLIED U32 TYPE
  529.   OPTYPE_JNC, //14: jump to source if !negative
  530.   OPTYPE_JNS, //15: jump to source if  negative
  531.   OPTYPE_JZC, //16: jump to source if !zero (AKA bne, !=)
  532.   OPTYPE_JZS, //17: jump to source if  zero (AKA beq, ==)
  533.   OPTYPE_JLT, //18: jump to source if D <  S
  534.   OPTYPE_JGT, //19: jump to source if D >  S
  535.   OPTYPE_JLE, //20: jump to source if D <= S
  536.   OPTYPE_JGE, //21: jump to source if D >= S
  537.   OPTYPE_JNE, //22: jump to source if D != S
  538.   OPTYPE_JEQ, //23: jump to source if D == S
  539.  
  540.   OPTYPE_U2B, //24: u32-to-binary-coded-decimal
  541.   OPTYPE_B2U, //25: binary-coded-decimal-to-u32
  542.   OPTYPE_I2F, //26: s32-to-float cast
  543.   OPTYPE_F2I, //27: float-to-s32 cast
  544.  
  545.   OPTYPE_RTS, //28: return from subroutine
  546.  
  547.   OPTYPE_BRA, //29: branch always
  548.  
  549.   OPTYPE_RSI, //30: return from subroutine with custom increment (s16 type)
  550.  
  551.   OPTYPE_RFN, //31: return from function; rts, but pull r5 -> r1 (not r0)
  552.  
  553. };
  554.  
  555.  
  556.  
  557. //operations without implied data types
  558. enum optypes_normal_enum {
  559.   //misc
  560.   OPTYPE_BRK, // 0:  (tbd)
  561.   OPTYPE_MOV, // 1:  move data
  562.   OPTYPE_CMP, // 2: compare data
  563.   OPTYPE_JMP, // 3: unconditional jump to source
  564.   OPTYPE_JSR, // 4: jump to subroutine in source
  565.   OPTYPE_SLD, // 5: load to dst from stack offset (s16 src offset, rel. to sp)
  566.   OPTYPE_SST, // 6: store dst to stack offset  (s16 src, relative to sp)
  567.   OPTYPE_SPH, // 7: push s to stack
  568.   OPTYPE_SPL, // 8: pull from stack into s
  569.  
  570.   //arithmetic
  571.   OPTYPE_INC, // 9: ++D
  572.   OPTYPE_DEC, //10: --D
  573.   OPTYPE_ADD, //11: D += S
  574.   OPTYPE_SUB, //12: D -= S
  575.   OPTYPE_MUL, //13: D *= S
  576.   OPTYPE_DIV, //14: D /= S
  577.   OPTYPE_MOD, //15: D %= S
  578.   OPTYPE_NEG, //16: D = -S
  579.  
  580.   //bitwise (not available for f32 types)
  581.   OPTYPE_NOT, //17: bitwise not          (~)
  582.   OPTYPE_NND, //18: bitwise nand         (& ~)
  583.   OPTYPE_AND, //19: bitwise and          (&)
  584.   OPTYPE_NOR, //20: bitwise nor          (| ~)
  585.   OPTYPE_IOR, //21: bitwise inclusive or (|)
  586.   OPTYPE_XOR, //22: bitwise exclusive or (^)
  587.   OPTYPE_SHL, //23: shift left           (<<)
  588.   OPTYPE_SHR, //24: shift right          (>>)
  589.   OPTYPE_ROL, //25: rotate left (technically redundant)
  590.   OPTYPE_ROR, //26: rotate right
  591.   OPTYPE_EXT, //27: sign extend; 8-bit to 16-bit, or 16-bit to 32-bit (no floats!)
  592.  
  593.   OPTYPE_JFN, //28: jump to function; jsr, but push r1 -> r5 (not r0)
  594.  
  595.   //(there's space for 3 more operation types here)
  596.  
  597. };
  598.  
  599.  
  600.  
  601. #define OPCODE(_op_data, _op_type) \
  602.   ( OPDATA_##_op_data | OPTYPE_##_op_type)
  603.  
  604.  
  605.  
  606. //addressing modes
  607. enum opaddr_enum {
  608.   //general-purpose registers
  609.   OPADDR_GP0 = 0,
  610.   OPADDR_GP1 = 1,
  611.   OPADDR_GP2 = 2,
  612.   OPADDR_GP3 = 3,
  613.   OPADDR_GP4 = 4,
  614.   OPADDR_GP5 = 5,
  615.  
  616.   //stack pointer
  617.   OPADDR_SP = 6,
  618.  
  619.   //immediate value (source only)
  620.   OPADDR_IMM = 7,
  621.   //flags register (destination only)
  622.   OPADDR_FLAGS = 7,
  623.  
  624. };
  625.  
  626.  
  627.  
  628. enum execute_result_enum {
  629.   EXERES_SUCCESS = 0, //no error
  630.   EXERES_BRK     = 1, //brk was invoked
  631.   EXERES_NOP     = 2, //nop was invoked
  632.   EXERES_ILLEGAL = 3, //attempted to execute an illegal opcode
  633. };
  634.  
  635.  
  636.  
  637.  
  638.  
  639. /*
  640.  
  641.   |FEDCBA9876543210|FEDCBA98-76543210|
  642.   /----------------------------------\
  643.   |VVVVVVVVVVVVVVVV|dDDDsSSS-NTTOOOOO|
  644.   \----------------------------------/
  645.   V: if applicable, and T <= 1, operand value will be stored here
  646.      instead of in the next word (32-bits)
  647.   d: 'is dst value indirect?', or 'dereference u32 value in dst?'
  648.   D: destination register; 0-5 = general purpose, 6 = stack ptr, 7 = flags
  649.   s: 'is src value indirect?', or 'dereference u32 value in src?'
  650.   S: source register; 0-5 = general purpose, 6 = stack ptr, 7 = immediate value
  651.      (immediate value isn't actually a cpu register,
  652.       rather it's a value within the full opcode itself)
  653.   N: 'data is signed?'
  654.   T: data type; 0,1,2,3 = char,short,int,float
  655.   O: operation type
  656.  
  657.   behavior of D and S being equal is undefined unless explicitly supported
  658.     (this can be checked with the CPU operation!)
  659.   dereferencing values not of type u32 is undefined
  660.   behavior of misaligned instructions (not a multiple of 4 bytes) is undefined
  661.  
  662.   if N and TT make 011, an unsigned float
  663.   (which isn't standard, if it exists at all),
  664.   OOOOO will be a separate line operation type category
  665.  
  666. */
  667.  
  668. union ins32 { //instruction; 4B
  669.   u32 all;
  670.  
  671.   struct {
  672.     //opcode; 1B
  673.     union {
  674.       u8 op;
  675.       struct {
  676.         u8 op_type  : 5;
  677.         u8 op_dtype : 2; //operand data type; 0,1,2,3 = char,short,int,float
  678.         u8 op_dsign : 1; //'is data type signed?'
  679.       };
  680.     };
  681.  
  682.     //source and destination; 1B
  683.     struct {
  684.       u8 src     : 3;
  685.       u8 src_ind : 1;
  686.       u8 dst     : 3;
  687.       u8 dst_ind : 1;
  688.     };
  689.  
  690.     //operand value (if op_data <= 1); 2B
  691.     union {
  692.       u8  v_u8;
  693.       s8  v_s8;
  694.       u16 v_u16;
  695.       s16 v_s16;
  696.     };
  697.  
  698.   };
  699.  
  700. };
  701.  
  702.  
  703.  
  704.  
  705.  
  706. //"kit::" explicitly used in type to make reading it a bit less confusing
  707. union reg32 { //register
  708.   kit::u8   u8;
  709.   kit::s8   s8;
  710.  
  711.   kit::u16  u16;
  712.   kit::s16  s16;
  713.  
  714.   kit::u32  u32;
  715.   kit::s32  s32;
  716.  
  717.   kit::f32  f32;
  718.  
  719.   ins32 ins; //may or may not use this
  720.  
  721. };
  722.  
  723.  
  724.  
  725.  
  726.  
  727. struct cpu32 { //cpu state; 56B
  728.   union {
  729.     reg32 regs[8];
  730.     struct {
  731.       reg32 regs_gp[6];
  732.  
  733.       reg32 regs_sp;
  734.  
  735.       union {
  736.         reg32 regs_flags;
  737.         struct {
  738.           bool  flags_less     : 1; //these two are only set by cmp
  739.           bool  flags_equal    : 1;  //
  740.           bool _flags_padding0 : 2;
  741.           bool  flags_zero     : 1;
  742.           bool _flags_padding1 : 2;
  743.           bool  flags_negative : 1;
  744.         };
  745.       };
  746.  
  747.     };
  748.  
  749.   };
  750.  
  751.   u32 regs_pc, _;
  752.  
  753.   u64 ticksStart = 0;
  754.  
  755.   u8* memory = nullptr;
  756.  
  757.  
  758.  
  759.   //(memory should be padded by at least 8B beyond its
  760.    //max address value to prevent any possible overstepping!)
  761.   s32 execute(); //advances program counter automatically
  762.  
  763.   void cpuid(u16 type);
  764.  
  765.   void syscall(u16 type);
  766.  
  767. };
  768.  
  769.  
  770.  
  771.  
  772.  
  773. enum syscall_enum {
  774.   SYSCALL_TAN,    // 0: r0.f32 = tanf(r0.f32)
  775.   SYSCALL_COSSIN, // 1: r0.f32 = cosf(r0.f32), r1.f32 = sinf(r1.f32)
  776.   SYSCALL_COS,    // 2: r0.f32 = cosf(r0.f32)
  777.   SYSCALL_SIN,    // 3: r1.f32 = sinf(r1.f32)
  778.  
  779.   SYSCALL_MEMSET, // 4: memory::set ((void*)r0.u32, r1.u8 , r2.u32)
  780.   SYSCALL_MEMCPY, // 5: memory::copy((void*)r0.u32, (void*)r1.u32, r2.u32)
  781.  
  782.   SYSCALL_SETDRAWCOLOR, // 6: rndr->setDrawColor(r0.u32)
  783.  
  784.   SYSCALL_DRAWPOINT_S32, // 7: rndr->drawPoint(r0.s32, r1.s32)
  785.   SYSCALL_DRAWPOINT_F32, // 8: rndr->drawPoint(r0.f32, r1.f32)
  786.   SYSCALL_DRAWLINE_S32,  // 9: rndr->drawLine(r0.s32, r1.s32, r2.s32, r3.s32)
  787.   SYSCALL_DRAWLINE_F32,  //10: rndr->drawLine(r0.f32, r1.f32, r2.f32, r3.f32)
  788.   SYSCALL_DRAWRECT_S32,  //11: rndr->drawRects(&{r0.s32, r1.s32, r2.s32, r3.s32}, 1)
  789.   SYSCALL_DRAWRECT_F32,  //12: rndr->drawRects(&{r0.f32, r1.f32, r2.f32, r3.f32}, 1)
  790.   SYSCALL_FILLRECT_S32,  //13: rndr->fillRects(&{r0.s32, r1.s32, r2.s32, r3.s32}, 1)
  791.   SYSCALL_FILLRECT_F32,  //14: rndr->fillRects(&{r0.f32, r1.f32, r2.f32, r3.f32}, 1)
  792.  
  793.   SYSCALL_DRAWPOINTS_S32, //15: rndr->drawPoints((shape::point *)r0.u32, r1.u32)
  794.   SYSCALL_DRAWPOINTS_F32, //16: rndr->drawPoints((shape::fpoint*)r0.u32, r1.u32)
  795.   SYSCALL_DRAWLINES_S32,  //17: rndr->drawLines((shape::point *)r0.u32, r1.u32)
  796.   SYSCALL_DRAWLINES_F32,  //18: rndr->drawLines((shape::fpoint*)r0.u32, r1.u32)
  797.   SYSCALL_DRAWRECTS_S32,  //19: rndr->drawRects((shape::rect *)r0.u32, r1.u32)
  798.   SYSCALL_DRAWRECTS_F32,  //20: rndr->drawRects((shape::frect*)r0.u32, r1.u32)
  799.   SYSCALL_FILLRECTS_S32,  //21: rndr->fillRects((shape::rect *)r0.u32, r1.u32)
  800.   SYSCALL_FILLRECTS_F32,  //22: rndr->fillRects((shape::frect*)r0.u32, r1.u32)
  801.  
  802.   SYSCALL_CLEAR,   //23: rndr->clear()
  803.   SYSCALL_PRESENT, //24: rndr->present(), then wait for next frame
  804.  
  805.   SYSCALL_RAND_U8,  //25: r0.u8  = (u8 )(frandf_b()*KIT_U8_MAX )
  806.   SYSCALL_RAND_U16, //26: r0.u16 = (u16)(frandf_b()*KIT_U16_MAX)
  807.   SYSCALL_RAND_U32, //27: r0.u32 = (u32)(frandf()*KIT_U32_MAX)
  808.   SYSCALL_RAND_F32, //28: r0.f32 = frandf()
  809.  
  810.   //29: r0.u32 = key32 if a key event was in the queue, or 0 if queue is empty
  811.   SYSCALL_GETKEY,
  812.  
  813.   //30: text->draw(r0.s32, r1.s32, r2.u32, r3.u32&KIT_S32_MAX)
  814.   SYSCALL_DRAWTEXT,
  815.  
  816. };
  817.  
  818.  
  819.  
  820.  
  821.  
  822. union key32 { //4B
  823.   u32 value = 0;
  824.   struct {
  825.     u32 vkey    : 10; //MSb is the _KIT_PKEY_MASK of that virtual keycode
  826.     u32 pkey    :  9; //physical keycode
  827.     u32 keymods : 11; //Event_Key_Mod without the 4 unused bits, or scroll lock
  828.     u32 repeat  :  1; //'is this event the result of holding a key down?'
  829.     u32 pressed :  1; //'is key down event?' (key up otherwise)
  830.   };
  831. };
  832.  
  833. static inline key32 convert_key(Event_Key evt){
  834.   key32 result;
  835.  
  836.   result.vkey  = evt.vkey&511; //get first 9 bits of vkey
  837.   result.vkey |= (evt.vkey&_KIT_PKEY_MASK) ? 512 : 0; //set bit 9 if(pkey_mask)
  838.  
  839.   result.pkey = evt.pkey&511; //the &511 *should* be redundant here
  840.  
  841.   #define keymod evt.sym.kmod
  842.   result.keymods  = 0;
  843.   result.keymods |= keymod.mode  ;  result.keymods <<= 1;
  844.   result.keymods |= keymod.caps  ;  result.keymods <<= 1;
  845.   result.keymods |= keymod.num   ;  result.keymods <<= 1;
  846.   result.keymods |= keymod.rgui  ;  result.keymods <<= 1;
  847.   result.keymods |= keymod.lgui  ;  result.keymods <<= 1;
  848.   result.keymods |= keymod.ralt  ;  result.keymods <<= 1;
  849.   result.keymods |= keymod.lalt  ;  result.keymods <<= 1;
  850.   result.keymods |= keymod.rctrl ;  result.keymods <<= 1;
  851.   result.keymods |= keymod.lctrl ;  result.keymods <<= 1;
  852.   result.keymods |= keymod.rshift;  result.keymods <<= 1;
  853.   result.keymods |= keymod.lshift;
  854.  
  855.   result.repeat  = evt.repeat;
  856.   result.pressed = evt.pressed;
  857.  
  858.   return result;
  859.  
  860. }
  861.  
  862.  
  863.  
  864.  
  865.  
  866. }; /* namespace kit */
  867.  
  868. #endif /* _KIT_32_CPU_HPP */
  869. /******************************************************************************/
  870. /******************************************************************************/
  871. //"sokoban_and_kit32_2025-01-31\sokoban\include\include_all.hpp":
  872. #ifndef _INCLUDE_ALL_HPP
  873. #define _INCLUDE_ALL_HPP
  874.  
  875. #include <kit/all.hpp>
  876. #include <kit/Xmp.hpp>
  877.  
  878. #define loghere kit_LogInfo("%s: %3i", __FILE__, __LINE__);
  879.  
  880. #define countof(_thing, _type) (sizeof(_thing)/sizeof(_type))
  881.  
  882. /********************************* "main.cpp" *********************************/
  883.  
  884. #define GAME_NAME "IsoZooid"
  885. #define RELEASE_VERSION      1
  886. #define RELEASE_VERSION_STR "1"
  887.  
  888. //(window is hidden initially, use setVisibility() to change that)
  889. #define WIN_TITLE \
  890.   GAME_NAME " r" RELEASE_VERSION_STR \
  891.   "  (F11 or ALT+Return to enter fullscreen mode)"
  892. #define WIN_W     1280
  893. #define WIN_H     720
  894. #define WIN_FLAGS WINFLAG_RESIZABLE
  895.  
  896. #define FULLSCREEN_MODE 2
  897.  
  898. //logical width and height
  899. #define LOGI_W (WIN_W/2) //=640
  900. #define LOGI_H (WIN_H/2) //=360
  901.  
  902. #define SFXPLAY(_name, _vol, _speed) \
  903.   sfx->play(*sfxData[_name], (_vol), (_vol), (_speed))
  904.  
  905. #define SFX_TRACKS 64
  906.  
  907. enum sfxData_names {
  908.   SFXNAME_BLIP1,
  909.   SFXNAME_CHK1,
  910.  
  911.   SFXDATA_LEN,
  912. };
  913.  
  914. #define SETTINGS_VERSION 3
  915. #define SETTINGS_PATH "settings.bin"
  916.  
  917. struct settings_t {
  918.   kit::u32   version;
  919.   kit::f32 music_vol;
  920.   kit::f32   sfx_vol;
  921.   kit::s32  selected; //selected level index
  922.   bool    fullscreen;
  923.   struct {
  924.   } binds;
  925. };
  926.  
  927. extern settings_t settings;
  928.  
  929. extern kit::AudioData* sfxData[SFXDATA_LEN];
  930.  
  931. extern kit::AudioDevice*   audio;
  932. extern kit::Window*        wndw;
  933. extern kit::Renderer*      rndr;
  934. #define TEXTCOLORED_LEN 16
  935. extern kit::BFont_Texture* text;
  936. extern kit::BFont_Texture* textColored[TEXTCOLORED_LEN];
  937.  
  938. #define GRAY(_hexvalue) 0xFF##_hexvalue##_hexvalue##_hexvalue
  939.  
  940. //if fade delta is 1/20th, then it will take 2/20ths of a second
  941.  //for a new song to fully fade-in (unless index is <0 of course)
  942.  
  943. void music_play(kit::s32 index = -1); //<0 to stop music
  944. void music_setFadeDelta(kit::f32 fadeTimeSeconds = 0.5f);
  945.  
  946. kit::f64 frand  (); // 0.0f -> 1.0f
  947. kit::f64 frand2 (); //-1.0f -> 1.0f
  948. kit::f32 frandf (); // 0.0f -> 1.0f
  949. kit::f32 frandf2(); //-1.0f -> 1.0f
  950.  
  951. void settings_load();
  952. void settings_save();
  953.  
  954. kit::s32 default_event_handler(kit::Event& evt);
  955.  
  956. kit::u32 unit_to_ryg(kit::f32 unit); //unit should be between 0.0f and 1.0f
  957.  
  958. kit::u32 hsv2rgb(kit::f32 H, kit::f32 S, kit::f32 V); //stolen with love
  959.  
  960. template <typename T>
  961. static inline T closer_to_0(T x,T y){ return (x>=0) ? MAX(x-y,0) : MIN(x+y,0); }
  962.  
  963. /****************************** "callbacks.cpp" *******************************/
  964.  
  965. #define MUSIC_INDEX_musicbox1_intro 0
  966. #define MUSIC_INDEX_musicbox1       1
  967.  
  968. extern kit::SoundEngine* sfx;
  969.  
  970. extern kit::s32 music_index;
  971.  
  972. /****************************** "utils_tile.cpp" ******************************/
  973.  
  974. #define TILE_ID_NULL     0
  975.  
  976. #define TILE_ID_BOXSMALL 1
  977. #define TILE_ID_BOXBIG   2
  978.  
  979. #define TILE_ID_GOAL     3
  980.  
  981. #define TILE_ID_BLOCK_R  4
  982. #define TILE_ID_BLOCK_G  5
  983. #define TILE_ID_BLOCK_B  6
  984. #define TILE_ID_BLOCK_Y  7 //more orange than yellow, but whatever
  985.  
  986. //...
  987.  
  988. #define TILE_ID_PLAYER_BACK  12
  989. #define TILE_ID_PLAYER_RIGHT 13
  990. #define TILE_ID_PLAYER_LEFT  14
  991.  
  992. #define TILE_ID_HIGHLIGHT 15 //for level editor
  993.  
  994. union tile_run { //compressed tile data
  995.   kit::u16 value;
  996.   struct { kit::u16 len:11, tile:4, opaque:1; };
  997.   tile_run(kit::u16 _value) : value(_value) {}
  998. };
  999.  
  1000. union tile_array { //uncompressed tile data
  1001.   kit::u8 value;
  1002.   struct { kit::u8 tile:4, arg:2, has_player:1, opaque:1; };
  1003.   tile_array(kit::u8 _value) : value(_value) {}
  1004. };
  1005.  
  1006.  
  1007.  
  1008. extern kit::shape::fpoint tile_camera;
  1009. extern kit::s32           tile_layer; //the maximum layer to draw to
  1010.  
  1011. //index in tiles array that corresponds to player's sprite
  1012. extern kit::u32 player_which;
  1013.  
  1014.  
  1015.  
  1016. void draw_tile(kit::s32 x, kit::s32 y, kit::s32 z,
  1017.                kit::u32 which, kit::f32 alpha = 1.0f);
  1018.  
  1019. void draw_tiles(kit::s32 size_x, kit::s32 size_y, kit::s32 size_z,
  1020.                 tile_array* tiles);
  1021.  
  1022. void draw_tiles_tiny(kit::s32 size_x, kit::s32 size_y, kit::s32 size_z,
  1023.                      tile_array* tiles);
  1024.  
  1025.  
  1026.  
  1027. static inline bool isInFront(kit::shape::point3d p_a, kit::shape::point3d p_b){
  1028.   return (p_a.x < p_b.x) || (p_a.y < p_b.y) || (p_a.z > p_b.z);
  1029. }
  1030.  
  1031. /****************************** "user_main.cpp" *******************************/
  1032.  
  1033. #define CTX_MAIN_MENU  0
  1034. #define CTX_LVL_SELECT 1
  1035. #define CTX_LVL_EDITOR 2
  1036. #define CTX_PLAYING    3
  1037. #define CTX_REBINDING  4
  1038.  
  1039. struct ctx_callbacks {
  1040.   kit::s32 (*handle_events)() = nullptr;
  1041.   void     (*draw_things  )() = nullptr;
  1042. };
  1043.  
  1044. extern kit::u32 ctx_which;
  1045.  
  1046. extern kit::shape::point  cursor_pos;
  1047. extern kit::shape::fpoint cursor_norm;
  1048. extern bool               cursor_scroll;
  1049. extern bool               cursor_snow;
  1050.  
  1051. extern kit::u64 frame_num;
  1052.  
  1053. void clear_snowflakes();
  1054.  
  1055. /**************************** "ctx_main_menu.cpp" *****************************/
  1056.  
  1057. #define ICON_MENU_LEN 8 //used for uv coordinate conversions
  1058.  
  1059. extern kit::Texture* icon_menu;
  1060. extern kit::Texture* icon_menu_inv;
  1061.  
  1062. void draw_icon_rgb(kit::f32 x, kit::f32 y, kit::u32 which,
  1063.                    kit::clrs::ABGR color, bool inverted = false);
  1064.  
  1065. void draw_icon_hsv(kit::f32 x, kit::f32 y, kit::u32 which,
  1066.                    kit::f32 hue, bool inverted = false);
  1067.  
  1068. kit::s32 ctx_main_menu_evt();  void ctx_main_menu_draw();
  1069.  
  1070. static inline bool point_in_rect(kit::shape::point& pnt,
  1071.                                  kit::shape::rect& rct)
  1072. {
  1073.   return (pnt.x >= rct.x) && (pnt.x < (rct.x+rct.w)) &&
  1074.          (pnt.y >= rct.y) && (pnt.y < (rct.y+rct.h));
  1075. }
  1076.  
  1077. /**************************** "ctx_lvl_select.cpp" ****************************/
  1078.  
  1079. kit::s32 ctx_lvl_select_evt();  void ctx_lvl_select_draw();
  1080.  
  1081. /****************************** "utils_misc.cpp" ******************************/
  1082.  
  1083. #include <string>
  1084.  
  1085. #define LEVEL_FILE_MAGIC 0x4C5A496B // = "kIZL" (no null terminator)
  1086.  
  1087. struct file_list { //16B
  1088.   std::string** files;
  1089.   kit::u32      files_len;
  1090.   kit::u8       _padding8[3];
  1091.   bool          valid = false;
  1092.  
  1093.   file_list(const char* searchPath = ".",
  1094.             kit::u32    signature  = LEVEL_FILE_MAGIC);
  1095.   ~file_list();
  1096.  
  1097.   void draw(kit::s32 x, kit::s32 y, kit::u32 max_len,
  1098.             kit::u32 first = 0, kit::u32 last = KIT_U32_MAX);
  1099.  
  1100.  
  1101.   const std::string& operator[](int idx) const {
  1102.     if(!valid)
  1103.       throw "file_list::operator[](): tried to index invalid list";
  1104.  
  1105.     if(idx < -(int)files_len  ||  idx >= (int)files_len)
  1106.       throw "file_list::operator[](): index out of bounds";
  1107.  
  1108.     return (idx>=0) ? *files[idx] : *files[files_len+idx /*(idx is negative)*/];
  1109.  
  1110.   }
  1111.  
  1112. };
  1113.  
  1114. struct level_file { //32B + (64*<# of signs>)B
  1115.   kit::u32      magic; //file signature = LEVEL_FILE_MAGIC
  1116.   kit::u16    release; //'what build number was the level made in/for?'
  1117.   kit::u16 headerSize; //must be >= 32
  1118.  
  1119.   kit::u32   dataSize; //not counting signs
  1120.   kit::u16      dimsX;
  1121.   kit::u16      dimsY;
  1122.  
  1123.   kit::u16      dimsZ;
  1124.   kit::u16    playerX;
  1125.   kit::u16    playerY;
  1126.   kit::u16    playerZ;
  1127.  
  1128.   //inside the file, data is just the file offset to the tile data.
  1129.    //when loaded, data will be converted to an actual valid pointer
  1130.   //nesting order is:  x,z,y
  1131.   tile_run*      data; //compressed tile data; run length encoded
  1132.  
  1133.   char   signs[4][64]; //maximum of 4 signs
  1134.  
  1135. };
  1136.  
  1137. /**************************** "ctx_lvl_editor.cpp" ****************************/
  1138.  
  1139. kit::s32 ctx_lvl_editor_evt();  void ctx_lvl_editor_draw();
  1140.  
  1141. /******************************************************************************/
  1142.  
  1143. #endif /* _INCLUDE_ALL_HPP */
  1144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement