Skip to content

Commit

Permalink
app: add examples for egg command (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
icexin authored Aug 7, 2021
1 parent a0ffbaf commit 9b479c4
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*.img
*.hdd

eggos.iso
*.iso
egg
bootblock
serial
trace.txt
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ Run `egg build -o kernel.elf` in your project directory to get the kernel file,

`egg pack -o eggos.iso -k kernel.elf` can pack the kernel into an iso file, and then you can use https://github.com/ventoy/Ventoy to run the iso file on a bare metal.

Here are some examples [examples](./app/examples)

Happy hacking!

# Debug
Expand Down
2 changes: 2 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ $ mage qemu

`egg pack -o eggos.iso -k kernel.elf` 可以将内核打包成一个iso文件,通过 https://github.com/ventoy/Ventoy 即可运行在真实的机器上。

这里是一些例子[例子](./app/examples)

Happy hacking!

# Debug
Expand Down
50 changes: 50 additions & 0 deletions app/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
This directory contains examples of how `eggos` can be used to write common go applications.

# helloworld

``` sh
$ cd helloworld
$ egg run
```

# concurrent prime sieve

``` sh
$ cd prime-sieve
$ egg run
```

# http server

``` sh
$ cd httpd
$ egg run -p 8000:8000
```

Access this address from browser http://127.0.0.1:8000/hello

# simple repl program

``` sh
$ cd repl
$ egg run
```

# simple animation

Code from `The Go Programming Language`

``` sh
$ cd graphic
$ egg pack -o graphic.iso
$ egg run graphic.iso
```

# handle syscall

This example shows how to add or modify syscall

``` sh
$ cd syscall
$ egg run
```
55 changes: 55 additions & 0 deletions app/examples/graphic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"image"
"image/color"
"image/draw"
"math"
"math/rand"
"time"

"github.com/icexin/eggos/drivers/vbe"
)

var palette = []color.Color{color.White, color.Black}

const (
whiteIndex = 0 // first color in palette
blackIndex = 1 // next color in palette
)

func main() {
// The sequence of images is deterministic unless we seed
// the pseudo-random number generator using the current time.
// Thanks to Randall McPherson for pointing out the omission.
rand.Seed(time.Now().UTC().UnixNano())
canvas := vbe.DefaultView.Canvas()
lissajous(canvas)
}

func lissajous(canvas draw.Image) {
const (
cycles = 5 // number of complete x oscillator revolutions
res = 0.001 // angular resolution
size = 100 // image canvas covers [-size..+size]
nframes = 64 // number of animation frames
delay = 8 // delay between frames in 10ms units
)

freq := rand.Float64() * 3.0 // relative frequency of y oscillator
phase := 0.0 // phase difference
for {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
blackIndex)
}
phase += 0.1
time.Sleep(time.Duration(delay*10) * time.Millisecond)
draw.Draw(canvas, img.Rect, img, image.ZP, draw.Src)
vbe.DefaultView.CommitRect(img.Rect)
}
}
7 changes: 7 additions & 0 deletions app/examples/helloworld/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("hello eggos")
}
15 changes: 15 additions & 0 deletions app/examples/httpd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"
"io"
"net/http"
)

func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "hello eggos")
})
fmt.Println("http server listen on :8000")
http.ListenAndServe(":8000", nil)
}
36 changes: 36 additions & 0 deletions app/examples/prime-sieve/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// A concurrent prime sieve

package main

import "fmt"

// Send the sequence 2, 3, 4, ... to channel 'ch'.
func Generate(ch chan<- int) {
for i := 2; ; i++ {
ch <- i // Send 'i' to channel 'ch'.
}
}

// Copy the values from channel 'in' to channel 'out',
// removing those divisible by 'prime'.
func Filter(in <-chan int, out chan<- int, prime int) {
for {
i := <-in // Receive value from 'in'.
if i%prime != 0 {
out <- i // Send 'i' to 'out'.
}
}
}

// The prime sieve: Daisy-chain Filter processes.
func main() {
ch := make(chan int) // Create a new channel.
go Generate(ch) // Launch Generate goroutine.
for i := 0; i < 10; i++ {
prime := <-ch
fmt.Println(prime)
ch1 := make(chan int)
go Filter(ch, ch1, prime)
ch = ch1
}
}
16 changes: 16 additions & 0 deletions app/examples/repl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"crypto/sha1"
"fmt"
)

func main() {
var s string
for {
fmt.Print(">>> ")
fmt.Scan(&s)
sum := sha1.Sum([]byte(s))
fmt.Printf("sha1(%s) = %x\n", s, sum)
}
}
25 changes: 25 additions & 0 deletions app/examples/syscall/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"os"
"syscall"

"github.com/icexin/eggos/kernel/isyscall"
)

var handler isyscall.Handler

func handleUname(req *isyscall.Request) {
fmt.Println("syscall `uname` called")
handler(req)
}

func main() {
handler = isyscall.GetHandler(syscall.SYS_UNAME)
isyscall.Register(syscall.SYS_UNAME, handleUname)
_, err := os.Hostname()
if err != nil {
panic(err)
}
}

0 comments on commit 9b479c4

Please sign in to comment.