refactor part 1
This commit is contained in:
parent
3a5151fed9
commit
37293b6259
158
src/main.cpp
158
src/main.cpp
@ -15,14 +15,14 @@
|
||||
#include <stdio.h>
|
||||
#include <maxmod9.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/_stdint.h>
|
||||
|
||||
#define TIMER_SPEED (BUS_CLOCK/1024)
|
||||
|
||||
void wait_forever(const char* msg);
|
||||
size_t LoadFile(const char* file, unsigned char **buffer);
|
||||
void readNextBatch(int input, void *output);
|
||||
void readNextMusic(FILE *input, void *output);
|
||||
void vbl_handler();
|
||||
void LoadNextChunk(void *output);
|
||||
void onVBL();
|
||||
|
||||
class CircularQueue {
|
||||
public:
|
||||
@ -72,19 +72,26 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
int ptr_background;
|
||||
volatile int file_image = -1;
|
||||
volatile int file_music = -1;
|
||||
|
||||
volatile bool noQueue = false;
|
||||
constexpr int frame_chunk_size = 256*192*4;
|
||||
constexpr int frame_chunk_total = 9;
|
||||
volatile int frame_chunk_counter = 0;
|
||||
volatile int frame_chunk_current = 0;
|
||||
volatile int frame_counter = 0;
|
||||
uint8_t frame_decompress[frame_chunk_size];
|
||||
uint8_t frame_buffer[frame_chunk_size*frame_chunk_total];
|
||||
CircularQueue frame_queue(frame_chunk_total);
|
||||
|
||||
constexpr int music_buffer_size = 3200*16;
|
||||
|
||||
|
||||
static uint8_t chunk[256*192*4*9];
|
||||
static int chunk_counter = 0;
|
||||
static CircularQueue queue(9);
|
||||
static int in = -1;
|
||||
static int in_music = -1;
|
||||
static int bgp;
|
||||
|
||||
static int framecount = 0;
|
||||
|
||||
mm_word on_stream_request( mm_word length, mm_addr dest, mm_stream_formats format ) {
|
||||
if(in_music){
|
||||
if(file_music){
|
||||
size_t samplesize = 1;
|
||||
switch(format){
|
||||
case MM_STREAM_8BIT_MONO: samplesize = 1; break;
|
||||
@ -93,18 +100,13 @@ mm_word on_stream_request( mm_word length, mm_addr dest, mm_stream_formats forma
|
||||
case MM_STREAM_16BIT_STEREO: samplesize = 4; break;
|
||||
}
|
||||
|
||||
// dmaCopy(&music_buffer[1024*music_queue.front], dest, 1024);
|
||||
// music_queue.pop();
|
||||
|
||||
|
||||
// int res = fread(dest, samplesize, length, in_music);
|
||||
int res = read(in_music, dest, length*samplesize);
|
||||
int res = read(file_music, dest, length*samplesize);
|
||||
|
||||
if(res){
|
||||
length = res/samplesize;
|
||||
} else {
|
||||
mmStreamClose();
|
||||
close(in_music);
|
||||
close(file_music);
|
||||
length = 0;
|
||||
}
|
||||
|
||||
@ -127,16 +129,16 @@ int main(void) {
|
||||
|
||||
nitroFSInit(NULL);
|
||||
|
||||
in = open("nitro:/image.bin", O_RDONLY);
|
||||
if (in == -1) wait_forever("cannot load image.bin");
|
||||
file_image = open("nitro:/image.bin", O_RDONLY);
|
||||
if (file_image == -1) wait_forever("cannot load image.bin");
|
||||
|
||||
in_music = open("nitro:/music.raw", O_RDONLY);
|
||||
if (in_music == -1) wait_forever("cannot load music.raw");
|
||||
file_music = open("nitro:/music.raw", O_RDONLY);
|
||||
if (file_music == -1) wait_forever("cannot load music.raw");
|
||||
|
||||
unsigned char* pal = NULL;
|
||||
size_t pal_len = LoadFile("nitro:/palette.bin", &pal);
|
||||
|
||||
bgp = bgInit(3, BgType_Bmp8, BgSize_B8_256x256, 0, 0);
|
||||
ptr_background = bgInit(3, BgType_Bmp8, BgSize_B8_256x256, 0, 0);
|
||||
|
||||
dmaCopy(pal, BG_PALETTE, pal_len);
|
||||
|
||||
@ -164,28 +166,12 @@ int main(void) {
|
||||
|
||||
// fseek(in, 0, SEEK_SET);
|
||||
|
||||
fprintf(stderr, "read\n");
|
||||
while(!queue.isFull()) {
|
||||
queue.push();
|
||||
readNextBatch(in, &chunk[256*192*4 * queue.rear]);
|
||||
fprintf(stderr, "read %i\n", queue.rear);
|
||||
fprintf(stderr, "preload start\n");
|
||||
while(!frame_queue.isFull()) {
|
||||
frame_queue.push();
|
||||
LoadNextChunk(&frame_buffer[frame_chunk_size*frame_queue.rear]);
|
||||
}
|
||||
|
||||
// while(!music_queue.isFull()) {
|
||||
// music_queue.push();
|
||||
// readNextMusic(in_music, &music_buffer[1024 * music_queue.rear]);
|
||||
// }
|
||||
|
||||
// Reset audio buffers
|
||||
// memset(audioL, 0, sampleSize * 2);
|
||||
// memset(audioR, 0, sampleSize * 2);
|
||||
|
||||
// // Preload 12 audio blocks
|
||||
// for (int i = 0; i < 12; i++) {
|
||||
// fread(&audioL[audioBlock * sampleSize], 1, sampleSize * 2, in_music);
|
||||
// fread(&audioR[audioBlock * sampleSize], 1, sampleSize * 2, in_music);
|
||||
// audioBlock++;
|
||||
// }
|
||||
fprintf(stderr, "preload done\n");
|
||||
|
||||
DC_FlushAll();
|
||||
|
||||
@ -199,7 +185,7 @@ int main(void) {
|
||||
|
||||
mm_stream mystream;
|
||||
mystream.sampling_rate = 22050;
|
||||
mystream.buffer_length = 3200*16;
|
||||
mystream.buffer_length = music_buffer_size;
|
||||
mystream.callback = on_stream_request;
|
||||
mystream.format = MM_STREAM_16BIT_MONO;
|
||||
mystream.timer = MM_TIMER2;
|
||||
@ -211,88 +197,68 @@ int main(void) {
|
||||
|
||||
while(1) {
|
||||
|
||||
irqSet(IRQ_VBLANK, vbl_handler);
|
||||
irqSet(IRQ_VBLANK, onVBL);
|
||||
|
||||
if (!queue.isFull()) {
|
||||
queue.push();
|
||||
readNextBatch(in, &chunk[256*192*4 * queue.rear]);
|
||||
if (!frame_queue.isFull()) {
|
||||
frame_queue.push();
|
||||
LoadNextChunk(&frame_buffer[frame_chunk_size*frame_queue.rear]);
|
||||
// fprintf(stderr, "read %i\n", queue.rear);
|
||||
}
|
||||
|
||||
// if (!music_queue.isFull()) {
|
||||
// music_queue.push();
|
||||
// readNextMusic(in_music, &music_buffer[1024 * music_queue.rear]);
|
||||
// fprintf(stderr, "read music %i\n", music_queue.rear);
|
||||
// }
|
||||
|
||||
if (!(framecount % 9)) mmStreamUpdate();
|
||||
if (!(frame_counter % 9)) mmStreamUpdate();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void vbl_handler()
|
||||
void onVBL()
|
||||
{
|
||||
// if (queue.isEmpty() && chunk_counter == 0) {
|
||||
// return;
|
||||
// fprintf(stderr, "NO QUEUE!!!!!\n");
|
||||
// }
|
||||
|
||||
// DC_FlushRange(&chunk[256*192*4 * queue.front + 256*192*chunk_counter], 256*192*4);
|
||||
dmaCopyAsynch(&chunk[256*192*4 * queue.front + 256*192*chunk_counter], bgGetGfxPtr(bgp), 256*192);
|
||||
if (queue.current < queue.size*0.4) {
|
||||
fprintf(stderr, "count: %i, %i\n ", chunk_counter, queue.current);
|
||||
if (!noQueue) dmaCopyAsynch(&frame_buffer[frame_chunk_size*frame_queue.front + 256*192*frame_chunk_counter], bgGetGfxPtr(ptr_background), 256*192);
|
||||
if (frame_queue.current < frame_queue.size*0.4) {
|
||||
fprintf(stderr, "count: %i, %i\n ", frame_chunk_counter, frame_queue.current);
|
||||
}
|
||||
chunk_counter++;
|
||||
framecount++;
|
||||
|
||||
if (chunk_counter >= 4) {
|
||||
if (!queue.isEmpty()) {
|
||||
queue.pop();
|
||||
chunk_counter = 0;
|
||||
frame_chunk_counter++;
|
||||
frame_counter++;
|
||||
|
||||
if (frame_chunk_counter >= 4) {
|
||||
if (!frame_queue.isEmpty()) {
|
||||
frame_queue.pop();
|
||||
frame_chunk_counter = 0;
|
||||
} else {
|
||||
chunk_counter = 4;
|
||||
frame_chunk_counter = 4;
|
||||
noQueue = true;
|
||||
fprintf(stderr, "NO QUEUE!!!!!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void *buffer = malloc(256*192*4);
|
||||
|
||||
void readNextMusic(FILE *input, void *output)
|
||||
void LoadNextChunk(void *output)
|
||||
{
|
||||
fread(output, 1024, 1, input);
|
||||
}
|
||||
uint32_t compress_size;
|
||||
read(file_image, &compress_size, sizeof(uint32_t));
|
||||
|
||||
static int chunk_count = 0;
|
||||
void readNextBatch(int input, void *output)
|
||||
{
|
||||
uint32_t chunk_len;
|
||||
// fread(&chunk_len, sizeof(uint32_t), 1, input);
|
||||
read(input, &chunk_len, sizeof(uint32_t));
|
||||
bool isRLE = compress_size & 0x01;
|
||||
compress_size = compress_size >> 1;
|
||||
|
||||
bool isRLE = chunk_len & 0x01;
|
||||
chunk_len = chunk_len >> 1;
|
||||
|
||||
if (queue.current < queue.size*0.4)
|
||||
fprintf(stderr, "%i\n ", chunk_count);
|
||||
|
||||
|
||||
|
||||
if (frame_queue.current < frame_queue.size*0.4)
|
||||
fprintf(stderr, "%i\n ", frame_chunk_current);
|
||||
|
||||
if (isRLE)
|
||||
{
|
||||
read(input, output, chunk_len);
|
||||
read(file_image, output, compress_size);
|
||||
// fread(output, 1, chunk_len, input);
|
||||
// decompress(buffer, output, RLE);
|
||||
}
|
||||
else
|
||||
{
|
||||
// fread(buffer, 1, chunk_len, input);
|
||||
read(input, buffer, chunk_len);
|
||||
fastlz_decompress(buffer, chunk_len, output, 256*192*4);
|
||||
read(file_image, frame_decompress, compress_size);
|
||||
fastlz_decompress(frame_decompress, compress_size, output, frame_chunk_size);
|
||||
}
|
||||
chunk_count++;
|
||||
frame_chunk_current++;
|
||||
noQueue = false;
|
||||
// DC_FlushRange(output, 256*192*4);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user