Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/m3m0r7/php-machine-emulator
A machine emulator written in PHP
https://github.com/m3m0r7/php-machine-emulator
Last synced: 5 days ago
JSON representation
A machine emulator written in PHP
- Host: GitHub
- URL: https://github.com/m3m0r7/php-machine-emulator
- Owner: m3m0r7
- Created: 2024-08-02T06:52:20.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-08-20T23:26:57.000Z (5 months ago)
- Last Synced: 2024-08-21T17:55:19.663Z (5 months ago)
- Language: PHP
- Size: 222 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# The PHP machine emulator
The PHP machine emulator is a CPU emulator for Intel x86 written in PHP.
However, since this project is grand in scale, contributions are made by [@m3m0r7](https://github.com/m3m0r7) when they have free time and are motivated.
Since it is written roughly, functionality is not guaranteed.## Requirements
- PHP 8.3+
- NASM## Quick start
1. Install this project via composer
```
$ composer require m3m0r7/php-machine-emulator
```2. Make an assembly as `HelloWorld.asm`
```asm
[bits 16]
[org 0x7C00]main:
cli
xor ax, ax
xor bx, bx
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x7C00
sti
mov si, hello_world
call print_string
hltprint_string:
lodsb
or al, al
jz .done
call .char
jmp .done
.char:
mov ah, 0x0E
int 0x10
jmp print_string
.done:
rethello_world:
db "Hello World!", 0x0D, 0x0A, 0times 510-($-$$) db 0
dw 0xAA55
```3. Make BIOS Starter as a `HelloWorld.php`
```php
getCode());
}
```4. Let's emulating CPU as following:
```
$ nasm HelloWorld.asm -o /dev/stdout | php HelloWorld.php
```5. It is shown as following:
```
Hello World!
```# Render a triangle via the VIDEO mode
For an example; You can render a pixel art from a bootloader as following:
1. Write an assembly and save as `Triangle.asm`
```asm
[bits 16]
[org 0x7C00]main:
; Setup segments
cli
xor ax, ax
xor bx, bx
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x7C00; Set the video mode with BIOS interrupt
mov ah, 0x00
mov al, 0x13
int 0x10; Move to the video memory
mov ax, 0xA000
mov es, ax
xor di, di; Set triangle memory to the source index
mov si, triangle; Looping for rows
mov cx, 10
draw_row:
; Push for rows size
push cx; Looping for cols
mov cx, 19draw_pixel:
; Load a byte from source index
lodsb
cmp al, 0xff
jne save_pixel; Specify render color - white
mov al, 0x0fsave_pixel:
; Save to ES:DI register from AL register value
stosbskip_pixel:
mov al, 0x00
loop draw_pixel; Rollback looping rows size to CX register
pop cx; Move to next line (i.e., Video Width)
add di, 320-19loop draw_row
; Infinity loop
hang:
jmp hang; 0x00 - no available to render a pixel (filling by black)
; 0xFF - render a pixel (filling by white)
triangle:
db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
db 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00
db 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00
db 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00
db 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00
db 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00
db 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFFtimes 510-($-$$) db 0
dw 0xAA55
```2. Write a PHP and save as `Triangle.php`
```php
getCode());
}
```3. Compile with NASM and run an emulator
```
$ nasm Triangle.asm -o /dev/stdout | php Triangle.php
```_NOTICE: You should expand your terminal 320x200 (Cols and Rows) in a default example assembly._
_But it is too big terminal size. Maybe you need to zoom out (`Cmd` + `-` in macOS) fully and using iTerm is better to render beautifully at currently._4. You will get result as following:
# Tests
```
./vendor/bin/phpunit --bootstrap ./tests/Bootstrap.php tests/Case
```# LICENSE
MIT