void vm_hw_lem1802_init(vm_t* vm) { struct lem1802_hardware* hw; hw = malloc(sizeof(struct lem1802_hardware)); hw->cycle_hook = 0; hw->write_hook = 0; hw->break_hook = 0; hw->vm = vm; // setting up some status values hw->blink_status = 1; hw->blink_tick = 0; hw->border_color = 0; // hw->border_was_updated = 1; hw->screen_was_updated = 1; hw->texture_has_changed = 1; hw->window_closed = 0; hw->context = NULL; // Set up the LEM1802 hardware information. hw->device.id = LEM1802_ID; hw->device.version = LEM1802_VERSION; hw->device.manufacturer = LEM1802_MANUFACTURER; hw->device.handler = &vm_hw_lem1802_interrupt; hw->device.free_handler = &vm_hw_lem1802_free; // set userdata to lem1820 struct hw->device.userdata = hw; // register hooks and hw hw->cycle_hook = vm_hook_register(vm, &vm_hw_lem1802_cycle, HOOK_ON_60HZ, hw); hw->write_hook = vm_hook_register(vm, &vm_hw_lem1802_write, HOOK_ON_WRITE, hw); hw->break_hook = vm_hook_register(vm, &vm_hw_lem1802_break, HOOK_ON_BREAK, hw); hw->hw_id = vm_hw_register(vm, &hw->device); hw->glfw_texture = malloc(HW_LEM1802_SCREEN_TEXTURE_MEM_SIZE); // Initialize the memory for LEM1802. vm_hw_lem1802_mem_init(hw); // Init GLFW and OpenGL vm_hw_lem1802_init_glfw(hw); // Set up the keyboard. vm_hw_keyboard_init(vm); glfwSetKeyCallback(&vm_hw_keyboard_handle_key); glfwSetCharCallback(&vm_hw_keyboard_handle_char); vm_hook_fire(hw->vm, hw->hw_id, HOOK_ON_HARDWARE_CHANGE, hw); }
struct lua_hardware* vm_hw_lua_load(vm_t* vm, bstring name) { bstring path, modtype; struct lua_hardware* hw; int module, hwinfo; // Calculate full path to file. path = osutil_getarg0path(); #ifdef _WIN32 bcatcstr(path, "modules\\"); #else bcatcstr(path, "/modules/"); #endif bconcat(path, name); // Create the new lua hardware structure. hw = malloc(sizeof(struct lua_hardware)); hw->vm = vm; hw->state = lua_open(); assert(hw->state != NULL); luaL_openlibs(hw->state); luaX_loadexpressionlib(hw->state); // Execute the code in the new Lua context. if (luaL_dofile(hw->state, path->data) != 0) { printf("lua error was %s.\n", lua_tostring(hw->state, -1)); // Return NULL. lua_close(hw->state); free(hw); bdestroy(path); return NULL; } // Load tables. lua_getglobal(hw->state, "MODULE"); module = lua_gettop(hw->state); lua_getglobal(hw->state, "HARDWARE"); hwinfo = lua_gettop(hw->state); // Ensure both tables were provided. if (lua_isnoneornil(hw->state, module) || lua_isnoneornil(hw->state, hwinfo)) { printf("failed to load hardware from %s.\n", path->data); // Return NULL. lua_close(hw->state); free(hw); bdestroy(path); return NULL; } // Check to see whether the module is // a hardware module. lua_getfield(hw->state, module, "Type"); modtype = bfromcstr(lua_tostring(hw->state, -1)); if (!biseqcstrcaseless(modtype, "Hardware")) { // Return NULL. lua_pop(hw->state, 1); lua_close(hw->state); free(hw); bdestroy(modtype); bdestroy(path); return NULL; } lua_pop(hw->state, 1); bdestroy(modtype); // Store information into the Lua // hardware structure. lua_getfield(hw->state, hwinfo, "ID"); lua_getfield(hw->state, hwinfo, "Version"); lua_getfield(hw->state, hwinfo, "Manufacturer"); hw->device.id = (uint32_t)lua_tonumber(hw->state, lua_gettop(hw->state) - 2); hw->device.version = (uint16_t)lua_tonumber(hw->state, lua_gettop(hw->state) - 1); hw->device.manufacturer = (uint32_t)lua_tonumber(hw->state, lua_gettop(hw->state)); lua_pop(hw->state, 3); printf("hardware loaded ID: %u, Version: %u, Manufacturer: %u.\n", hw->device.id, hw->device.version, hw->device.manufacturer); // Register the hardware. hw->device.handler = &vm_hw_lua_interrupt; hw->device.userdata = hw; vm_hw_register(vm, hw->device); // Register the hooks. hw->cycle_id = vm_hook_register(vm, &vm_hw_lua_cycle, HOOK_ON_POST_CYCLE, hw); hw->write_id = vm_hook_register(vm, &vm_hw_lua_write, HOOK_ON_WRITE, hw); // Pop tables from stack. lua_pop(hw->state, 2); // Return new hardware. return hw; }