Skip to content

Commit 9b479c4

Browse files
authored
app: add examples for egg command (#77)
1 parent a0ffbaf commit 9b479c4

File tree

10 files changed

+210
-1
lines changed

10 files changed

+210
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*.img
66
*.hdd
77

8-
eggos.iso
8+
*.iso
9+
egg
910
bootblock
1011
serial
1112
trace.txt

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ Run `egg build -o kernel.elf` in your project directory to get the kernel file,
8484

8585
`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.
8686

87+
Here are some examples [examples](./app/examples)
88+
8789
Happy hacking!
8890

8991
# Debug

README_CN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ $ mage qemu
8383

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

86+
这里是一些例子[例子](./app/examples)
87+
8688
Happy hacking!
8789

8890
# Debug

app/examples/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
This directory contains examples of how `eggos` can be used to write common go applications.
2+
3+
# helloworld
4+
5+
``` sh
6+
$ cd helloworld
7+
$ egg run
8+
```
9+
10+
# concurrent prime sieve
11+
12+
``` sh
13+
$ cd prime-sieve
14+
$ egg run
15+
```
16+
17+
# http server
18+
19+
``` sh
20+
$ cd httpd
21+
$ egg run -p 8000:8000
22+
```
23+
24+
Access this address from browser http://127.0.0.1:8000/hello
25+
26+
# simple repl program
27+
28+
``` sh
29+
$ cd repl
30+
$ egg run
31+
```
32+
33+
# simple animation
34+
35+
Code from `The Go Programming Language`
36+
37+
``` sh
38+
$ cd graphic
39+
$ egg pack -o graphic.iso
40+
$ egg run graphic.iso
41+
```
42+
43+
# handle syscall
44+
45+
This example shows how to add or modify syscall
46+
47+
``` sh
48+
$ cd syscall
49+
$ egg run
50+
```

app/examples/graphic/main.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"image"
5+
"image/color"
6+
"image/draw"
7+
"math"
8+
"math/rand"
9+
"time"
10+
11+
"github.com/icexin/eggos/drivers/vbe"
12+
)
13+
14+
var palette = []color.Color{color.White, color.Black}
15+
16+
const (
17+
whiteIndex = 0 // first color in palette
18+
blackIndex = 1 // next color in palette
19+
)
20+
21+
func main() {
22+
// The sequence of images is deterministic unless we seed
23+
// the pseudo-random number generator using the current time.
24+
// Thanks to Randall McPherson for pointing out the omission.
25+
rand.Seed(time.Now().UTC().UnixNano())
26+
canvas := vbe.DefaultView.Canvas()
27+
lissajous(canvas)
28+
}
29+
30+
func lissajous(canvas draw.Image) {
31+
const (
32+
cycles = 5 // number of complete x oscillator revolutions
33+
res = 0.001 // angular resolution
34+
size = 100 // image canvas covers [-size..+size]
35+
nframes = 64 // number of animation frames
36+
delay = 8 // delay between frames in 10ms units
37+
)
38+
39+
freq := rand.Float64() * 3.0 // relative frequency of y oscillator
40+
phase := 0.0 // phase difference
41+
for {
42+
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
43+
img := image.NewPaletted(rect, palette)
44+
for t := 0.0; t < cycles*2*math.Pi; t += res {
45+
x := math.Sin(t)
46+
y := math.Sin(t*freq + phase)
47+
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
48+
blackIndex)
49+
}
50+
phase += 0.1
51+
time.Sleep(time.Duration(delay*10) * time.Millisecond)
52+
draw.Draw(canvas, img.Rect, img, image.ZP, draw.Src)
53+
vbe.DefaultView.CommitRect(img.Rect)
54+
}
55+
}

app/examples/helloworld/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("hello eggos")
7+
}

app/examples/httpd/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
)
8+
9+
func main() {
10+
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
11+
io.WriteString(w, "hello eggos")
12+
})
13+
fmt.Println("http server listen on :8000")
14+
http.ListenAndServe(":8000", nil)
15+
}

app/examples/prime-sieve/main.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// A concurrent prime sieve
2+
3+
package main
4+
5+
import "fmt"
6+
7+
// Send the sequence 2, 3, 4, ... to channel 'ch'.
8+
func Generate(ch chan<- int) {
9+
for i := 2; ; i++ {
10+
ch <- i // Send 'i' to channel 'ch'.
11+
}
12+
}
13+
14+
// Copy the values from channel 'in' to channel 'out',
15+
// removing those divisible by 'prime'.
16+
func Filter(in <-chan int, out chan<- int, prime int) {
17+
for {
18+
i := <-in // Receive value from 'in'.
19+
if i%prime != 0 {
20+
out <- i // Send 'i' to 'out'.
21+
}
22+
}
23+
}
24+
25+
// The prime sieve: Daisy-chain Filter processes.
26+
func main() {
27+
ch := make(chan int) // Create a new channel.
28+
go Generate(ch) // Launch Generate goroutine.
29+
for i := 0; i < 10; i++ {
30+
prime := <-ch
31+
fmt.Println(prime)
32+
ch1 := make(chan int)
33+
go Filter(ch, ch1, prime)
34+
ch = ch1
35+
}
36+
}

app/examples/repl/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"crypto/sha1"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
var s string
10+
for {
11+
fmt.Print(">>> ")
12+
fmt.Scan(&s)
13+
sum := sha1.Sum([]byte(s))
14+
fmt.Printf("sha1(%s) = %x\n", s, sum)
15+
}
16+
}

app/examples/syscall/main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"syscall"
7+
8+
"github.com/icexin/eggos/kernel/isyscall"
9+
)
10+
11+
var handler isyscall.Handler
12+
13+
func handleUname(req *isyscall.Request) {
14+
fmt.Println("syscall `uname` called")
15+
handler(req)
16+
}
17+
18+
func main() {
19+
handler = isyscall.GetHandler(syscall.SYS_UNAME)
20+
isyscall.Register(syscall.SYS_UNAME, handleUname)
21+
_, err := os.Hostname()
22+
if err != nil {
23+
panic(err)
24+
}
25+
}

0 commit comments

Comments
 (0)