From bb2e911546d897f855519b96f0736812ae1aca87 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Sat, 28 Dec 2024 06:09:32 -0600 Subject: [PATCH] rotating triangle --- Makefile.dreamcast.mk | 5 +-- c/assert_dreamcast.h | 30 ++++++++++++++ c/classpath.h | 3 ++ c/classpath.inc.c | 3 ++ c/main_dreamcast.c | 2 - c/printf.h | 2 + classpath.mk | 3 ++ example/DreamcastVideo.class.h | 2 +- example/DreamcastVideo2.class.h | 2 +- example/DreamcastVideo2.java | 70 ++++++++++++++++++++++----------- example/Vec2.class.h | 15 +++++++ generate_classpath.sh | 12 +++--- p/VecTest.java | 30 ++++++++++++++ 13 files changed, 143 insertions(+), 36 deletions(-) create mode 100644 example/Vec2.class.h create mode 100644 p/VecTest.java diff --git a/Makefile.dreamcast.mk b/Makefile.dreamcast.mk index 6978472..e5152e0 100644 --- a/Makefile.dreamcast.mk +++ b/Makefile.dreamcast.mk @@ -50,8 +50,5 @@ LIBGCC_OBJ = \ include classpath.mk -CLASS_FILES = \ - example/DreamcastVideo2.class.o - main.elf: LDSCRIPT = $(LIB)/main.lds -main.elf: $(START_OBJ) $(OBJ) $(MAIN_OBJ) $(MAIN_DREAMCAST_OBJ) $(LIBGCC_OBJ) $(CLASS_FILES) $(CLASS_PATH) +main.elf: $(START_OBJ) $(OBJ) $(MAIN_OBJ) $(MAIN_DREAMCAST_OBJ) $(LIBGCC_OBJ) $(CLASS_PATH) diff --git a/c/assert_dreamcast.h b/c/assert_dreamcast.h index 089e4ff..62158df 100644 --- a/c/assert_dreamcast.h +++ b/c/assert_dreamcast.h @@ -1,6 +1,7 @@ #pragma once #include "printf.h" +#include "class_file.h" #define assert(b) \ do { \ @@ -9,3 +10,32 @@ while (1); \ } \ } while (0); + +static inline void assert_print_string(struct constant * constant) +{ + for (int i = 0; i < constant->utf8.length; i++) { + printc(constant->utf8.bytes[i]); + } + printc('\n'); +} + +#define assertvm(vm, b) \ +do { \ + if (!(b)) { \ + printf("%s:%d %s: vm assertion `%s` failed\n", __FILE__, __LINE__, __func__, #b); \ + \ + struct class_entry * class_entry = vm->current_frame->class_entry; \ + struct constant * class_constant = &class_entry->class_file->constant_pool[class_entry->class_file->this_class - 1]; \ + assert(class_constant->tag == CONSTANT_Class); \ + struct constant * class_name_constant = &class_entry->class_file->constant_pool[class_constant->class.name_index - 1]; \ + assert(class_name_constant->tag == CONSTANT_Utf8); \ + assert_print_string(class_name_constant); \ + \ + struct method_info * method_info = vm->current_frame->method; \ + struct constant * method_name_constant = &class_entry->class_file->constant_pool[method_info->name_index - 1]; \ + assert(method_name_constant->tag == CONSTANT_Utf8); \ + assert_print_string(method_name_constant); \ + \ + while (1); \ + } \ + } while (0); diff --git a/c/classpath.h b/c/classpath.h index 3cc81e8..5570f63 100644 --- a/c/classpath.h +++ b/c/classpath.h @@ -1,3 +1,6 @@ +#include "example/DreamcastVideo2.class.h" +#include "example/DreamcastVideo.class.h" +#include "example/Vec2.class.h" #include "java/io/PrintStream.class.h" #include "java/lang/Integer.class.h" #include "java/lang/Math.class.h" diff --git a/c/classpath.inc.c b/c/classpath.inc.c index 6d61743..44bb591 100644 --- a/c/classpath.inc.c +++ b/c/classpath.inc.c @@ -1,3 +1,6 @@ +(const uint8_t *)&_binary_example_DreamcastVideo2_class_start, +(const uint8_t *)&_binary_example_DreamcastVideo_class_start, +(const uint8_t *)&_binary_example_Vec2_class_start, (const uint8_t *)&_binary_java_io_PrintStream_class_start, (const uint8_t *)&_binary_java_lang_Integer_class_start, (const uint8_t *)&_binary_java_lang_Math_class_start, diff --git a/c/main_dreamcast.c b/c/main_dreamcast.c index 7b5d43a..ac0a119 100644 --- a/c/main_dreamcast.c +++ b/c/main_dreamcast.c @@ -8,7 +8,6 @@ #include "sh7091_scif.h" #include "classpath.h" -#include "example/DreamcastVideo2.class.h" void main() { @@ -16,7 +15,6 @@ void main() const uint8_t * class_file_buffers[] = { #include "classpath.inc.c" - (const uint8_t *)&_binary_example_DreamcastVideo2_class_start, }; int class_file_buffers_length = (sizeof (class_file_buffers)) / (sizeof (class_file_buffers[0])); diff --git a/c/printf.h b/c/printf.h index cd462ce..da688a7 100644 --- a/c/printf.h +++ b/c/printf.h @@ -25,6 +25,8 @@ void print_cstring(const char * s); void _printf(const char * format, ...); #define printf(...) _printf(__VA_ARGS__) +#define printc(c) print_char(c) +#define prints(s) print_cstring(s) #if defined(DEBUG_PRINT) #define debugf(...) _printf(__VA_ARGS__) diff --git a/classpath.mk b/classpath.mk index bf09e1e..4888219 100644 --- a/classpath.mk +++ b/classpath.mk @@ -1,4 +1,7 @@ CLASS_PATH = \ + example/DreamcastVideo2.class.o \ + example/DreamcastVideo.class.o \ + example/Vec2.class.o \ java/io/PrintStream.class.o \ java/lang/Integer.class.o \ java/lang/Math.class.o \ diff --git a/example/DreamcastVideo.class.h b/example/DreamcastVideo.class.h index 2115128..1465262 100644 --- a/example/DreamcastVideo.class.h +++ b/example/DreamcastVideo.class.h @@ -3,7 +3,7 @@ #include #ifdef __cplusplus -extern "C" { +extern C { #endif extern uint32_t _binary_example_DreamcastVideo_class_start __asm("_binary_example_DreamcastVideo_class_start"); diff --git a/example/DreamcastVideo2.class.h b/example/DreamcastVideo2.class.h index 33a8306..406384c 100644 --- a/example/DreamcastVideo2.class.h +++ b/example/DreamcastVideo2.class.h @@ -3,7 +3,7 @@ #include #ifdef __cplusplus -extern "C" { +extern C { #endif extern uint32_t _binary_example_DreamcastVideo2_class_start __asm("_binary_example_DreamcastVideo2_class_start"); diff --git a/example/DreamcastVideo2.java b/example/DreamcastVideo2.java index 57f0a22..c925b3d 100644 --- a/example/DreamcastVideo2.java +++ b/example/DreamcastVideo2.java @@ -17,10 +17,22 @@ import sega.dreamcast.systembus.SystembusBits; import sega.dreamcast.MemoryMap; import java.misc.Memory; +class Vec2 { + float x; + float y; +} + class DreamcastVideo2 { - public static TAGlobalParameter.polygon_type_0 gt0; - public static TAVertexParameter.polygon_type_0 vt0; - public static TAGlobalParameter.end_of_list eol; + static final int framebuffer_width = 640; + static final int framebuffer_height = 480; + + static TAGlobalParameter.polygon_type_0 gt0; + static TAVertexParameter.polygon_type_0 vt0; + static TAGlobalParameter.end_of_list eol; + + static Vec2[] vtx; + + static float theta; static { int parameter_control_word = TAParameter.para_control__para_type__polygon_or_modifier_volume @@ -52,6 +64,31 @@ class DreamcastVideo2 { 0xff00ff00); // color (green) eol = new TAGlobalParameter.end_of_list(TAParameter.para_control__para_type__end_of_list); + + vtx = new Vec2[3]; + for (int i = 0; i < 3; i++) + vtx[i] = new Vec2(); + vtx[0].x = 0.0f; + vtx[0].y = 1.0f; + vtx[1].x = 0.866025f; + vtx[1].y = -0.5f; + vtx[2].x = -0.866025f; + vtx[2].y = -0.5f; + } + + public static void transform_vertex(Vec2 v, boolean end_of_strip) { + DreamcastVideo2.vt0.parameter_control_word = TAParameter.para_control__para_type__vertex_parameter; + if (end_of_strip) + DreamcastVideo2.vt0.parameter_control_word |= TAParameter.para_control__end_of_strip; + + float x0 = v.x * Math.cos(theta) - v.y * Math.sin(theta); + float y0 = v.x * Math.sin(theta) + v.y * Math.cos(theta); + float x = x0 * 240 + 320; + float y = -y0 * 240 + 240; + + DreamcastVideo2.vt0.x = x; + DreamcastVideo2.vt0.y = y; + Memory.putSQ1(DreamcastVideo2.vt0, MemoryMap.ta_fifo_polygon_converter); } public static void transfer_scene() { @@ -59,21 +96,9 @@ class DreamcastVideo2 { Memory.putSQ1(DreamcastVideo2.gt0, MemoryMap.ta_fifo_polygon_converter); // vertex parameters - DreamcastVideo2.vt0.parameter_control_word = TAParameter.para_control__para_type__vertex_parameter; - DreamcastVideo2.vt0.x = 10.0f; - DreamcastVideo2.vt0.y = 10.0f; - Memory.putSQ1(DreamcastVideo2.vt0, MemoryMap.ta_fifo_polygon_converter); - - DreamcastVideo2.vt0.parameter_control_word = TAParameter.para_control__para_type__vertex_parameter; - DreamcastVideo2.vt0.x = 100.0f; - DreamcastVideo2.vt0.y = 10.0f; - Memory.putSQ1(DreamcastVideo2.vt0, MemoryMap.ta_fifo_polygon_converter); - - DreamcastVideo2.vt0.parameter_control_word = TAParameter.para_control__para_type__vertex_parameter - | TAParameter.para_control__end_of_strip; - DreamcastVideo2.vt0.x = 100.0f; - DreamcastVideo2.vt0.y = 100.0f; - Memory.putSQ1(DreamcastVideo2.vt0, MemoryMap.ta_fifo_polygon_converter); + for (int i = 0; i < 3; i++) { + transform_vertex(vtx[i], (i == 2)); + } // end of list Memory.putSQ1(DreamcastVideo2.eol, MemoryMap.ta_fifo_polygon_converter); @@ -100,9 +125,6 @@ class DreamcastVideo2 { }; int opb_size_total = opb_size[0].total(); - - int framebuffer_width = 640; - int framebuffer_height = 480; int num_render_passes = opb_size.length; Core.fb_init(framebuffer_width, framebuffer_height); @@ -126,11 +148,10 @@ class DreamcastVideo2 { Background.background(TextureMemoryAllocation.background_start[1], background_color); - //int ta = -1; - //int core = -2; int core = 0; int ta = 0; while (true) { + // unpipelined render loop TAFIFOPolygonConverter.init(TextureMemoryAllocation.isp_tsp_parameters_start[ta], TextureMemoryAllocation.isp_tsp_parameters_end[ta], TextureMemoryAllocation.object_list_start[ta], @@ -141,6 +162,7 @@ class DreamcastVideo2 { framebuffer_height / 32); transfer_scene(); TAFIFOPolygonConverter.wait_opaque_list(); + Core.start_render(TextureMemoryAllocation.region_array_start[ta], TextureMemoryAllocation.isp_tsp_parameters_start[ta], TextureMemoryAllocation.background_start[ta], @@ -149,6 +171,8 @@ class DreamcastVideo2 { Core.wait_end_of_render_tsp(); Memory.putU4(Holly.FB_R_SOF1, TextureMemoryAllocation.framebuffer_start[core]); core = (core + 1) % 1; + + theta += Math.DEGREES_TO_RADIANS; } } } diff --git a/example/Vec2.class.h b/example/Vec2.class.h new file mode 100644 index 0000000..fa2112e --- /dev/null +++ b/example/Vec2.class.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern C { +#endif + +extern uint32_t _binary_example_Vec2_class_start __asm("_binary_example_Vec2_class_start"); +extern uint32_t _binary_example_Vec2_class_end __asm("_binary_example_Vec2_class_end"); +extern uint32_t _binary_example_Vec2_class_size __asm("_binary_example_Vec2_class_size"); + +#ifdef __cplusplus +} +#endif diff --git a/generate_classpath.sh b/generate_classpath.sh index b8ceb64..66a41c4 100644 --- a/generate_classpath.sh +++ b/generate_classpath.sh @@ -66,10 +66,12 @@ function classpath_h () { done } +set -e + find sega/ java/ -name '*.class' -exec rm -f {} \; find example/ sega/ java/ -name '*.java' | sort | make_class -find sega/ java/ -name '*.class' | sort | rename_class_files -find sega/ java/ -name '*.class' | sort | classpath_mk -find sega/ java/ -name '*.class' | sort | classpath_inc_c -find sega/ java/ -name '*.class' | sort | make_header -find sega/ java/ -name '*.class' | sort | classpath_h +find example/ sega/ java/ -name '*.class' | sort | rename_class_files +find example/ sega/ java/ -name '*.class' | sort | classpath_mk +find example/ sega/ java/ -name '*.class' | sort | classpath_inc_c +find example/ sega/ java/ -name '*.class' | sort | make_header +find example/ sega/ java/ -name '*.class' | sort | classpath_h diff --git a/p/VecTest.java b/p/VecTest.java new file mode 100644 index 0000000..4715202 --- /dev/null +++ b/p/VecTest.java @@ -0,0 +1,30 @@ +package p; + +class Vec2 { + float x; + float y; +} + +class VecTest { + static Vec2[] vtx; + + static { + vtx = new Vec2[3]; + for (int i = 0; i < 3; i++) + vtx[i] = new Vec2(); + vtx[0].x = 0.0f; + vtx[0].y = 1.0f; + vtx[1].x = 0.866025f; + vtx[1].y = -0.5f; + vtx[2].x = -0.866025f; + vtx[2].y = -0.5f; + } + + public static float test() { + return vtx[0].y; + } + + public static void main(String[] args) { + System.out.println(test()); + } +}