copy mmapd file into large buffer to allow read/write past end
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 1 Jan 2022 03:16:07 +0000 (03:16 +0000)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 1 Jan 2022 03:16:07 +0000 (03:16 +0000)
verilator/microwatt-verilator.cpp

index a4a8f25b222922e0fec0d8f7d23f6f1bb70e6047..dbdee9271bbc4b28be8d9ca9f8d309bfebec950d 100644 (file)
@@ -94,6 +94,7 @@ int main(int argc, char **argv)
     // contents are not overwritten
     unsigned char *mem = NULL;
     if (bram_file != NULL) {
+        unsigned char *fmem = NULL;
 
         int fd = open(bram_file, O_RDONLY);
         if (fd < 0) {
@@ -108,14 +109,20 @@ int main(int argc, char **argv)
             exit(2);
         }
 
-        mem = (unsigned char*)mmap(NULL, statbuf.st_size,
+        fmem = (unsigned char*)mmap(NULL, statbuf.st_size,
                                    PROT_READ|PROT_WRITE, MAP_PRIVATE,
                                    fd, 0);
-        if (mem == MAP_FAILED) {
+        if (fmem == MAP_FAILED) {
             printf("Mapping Failed\n");
             exit(2);
         }
         close(fd);
+
+        // allocate more memory than is in the file, copy it over
+        size_t sz = 0x2000000;
+        mem = (unsigned char*)malloc(sz);
+        memcpy(mem, fmem, statbuf.st_size);
+        munmap(fmem, statbuf.st_size);
     }
     unsigned long long bram_data = 0;
     unsigned long long bram_data1 = 0; // another clock delay
@@ -191,6 +198,10 @@ int main(int argc, char **argv)
 #endif // BRAM_DEBUG
        }
 
+    if (mem != NULL) {
+        free(mem);
+    }
+
     fclose(dump);
 
 #if VM_TRACE