From 45501b58cbd3b1f16bfeb8bfa667c3649bba6b87 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Tue, 21 Jan 2025 18:36:49 -0600 Subject: [PATCH] example: add JavaCubeMemory --- c/classpath.h | 2 + c/classpath.inc.c | 2 + c/main_dreamcast.c | 2 +- classes/example/JavaCubeMemory.java | 247 ++++++++++++++++++++++++++++ classpath.mk | 4 +- generate_classpath.sh | 3 + 6 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 classes/example/JavaCubeMemory.java diff --git a/c/classpath.h b/c/classpath.h index 96028a6..133eaca 100644 --- a/c/classpath.h +++ b/c/classpath.h @@ -6,6 +6,7 @@ #include "classes/filesystem/iso9660/PrimaryVolumeDescriptor.class.h" #include "classes/filesystem/iso9660/VolumeParser.class.h" #include "classes/java/io/PrintStream.class.h" +#include "classes/java/lang/Character.class.h" #include "classes/java/lang/DecimalDigits.class.h" #include "classes/java/lang/Integer.class.h" #include "classes/java/lang/Float.class.h" @@ -79,3 +80,4 @@ #include "classes/sega/dreamcast/holly/TAVertexParameter.class.h" #include "classes/sega/dreamcast/holly/TextureMemoryAllocation.class.h" #include "classes/sega/dreamcast/sh7091/StoreQueueBuffer.class.h" +#include "classes/example/JavaCubeMemory.class.h" diff --git a/c/classpath.inc.c b/c/classpath.inc.c index a04afe1..085d454 100644 --- a/c/classpath.inc.c +++ b/c/classpath.inc.c @@ -6,6 +6,7 @@ (const uint8_t *)&_binary_classes_filesystem_iso9660_PrimaryVolumeDescriptor_class_start, (const uint8_t *)&_binary_classes_filesystem_iso9660_VolumeParser_class_start, (const uint8_t *)&_binary_classes_java_io_PrintStream_class_start, +(const uint8_t *)&_binary_classes_java_lang_Character_class_start, (const uint8_t *)&_binary_classes_java_lang_DecimalDigits_class_start, (const uint8_t *)&_binary_classes_java_lang_Integer_class_start, (const uint8_t *)&_binary_classes_java_lang_Float_class_start, @@ -79,3 +80,4 @@ (const uint8_t *)&_binary_classes_sega_dreamcast_holly_TAVertexParameter_class_start, (const uint8_t *)&_binary_classes_sega_dreamcast_holly_TextureMemoryAllocation_class_start, (const uint8_t *)&_binary_classes_sega_dreamcast_sh7091_StoreQueueBuffer_class_start, +(const uint8_t *)&_binary_classes_example_JavaCubeMemory_class_start, diff --git a/c/main_dreamcast.c b/c/main_dreamcast.c index dea6611..8d1b718 100644 --- a/c/main_dreamcast.c +++ b/c/main_dreamcast.c @@ -20,7 +20,7 @@ void main() //const uint8_t * main_class = (const uint8_t *)"example/JavaCube"; //const uint8_t * main_class = (const uint8_t *)"example/GdromClassLoader"; - const uint8_t * main_class = (const uint8_t *)"Main"; + const uint8_t * main_class = (const uint8_t *)"example/JavaCubeMemory"; int main_class_length = string_length((const char *)main_class); for (int i = 0; i < main_class_length; i++) diff --git a/classes/example/JavaCubeMemory.java b/classes/example/JavaCubeMemory.java new file mode 100644 index 0000000..304bd25 --- /dev/null +++ b/classes/example/JavaCubeMemory.java @@ -0,0 +1,247 @@ +package example; + +import sega.dreamcast.holly.Background; +import sega.dreamcast.holly.Core; +import sega.dreamcast.holly.CoreBits; +import sega.dreamcast.holly.Holly; +import sega.dreamcast.holly.RegionArray; +import sega.dreamcast.holly.TABits; +import sega.dreamcast.holly.TAFIFOPolygonConverter; +import sega.dreamcast.holly.TAParameter; +import sega.dreamcast.holly.TextureMemoryAllocation; +import sega.dreamcast.holly.ISPTSP; +import sega.dreamcast.holly.TAVertexParameter; +import sega.dreamcast.holly.TAGlobalParameter; +import sega.dreamcast.holly.VideoOutput; +import sega.dreamcast.holly.VideoOutputMode; +import sega.dreamcast.MemoryMap; +import model.CubeModel; +import model.Vec3; +import model.Vec2; +import model.FacePTN; +import model.ModelObject; +import jvm.internal.Memory; + +public class JavaCubeMemory { + static final int framebuffer_width = 640; + static final int framebuffer_height = 480; + + static Vec2[] quad; + static Vec2[] quad_uv; + + static float theta; + + public static int[] texture_extents; + + static String[] texture_filenames = { + "JAVA_CUP.DAT;1", + "JAVA_TEX.DAT;1", + }; + + static final int parameter_control_word = TAParameter.para_control__para_type__polygon_or_modifier_volume + | TAParameter.para_control__list_type__translucent + | TAParameter.obj_control__col_type__packed_color + | TAParameter.obj_control__texture; + + static final int isp_tsp_instruction_word = ISPTSP.isp_tsp_instruction_word__depth_compare_mode__greater + | ISPTSP.isp_tsp_instruction_word__culling_mode__no_culling; + + static final int tsp_instruction_word = ISPTSP.tsp_instruction_word__src_alpha_instr__src_alpha + | ISPTSP.tsp_instruction_word__dst_alpha_instr__inverse_src_alpha + | ISPTSP.tsp_instruction_word__fog_control__no_fog + | ISPTSP.tsp_instruction_word__texture_u_size__512 + | ISPTSP.tsp_instruction_word__texture_v_size__1024 + | ISPTSP.tsp_instruction_word__use_alpha; + + static final int texture_address = TextureMemoryAllocation.texture_regions[1][0] + 512; + static final int texture_control_word = ISPTSP.texture_control_word__pixel_format__4444 + | ISPTSP.texture_control_word__scan_order__non_twiddled + | ISPTSP.texture_control_word__texture_address(texture_address / 8); + + static { + texture_extents = new int[texture_filenames.length]; + for (int i = 0; i < texture_filenames.length; i++) { + texture_extents[i] = -1; + } + } + + public static void transform_vertex(int parameter_control_word, + Vec3[] position, + Vec2[] texture, + FacePTN ptn) { + + float px = position[ptn.position].x; + float py = position[ptn.position].y; + float pz = position[ptn.position].z; + + float theta2 = theta * 2.0f; + float scale = (Math.sin(theta2) + 3.0f) * 0.3f; + + px *= scale; + py *= scale; + pz *= scale; + + float x0 = px * Math.cos(theta) - py * Math.sin(theta); + float y0 = px * Math.sin(theta) + py * Math.cos(theta); + float z0 = pz; + + float theta05 = theta * 0.5f; + + float x1 = x0 * Math.cos(theta05) - z0 * Math.sin(theta05); + float y1 = y0; + float z1 = x0 * Math.sin(theta05) + z0 * Math.cos(theta05); + + // camera transform + float z2 = 3f + z1; + + // perspective + float x2 = x1 / z2; + float y2 = y1 / z2; + + // screen space + float x = x2 * 240f + 320f; + float y = -y2 * 240f + 240f; + float z = 1.0f / z2; + + TAVertexParameter.polygon_type_3(parameter_control_word, + x, + y, + z, + texture[ptn.texture].x, // u + texture[ptn.texture].y, // v + 0xff000000, // base_color + 0); // offset_color + } + + public static void transform_triangle(int n, Vec3[] position, Vec2[] texture, FacePTN[] face) { + for (int i = 0; i < 3; i++) { + int parameter_control_word = TAParameter.para_control__para_type__vertex_parameter; + if (i == 2) + parameter_control_word |= TAParameter.para_control__end_of_strip; + transform_vertex(parameter_control_word, position, texture, face[i]); + } + } + + public static void transfer_cube_scene() { + // global parameters + TAGlobalParameter.polygon_type_0(parameter_control_word, + isp_tsp_instruction_word, + tsp_instruction_word, + texture_control_word, + 0, // data_size_for_sort_dma + 0); // next_address_for_sort_dma + + // triangle parameters + ModelObject obj = CubeModel.objects[0]; + for (int i = 0; i < obj.faces.length; i ++) { + transform_triangle(i, CubeModel.position, CubeModel.texture, obj.faces[i]); + } + + // end of list + TAGlobalParameter.end_of_list(TAParameter.para_control__para_type__end_of_list); + } + + public static void transfer_textures() { + int texture = MemoryMap.texture_memory64 + TextureMemoryAllocation.texture_regions[1][0] + 512; + + int length = 512 * 512 * 2 * 2 / 4; + int address = 0xac400000; + for (int i = 0; i < length; i++) { + int value = Memory.getU4(address); + Memory.putU4(texture, value); + if ((i & 2047) == 0) { + System.out.print(i); + System.out.print(' '); + System.out.print(address); + System.out.print(' '); + System.out.print(texture); + } + address += 4; + texture += 4; + } + } + + public static void main() { + System.out.println("main1"); + int ta_alloc = + TABits.ta_alloc_ctrl__opb_mode__increasing_addresses + | TABits.ta_alloc_ctrl__pt_opb__no_list + | TABits.ta_alloc_ctrl__tm_opb__no_list + | TABits.ta_alloc_ctrl__t_opb__8x4byte + | TABits.ta_alloc_ctrl__om_opb__no_list + | TABits.ta_alloc_ctrl__o_opb__no_list + ; + + RegionArray.OPBSize[] opb_size = { + new RegionArray.OPBSize(0, // opaque + 0, // opaque_modifier_volume + 8 * 4, // translucent + 0, // translucent_modifier_volume + 0) // punch_through + }; + int opb_size_total = opb_size[0].total(); + + int background_color = 0xff100a00; + Background.background(TextureMemoryAllocation.background_start[0], + 0x00c0c0c0); // sega white + Background.background(TextureMemoryAllocation.background_start[1], + 0x00c0c0c0);//background_color); // dark black + + Memory.putU4(Holly.VO_BORDER_COL, background_color); + + int num_render_passes = opb_size.length; + RegionArray.region_array(framebuffer_width / 32, + framebuffer_height / 32, + opb_size, + num_render_passes, + TextureMemoryAllocation.region_array_start[0], + TextureMemoryAllocation.object_list_start[0]); + RegionArray.region_array(framebuffer_width / 32, + framebuffer_height / 32, + opb_size, + num_render_passes, + TextureMemoryAllocation.region_array_start[1], + TextureMemoryAllocation.object_list_start[1]); + + Core.init(); + + System.out.println("transfer_textures"); + transfer_textures(); + System.out.println("transfer_textures2"); + + VideoOutput.set_framebuffer_resolution(640, 480); + VideoOutput.set_mode(VideoOutputMode.vga); + + int core = 0; + int ta = 0; + System.out.println("main"); + while (true) { + // unpipelined render loop + TAFIFOPolygonConverter.init(TextureMemoryAllocation.isp_tsp_parameters_start[ta], + TextureMemoryAllocation.isp_tsp_parameters_end[ta], + TextureMemoryAllocation.object_list_start[ta], + TextureMemoryAllocation.object_list_end[ta], + opb_size_total, + ta_alloc, + framebuffer_width / 32, + framebuffer_height / 32); + transfer_cube_scene(); + TAFIFOPolygonConverter.wait_translucent_list(); + + Core.start_render(TextureMemoryAllocation.region_array_start[ta], + TextureMemoryAllocation.isp_tsp_parameters_start[ta], + TextureMemoryAllocation.background_start[1], + TextureMemoryAllocation.framebuffer_start[core], + framebuffer_width); + Core.wait_end_of_render_tsp(); + + while (!(CoreBits.spg_status__vsync(Memory.getU4(Holly.SPG_STATUS)) != 0)); + Memory.putU4(Holly.FB_R_SOF1, TextureMemoryAllocation.framebuffer_start[core]); + while ((CoreBits.spg_status__vsync(Memory.getU4(Holly.SPG_STATUS)) != 0)); + + core = (core + 1) % 1; + + theta += Math.DEGREES_TO_RADIANS; + } + } +} diff --git a/classpath.mk b/classpath.mk index 3c1813f..8940bd8 100644 --- a/classpath.mk +++ b/classpath.mk @@ -7,6 +7,7 @@ CLASS_PATH = \ classes/filesystem/iso9660/PrimaryVolumeDescriptor.class.o \ classes/filesystem/iso9660/VolumeParser.class.o \ classes/java/io/PrintStream.class.o \ + classes/java/lang/Character.class.o \ classes/java/lang/DecimalDigits.class.o \ classes/java/lang/Integer.class.o \ classes/java/lang/Float.class.o \ @@ -79,4 +80,5 @@ CLASS_PATH = \ classes/sega/dreamcast/holly/TAGlobalParameter.class.o \ classes/sega/dreamcast/holly/TAVertexParameter.class.o \ classes/sega/dreamcast/holly/TextureMemoryAllocation.class.o \ - classes/sega/dreamcast/sh7091/StoreQueueBuffer.class.o + classes/sega/dreamcast/sh7091/StoreQueueBuffer.class.o \ + classes/example/JavaCubeMemory.class.o diff --git a/generate_classpath.sh b/generate_classpath.sh index e908895..29b927d 100644 --- a/generate_classpath.sh +++ b/generate_classpath.sh @@ -81,6 +81,7 @@ declare -a boot_classes=( filesystem/iso9660/PrimaryVolumeDescriptor.class filesystem/iso9660/VolumeParser.class java/io/PrintStream.class + java/lang/Character.class java/lang/DecimalDigits.class java/lang/Integer.class java/lang/Float.class @@ -158,6 +159,8 @@ declare -a boot_classes=( sega/dreamcast/holly/TAVertexParameter.class sega/dreamcast/holly/TextureMemoryAllocation.class sega/dreamcast/sh7091/StoreQueueBuffer.class + + example/JavaCubeMemory.class ) function boot_classes () {