Files
background.js
content.css
content.js
mame_generator.css
mame_generator.html
mame_generator.js
manifest.json
Code
7 1,548
mame_generator.js
/**
* MAME4Droid (0.139u1) AI Homebrew ROM Generator
* Generates MAME-compatible ROM packages from user-configured parameters.
*/
// ============================================================
// STATE
// ============================================================
const state = {
palette: 0,
gameType: 0,
gameName: 'HOMEBREW',
manufacturer: 'AI STUDIO',
year: 2024,
enemyCount: 8,
gameSpeed: 5,
levelCount: 8,
lives: 3,
generating: false,
lastRomData: null,
};
// ============================================================
// PALETTE DEFINITIONS (4-color per palette, 80s arcade style)
// ============================================================
const PALETTES = [
{ name: 'CLASSIC', colors: [0xff0040, 0xff8000, 0xffff00, 0x00ff80] },
{ name: 'NEON', colors: [0x00ccff, 0xff44ff, 0xffcc00, 0x44ff44] },
{ name: 'FIRE', colors: [0xff6600, 0xff0066, 0xffcc00, 0xffffff] },
{ name: 'RETRO', colors: [0xff00ff, 0x00ffff, 0xffffff, 0xffff00] },
];
const GAME_TYPES = ['SHOOTER', 'MAZE', 'PLATFORM', 'BALL'];
// ============================================================
// UI BINDING
// ============================================================
const $ = (id) => document.getElementById(id);
function initUI() {
// Sliders
const sliders = [
{ id: 'enemyCount', valId: 'enemyCountVal', fmt: (v) => String(v).padStart(2, '0') },
{ id: 'gameSpeed', valId: 'gameSpeedVal', fmt: (v) => String(v).padStart(2, '0') },
{ id: 'levelCount', valId: 'levelCountVal', fmt: (v) => String(v).padStart(2, '0') },
{ id: 'lives', valId: 'livesVal', fmt: (v) => String(v).padStart(2, '0') },
];
sliders.forEach(({ id, valId, fmt }) => {
const slider = $(id);
const valEl = $(valId);
if (slider && valEl) {
slider.addEventListener('input', () => {
const v = parseInt(slider.value, 10);
valEl.textContent = fmt(v);
state[id] = v;
updateRomInfo();
});
}
});
// Text inputs
const textInputs = ['gameName', 'manufacturer'];
textInputs.forEach((id) => {
const el = $(id);
if (el) {
el.addEventListener('input', () => {
state[id === 'gameName' ? 'gameName' : 'manufacturer'] = el.value.toUpperCase();
updateRomInfo();
});
}
});
// Year
const yearEl = $('year');
if (yearEl) {
yearEl.addEventListener('input', () => {
state.year = parseInt(yearEl.value, 10) || 2024;
updateRomInfo();
});
}
// Palette selection
document.querySelectorAll('.palette-opt').forEach((el) => {
el.addEventListener('click', () => {
document.querySelectorAll('.palette-opt').forEach((p) => p.classList.remove('selected'));
el.classList.add('selected');
state.palette = parseInt(el.dataset.palette, 10);
updateRomInfo();
});
});
// Game type selection
document.querySelectorAll('.type-opt').forEach((el) => {
el.addEventListener('click', () => {
document.querySelectorAll('.type-opt').forEach((t) => t.classList.remove('selected'));
el.classList.add('selected');
state.gameType = parseInt(el.dataset.type, 10);
updateRomInfo();
});
});
// Generate button
const genBtn = $('generateBtn');
if (genBtn) {
genBtn.addEventListener('click', generateROM);
}
// Arcade buttons (A, B, C, START) — play sounds
document.querySelectorAll('.btn-arcade').forEach((btn) => {
btn.addEventListener('click', () => playBlip());
});
updateRomInfo();
}
// ============================================================
// AUDIO (Web Audio API — retro blips)
// ============================================================
let audioCtx = null;
function getAudioCtx() {
if (!audioCtx) {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
}
return audioCtx;
}
function playBlip(freq, duration, type) {
try {
const ctx = getAudioCtx();
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = type || 'square';
osc.frequency.setValueAtTime(freq || 800, ctx.currentTime);
gain.gain.setValueAtTime(0.08, ctx.currentTime);
gain.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + (duration || 0.08));
osc.connect(gain);
gain.connect(ctx.destination);
osc.start();
osc.stop(ctx.currentTime + (duration || 0.08));
} catch (e) {
// Audio not available
}
}
function playGenerateSound() {
playBlip(400, 0.1, 'square');
setTimeout(() => playBlip(600, 0.1, 'square'), 100);
setTimeout(() => playBlip(800, 0.15, 'square'), 200);
setTimeout(() => playBlip(1200, 0.2, 'square'), 350);
}
function playDoneSound() {
playBlip(1200, 0.1, 'square');
setTimeout(() => playBlip(1500, 0.15, 'square'), 120);
setTimeout(() => playBlip(1800, 0.2, 'square'), 250);
}
// ============================================================
// ROM GENERATION
// ============================================================
/**
* Generates the complete ROM binary data based on user configuration.
* Format: 256-byte header + variable game data.
*/
function generateROMData() {
const palette = PALETTES[state.palette];
const gameType = state.gameType;
// --- HEADER (256 bytes) ---
const header = new Uint8Array(256);
const enc = new TextEncoder();
// Bytes 0-7: Magic "MAMEROM\0"
header.set(enc.encode('MAMEROM\0'), 0);
// Byte 8: Version (0x01)
header[8] = 0x01;
// Byte 9: Game type
header[9] = gameType;
// Byte 10: Enemy count
header[10] = state.enemyCount;
// Byte 11: Game speed
header[11] = state.gameSpeed;
// Byte 12: Level count
header[12] = state.levelCount;
// Byte 13: Lives
header[13] = state.lives;
// Byte 14: Palette index
header[14] = state.palette;
// Bytes 16-23: Game name (8 bytes, padded with spaces)