Skip to content

Kernel.zip File Handling Analysis

Summary

The JavaScript code in the WASM frontend correctly handles all .c and .s files from kernel.zip, making them available to the debugger and disassembler.

File Processing Flow

1. Loading kernel.zip

File: src/frontend/nd500wasm/web/debugger.js

Function: loadDemoKernel() (Lines 1933-1940)

async loadDemoKernel() {
    const zipResponse = await fetch('kernel.zip');
    if (zipResponse.ok) {
        const zipBlob = await zipResponse.blob();
        const zipFile = new File([zipBlob], 'kernel.zip');
        await this.handleZipUpload(zipFile);
    }
}

2. Extracting Files from ZIP

Function: handleZipUpload() (Lines 3258-3326)

async handleZipUpload(file) {
    const JSZip = window.JSZip;  // Uses JSZip library
    const zip = await JSZip.loadAsync(file);

    // Process each file in ZIP
    for (const [filename, zipEntry] of Object.entries(zip.files)) {
        const basename = filename.split('/').pop();

        // Source files (.s, .asm, .c) → handleSourceUpload()
        if (filename.endsWith('.s') || filename.endsWith('.asm') || filename.endsWith('.c')) {
            const content = await zipEntry.async('string');
            await this.handleSourceUpload(new File([content], basename));
            counts.source++;
        }

        // Map files (.map) → handleMapUpload()
        else if (filename.endsWith('.map')) {
            const content = await zipEntry.async('string');
            await this.handleMapUpload(new File([content], basename));
            counts.map++;
        }

        // Binary executables (.o, .out, no extension) → loadAoutFromBuffer()
        else if (filename.endsWith('.o') || filename.endsWith('.out') ||
                 !filename.includes('.')) {
            const bytes = await zipEntry.async('uint8array');
            await this.loadAoutFromBuffer(bytes, basename);
            counts.executable++;
        }
    }

    this.updateSourceDropdown();  // Populate UI dropdowns
}

3. Storing Source Files (.c and .s)

Function: handleSourceUpload() (Lines 3205-3227)

async handleSourceUpload(file) {
    const content = await file.text();
    const filename = file.name;

    // Store in JavaScript Map (for UI access)
    this.sourceFiles.set(filename, content);

    // Store in WASM backend (for C debugger access)
    const result = this.module.ccall(
        'nd500_dbg_store_source_js',  // WASM function
        'number',                      // Return type
        ['string', 'string'],          // Parameter types
        [filename, content]            // Parameters
    );

    if (result !== 0) {
        throw new Error('Failed to store source file in WASM');
    }
}

Dual Storage:

  1. JavaScript Map (this.sourceFiles) - For UI display and dropdown population
  2. WASM Backend - For C debugger to access during disassembly/debugging

4. Making Files Available in UI

Function: updateSourceDropdown() (Lines 3139-3186)

updateSourceDropdown() {
    const asmDropdown = document.getElementById('asm-file-dropdown');
    const cDropdown = document.getElementById('c-file-dropdown');

    // Clear existing options
    asmDropdown.innerHTML = '<option value="">Select assembly file...</option>';
    cDropdown.innerHTML = '<option value="">Select C file...</option>';

    // Populate dropdowns based on file extension
    for (const filename of this.sourceFiles.keys()) {
        const ext = filename.substring(filename.lastIndexOf('.'));
        const option = document.createElement('option');
        option.value = filename;
        option.textContent = filename;

        if (ext === '.s' || ext === '.asm') {
            asmDropdown.appendChild(option);  // Assembly dropdown
        } else if (ext === '.c') {
            cDropdown.appendChild(option);     // C source dropdown
        }
    }
}

5. Displaying Source Files

Function: showSourceFile() (Lines 3187-3203)

showSourceFile(filename) {
    const content = this.sourceFiles.get(filename);
    if (!content) {
        console.error(`Source file not found: ${filename}`);
        return;
    }

    const sourceViewer = document.getElementById('source-viewer');
    sourceViewer.textContent = content;  // Display in UI

    // Syntax highlighting (if Prism.js loaded)
    if (window.Prism) {
        Prism.highlightElement(sourceViewer);
    }
}

Current kernel.zip Contents

Location: src/frontend/nd500wasm/web/demo/kernel.zip

Contents (6 files, 40,431 bytes):

File Size Type Processing
locore.c 2,702 bytes C source → handleSourceUpload() → stored in sourceFiles + WASM
locore.s 499 bytes Assembly → handleSourceUpload() → stored in sourceFiles + WASM
kernel.c 8,553 bytes C source → handleSourceUpload() → stored in sourceFiles + WASM
kernel.s 10,825 bytes Assembly → handleSourceUpload() → stored in sourceFiles + WASM
kernel 1,624 bytes Executable → loadAoutFromBuffer() → WASM filesystem
kernel.map 16,228 bytes Map file → handleMapUpload() → stored in mapFiles + WASM

File Type Detection Logic

File extension mapping:

// Source files (text storage)
.c      → C source      → handleSourceUpload()
.s      → Assembly      → handleSourceUpload()
.asm    → Assembly      → handleSourceUpload()

// Map files (text storage)
.map    → Symbol map    → handleMapUpload()

// Binary files (binary storage)
.o      → Object file   → loadAoutFromBuffer()
.out    → Executable    → loadAoutFromBuffer()
(no ext)→ Executable    → loadAoutFromBuffer()  // e.g., "kernel"

WASM Backend Integration

WASM Functions Called

  1. nd500_dbg_store_source_js(filename, content)

    • Stores source file in WASM backend
    • Used by disassembler to show source alongside assembly
    • Returns 0 on success, non-zero on failure
  2. nd500_dbg_store_map_js(filename, content)

    • Stores map file in WASM backend
    • Maps assembly line numbers to source line numbers
    • Returns 0 on success, non-zero on failure

WASM File System

Binary executables written to WASM virtual filesystem:

this.module.FS.writeFile(`/${filename}`, bytes);

Then loaded via C debugger API:

const result = this.module.ccall('nd500_dbg_load_aout_js', 'number',
                                  ['string'], [filename]);

Verification

All Required Files Present

✓ locore.c - Bootstrap C source (for source-level debugging) ✓ locore.s - Bootstrap assembly (for instruction-level debugging) ✓ kernel.c - Kernel C source (for source-level debugging) ✓ kernel.s - Kernel assembly (for instruction-level debugging) ✓ kernel - Executable binary (for execution) ✓ kernel.map - Symbol map (for address-to-source mapping)

Processing Confirmed

The JavaScript correctly:

  1. ✓ Extracts all files from kernel.zip
  2. ✓ Detects file types by extension
  3. ✓ Stores .c and .s files as text (dual storage: JS + WASM)
  4. ✓ Stores .map files as text (dual storage: JS + WASM)
  5. ✓ Stores binary executables in WASM filesystem
  6. ✓ Populates UI dropdowns with source files
  7. ✓ Makes all files available to debugger/disassembler

UI Components

HTML Elements (index.html)

File upload:

<input type="file" id="zipFileInput" accept=".zip">

Source file dropdowns:

<select id="asm-file-dropdown"></select>  <!-- Assembly files -->
<select id="c-file-dropdown"></select>    <!-- C source files -->

Source viewer:

<pre id="source-viewer" class="language-c"></pre>

User Workflow

  1. Click "Load Demo Kernel" or upload custom kernel.zip
  2. JavaScript extracts all files
  3. Dropdowns populate with available .c and .s files
  4. User selects file from dropdown
  5. Source code displays in viewer with syntax highlighting
  6. Debugger/disassembler can cross-reference source with assembly

Dependencies

External Libraries

JSZip - ZIP file extraction

<script src="https://cdn.jsdelivr.net/npm/jszip@3.10.1/dist/jszip.min.js"></script>

Prism.js - Syntax highlighting (optional)

<link rel="stylesheet" href="prism.css">
<script src="prism.js"></script>

WASM Module

Must export these functions:

  • nd500_dbg_store_source_js()
  • nd500_dbg_store_map_js()
  • nd500_dbg_load_aout_js()

Testing

Test File

Location: src/frontend/nd500wasm/web/test_zip_loading.html

Validates:

  • ZIP extraction
  • File type detection
  • Source file storage
  • Map file storage
  • Binary file loading

Console Logging

The code includes extensive logging:

console.log(`[handleZipUpload] Starting ZIP extraction: ${file.name}`);
console.log(`[handleZipUpload] ZIP file contents:`, Object.keys(zip.files));
console.log(`[handleZipUpload] Detected as SOURCE file: ${basename}`);
console.log(`[handleZipUpload] Processing complete. Counts:`, counts);
console.log(`[handleSourceUpload] Successfully uploaded: ${filename}`);

Check browser console for verification during loading.

Summary of Analysis

Question: Does the JavaScript make all .s and .c files available?

Answer: ✓ YES

The JavaScript code:

  1. ✓ Correctly extracts .c and .s files from kernel.zip
  2. ✓ Stores them in dual locations (JavaScript Map + WASM backend)
  3. ✓ Makes them available to UI (dropdowns, source viewer)
  4. ✓ Makes them available to debugger/disassembler (WASM backend)
  5. ✓ Handles all 4 source files in current kernel.zip:
    • locore.c
    • locore.s
    • kernel.c
    • kernel.s

Deployed kernel.zip contents verified:

  • ✓ All 6 required files present
  • ✓ Checksum: 052dc6146f636ee59531896542b100cf
  • ✓ Deployed to all 3 locations

No changes needed - The JavaScript implementation is complete and correct.