From bca7bde569a88996371a1f87f1085c2e76d0e0b2 Mon Sep 17 00:00:00 2001 From: giomba Date: Thu, 12 Aug 2021 20:37:15 +0200 Subject: [PATCH] added buffering for frame creation this will soon allow us to compress images --- frame.cpp | 58 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/frame.cpp b/frame.cpp index 7f96f9e..5cee9d5 100644 --- a/frame.cpp +++ b/frame.cpp @@ -5,6 +5,7 @@ * */ #include #include +#include enum ExitStatus { BAD_ARGUMENT = 1, @@ -16,6 +17,9 @@ int main(int argc, char** argv) { if (argc < 2) { return BAD_ARGUMENT; } + const int images = argc - 1; + std::stringstream jump_table; + std::stringstream asm_code; std::fstream outfile; outfile.open("frame.S", std::ios::out); @@ -23,19 +27,23 @@ int main(int argc, char** argv) { return FILE_NOT_OPEN; } - outfile << "#include \"macro.h\"" << std::endl; - outfile << "#include " << std::endl; + jump_table << "#include \"macro.h\"" << std::endl; + jump_table << "#include " << std::endl; - for (int current_image = 0; current_image < argc - 1; ++current_image) { - outfile << ".global line_jump_table_" << current_image << std::endl; - outfile << "line_jump_table_" << current_image << ":" << std::endl; + for (int current_image = 0; current_image < images; ++current_image) { + jump_table << ".global line_jump_table_" << current_image << std::endl; + jump_table << "line_jump_table_" << current_image << ":" << std::endl; for (int i = 0; i < 256; ++i) { - outfile << '\t' << "jmp line_" << current_image << "_" << i << std::endl; + jump_table << '\t' << "jmp line_" << current_image << "_" << i << std::endl; } } - for (int current_image = 0; current_image < argc - 1; ++current_image) { + + char** image = new char*[images]; + + for (int current_image = 0; current_image < images; ++current_image) { std::fstream infile; + infile.open(argv[1 + current_image], std::ios::in); if (! infile) { return FILE_NOT_OPEN; @@ -60,7 +68,7 @@ int main(int argc, char** argv) { infile >> width >> height; std::cout << width << " x " << height << std::endl; - char* image = new char[height * width]; + image[current_image] = new char[height * width]; for (int y = 0; y < height; ) { for (int x = 0; x < width; ) { @@ -68,7 +76,7 @@ int main(int argc, char** argv) { if (! infile.good()) return FILE_BAD_FORMAT; if (c == '0' || c == '1') { - image[y * width + x] = c; + image[current_image][y * width + x] = c; x++; } else { continue; @@ -80,13 +88,13 @@ int main(int argc, char** argv) { infile.close(); for (int y = 0; y < height; ++y) { - outfile << "line_" << current_image << "_" << y << ":" << std::endl; + asm_code << "line_" << current_image << "_" << y << ":" << std::endl; int count = 0; - char last = image[y * width + 0]; + char last = image[current_image][y * width + 0]; for (int x = 0; x < width; ++x) { - char current = image[y * width + x]; + char current = image[current_image][y * width + x]; std::cout << "current " << current << " last " << last << std::endl; if (current == last) { ++count; @@ -94,34 +102,36 @@ int main(int argc, char** argv) { std::cout << "detected change" << std::endl; if (last == '0') { std::cout << "clear" << std::endl; - outfile << '\t' << "cbi IO(PORTB), 4" << std::endl; + asm_code << '\t' << "cbi IO(PORTB), 4" << std::endl; } else if (last == '1') { std::cout << "set" << std::endl; - outfile << '\t' << "sbi IO(PORTB), 4" << std::endl; + asm_code << '\t' << "sbi IO(PORTB), 4" << std::endl; } if (count > 1) { - outfile << '\t' << "ldi r31, " << count - 1 << std::endl; - outfile << "1:" << std::endl; - outfile << '\t' << "dec r31" << std::endl; - outfile << '\t' << "nop" << std::endl; - outfile << '\t' << "brne 1b" << std::endl; + asm_code << '\t' << "ldi r31, " << count - 1 << std::endl; + asm_code << "1:" << std::endl; + asm_code << '\t' << "dec r31" << std::endl; + asm_code << '\t' << "nop" << std::endl; + asm_code << '\t' << "brne 1b" << std::endl; } - outfile << '\t' << "rjmp 2f" << std::endl; - outfile << "2:" << std::endl; + asm_code << '\t' << "rjmp 2f" << std::endl; + asm_code << "2:" << std::endl; last = current; count = 1; } } - outfile << '\t' << "cbi IO(PORTB), 4" << std::endl; - outfile << '\t' << "jmp jump_table_return_address" << std::endl; + asm_code << '\t' << "cbi IO(PORTB), 4" << std::endl; + asm_code << '\t' << "jmp jump_table_return_address" << std::endl; } - delete[] image; } + delete[] image; + + outfile << jump_table.str() << asm_code.str(); outfile.close(); }