]> Chaos Git - corbenik/corbenik.git/commitdiff
Switch corbenik to use same memfind as loader
authorchaoskagami <chaos.kagami@gmail.com>
Tue, 24 May 2016 02:00:01 +0000 (22:00 -0400)
committerchaoskagami <chaos.kagami@gmail.com>
Tue, 24 May 2016 02:00:01 +0000 (22:00 -0400)
Makefile
source/main.c
source/std/draw.c
source/std/memory.c
source/std/memory.h

index f22430fcba82cf26fc42cf495cadf4ba01f490c4..cd093519fcccc410a0aee4a25e0b9583f5c4831b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -55,7 +55,7 @@ host/langemu.conf:
 clean:
        make -C modules clean
        make -C external clean
-       rm host/langemu.conf
+       rm -f host/langemu.conf
        rm -rf $(dir_out) $(dir_build)
 
 .PHONY: $(dir_out)/arm9loaderhax.bin
index d0a35aa2ac15315422b59a04665cea36163cf3ab..c66ec4e5bfd8086c3e3aedbca775118af82d710d 100644 (file)
@@ -22,7 +22,7 @@ main()
     // Autoboot. Non-standard code path.
     if (config.options[OPTION_AUTOBOOT] && !(HID_PAD & BUTTON_R)) {
         if (config.options[OPTION_SILENCE])
-            shut_up();
+            shut_up(); // This does exactly what it sounds like.
         load_firms();
         doing_autoboot = 1;
         boot_cfw(); // Just boot shit.
index 069557324ac551bb63049c4eeea97e2bd94319c6..0cdacac10c3c2b49d81cdafe469212a5ea25d3e8 100644 (file)
@@ -241,6 +241,9 @@ putc(void* buf, const int c)
 void
 puts(void* buf, const char* string)
 {
+    if ((buf == stdout || buf == stderr) && kill_output)
+        return;
+
     char* ref = (char*)string;
 
     while (*ref != '\0') {
@@ -331,6 +334,9 @@ void
 fflush(void* channel)
 {
     if (channel == TOP_SCREEN) {
+        if (kill_output)
+            return;
+
         for (int y = 0; y < TEXT_TOP_HEIGHT; y++) {
             for (int x = 0; x < TEXT_TOP_WIDTH; x++) {
                 char c = text_buffer_top[y * TEXT_TOP_WIDTH + x];
@@ -345,6 +351,9 @@ fflush(void* channel)
             }
         }
     } else if (channel == BOTTOM_SCREEN) {
+        if (kill_output)
+            return;
+
         for (int y = 0; y < TEXT_BOTTOM_HEIGHT; y++) {
             for (int x = 0; x < TEXT_BOTTOM_WIDTH; x++) {
                 char c = text_buffer_bottom[y * TEXT_BOTTOM_WIDTH + x];
@@ -367,6 +376,9 @@ fflush(void* channel)
 void
 vfprintf(void* channel, const char* format, va_list ap)
 {
+    if ((channel == stdout || channel == stderr) && kill_output)
+        return;
+
     char* ref = (char*)format;
 
     unsigned char* color;
@@ -479,6 +491,12 @@ vfprintf(void* channel, const char* format, va_list ap)
 void
 fprintf(void* channel, const char* format, ...)
 {
+    // The output suppression is in all the functions to minimize overhead.
+    // Function calls and format conversions take time and we don't just want
+    // to stop at putc
+    if ((channel == stdout || channel == stderr) && kill_output)
+        return;
+
     va_list ap;
     va_start(ap, format);
 
index 27c7cdb7ef913c1c073f4892c6bfa08099920974..f829b36b6a985630bc2d54ba500eb3cc6b408a06 100644 (file)
@@ -167,91 +167,30 @@ atoi(const char* str)
 #define NOT_FOUND patlen
 #define max(a, b) ((a < b) ? b : a)
 
-void
-make_delta1(int* delta1, uint8_t* pat, int32_t patlen)
+// Quick Search algorithm, adapted from
+// http://igm.univ-mlv.fr/~lecroq/string/node19.html#SECTION00190
+uint8_t*
+memfind(uint8_t* startPos, uint32_t size, const void* pattern, uint32_t patternSize)
 {
-    int i;
-    for (i = 0; i < ALPHABET_LEN; i++) {
-        delta1[i] = NOT_FOUND;
-    }
-    for (i = 0; i < patlen - 1; i++) {
-        delta1[pat[i]] = patlen - 1 - i;
-    }
-}
+    const uint8_t* patternc = (const uint8_t*)pattern;
 
-int
-is_prefix(uint8_t* word, int wordlen, int pos)
-{
-    int i;
-    int suffixlen = wordlen - pos;
-    // could also use the strncmp() library function here
-    for (i = 0; i < suffixlen; i++) {
-        if (word[i] != word[pos + i]) {
-            return 0;
-        }
-    }
-    return 1;
-}
+    // Preprocessing
+    uint32_t table[ALPHABET_LEN];
 
-int
-suffix_length(uint8_t* word, int wordlen, int pos)
-{
-    int i;
-    // increment suffix length i to the first mismatch or beginning
-    // of the word
-    for (i = 0; (word[pos - i] == word[wordlen - 1 - i]) && (i < pos); i++)
-        ;
-    return i;
-}
+    for (uint32_t i = 0; i < ALPHABET_LEN; ++i)
+        table[i] = patternSize + 1;
+    for (uint32_t i = 0; i < patternSize; ++i)
+        table[patternc[i]] = patternSize - i;
 
-void
-make_delta2(int* delta2, uint8_t* pat, int32_t patlen)
-{
-    int p;
-    int last_prefix_index = patlen - 1;
+    // Searching
+    uint32_t j = 0;
 
-    // first loop
-    for (p = patlen - 1; p >= 0; p--) {
-        if (is_prefix(pat, patlen, p + 1)) {
-            last_prefix_index = p + 1;
-        }
-        delta2[p] = last_prefix_index + (patlen - 1 - p);
+    while (j <= size - patternSize) {
+        if (memcmp(patternc, startPos + j, patternSize) == 0)
+            return startPos + j;
+        j += table[startPos[j + patternSize]];
     }
 
-    // second loop
-    for (p = 0; p < patlen - 1; p++) {
-        int slen = suffix_length(pat, patlen, p);
-        if (pat[p - slen] != pat[patlen - 1 - slen]) {
-            delta2[patlen - 1 - slen] = patlen - 1 - p + slen;
-        }
-    }
-}
-
-uint8_t*
-memfind(uint8_t* string, uint32_t stringlen, uint8_t* pat, uint32_t patlen)
-{
-    uint32_t i;
-    int delta1[ALPHABET_LEN];
-    int delta2[ALPHABET_LEN]; // Max search length is 256.
-    make_delta1(delta1, pat, patlen);
-    make_delta2(delta2, pat, patlen);
-
-    // The empty pattern must be considered specially
-    if (patlen == 0)
-        return string;
-
-    i = patlen - 1;
-    while (i < stringlen) {
-        int j = patlen - 1;
-        while (j >= 0 && (string[i] == pat[j])) {
-            --i;
-            --j;
-        }
-        if (j < 0) {
-            return (string + i + 1);
-        }
-
-        i += max(delta1[string[i]], delta2[j]);
-    }
     return NULL;
 }
+
index 859115fb58b35d97ca8cf44de9ee5c66043648e9..44e59ae36736dfde561df625b07bf29d8a803072 100644 (file)
@@ -12,8 +12,8 @@ int memcmp(const void* buf1, const void* buf2, const size_t size);
 void strncpy(void* dest, const void* src, const size_t size);
 int strncmp(const void* buf1, const void* buf2, const size_t size);
 int atoi(const char* str);
-uint8_t* memfind(uint8_t* string, uint32_t stringlen, uint8_t* pat,
-                 uint32_t patlen);
+uint8_t* memfind(uint8_t* startPos, uint32_t size,
+                 const void* pattern, uint32_t patternSize);
 int isprint(char c);
 
 #endif