Compare commits
4 Commits
b79d717f1a
...
d40ea2a8ae
Author | SHA1 | Date |
---|---|---|
Danila Fedorin | d40ea2a8ae | |
Danila Fedorin | eb32577d38 | |
Danila Fedorin | e740fd7688 | |
Danila Fedorin | 5ce0d41a53 |
|
@ -1,9 +1,12 @@
|
||||||
sprite dum [
|
sprite dum [
|
||||||
` xx `
|
` x x `
|
||||||
` xx `
|
` x x `
|
||||||
` xx `
|
` x x `
|
||||||
|
` `
|
||||||
|
`x x`
|
||||||
|
` xxxxxx `
|
||||||
]
|
]
|
||||||
|
|
||||||
fun main(): u0 {
|
fun main(): u0 {
|
||||||
|
draw_sprite(dum, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
sprite dum [
|
||||||
|
` x x `
|
||||||
|
` x x `
|
||||||
|
` x x `
|
||||||
|
` `
|
||||||
|
`x x`
|
||||||
|
` xxxxxx `
|
||||||
|
]
|
||||||
|
|
||||||
|
fun main(): u8 {
|
||||||
|
var x = 0;
|
||||||
|
var y = 5;
|
||||||
|
var vy = 0;
|
||||||
|
var ay = 1;
|
||||||
|
var mode = 0;
|
||||||
|
while(1) {
|
||||||
|
draw_sprite(dum, x, y);
|
||||||
|
set_delay(1);
|
||||||
|
while(get_delay()){}
|
||||||
|
draw_sprite(dum, x, y);
|
||||||
|
if(5 - y) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
mode = 0;
|
||||||
|
}
|
||||||
|
if(26 - y) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
mode = 1;
|
||||||
|
}
|
||||||
|
if(mode) {
|
||||||
|
vy = vy - ay;
|
||||||
|
y = y - vy;
|
||||||
|
} else {
|
||||||
|
y = y + vy;
|
||||||
|
vy = vy + ay;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
sprite dum [
|
||||||
|
` x x `
|
||||||
|
` x x `
|
||||||
|
` x x `
|
||||||
|
` `
|
||||||
|
`x x`
|
||||||
|
` xxxxxx `
|
||||||
|
]
|
||||||
|
|
||||||
|
fun main(): u8 {
|
||||||
|
var x = 0;
|
||||||
|
var y = 27;
|
||||||
|
var vx = 1;
|
||||||
|
var vy = 0;
|
||||||
|
var a = 1;
|
||||||
|
while(1) {
|
||||||
|
var draw_y = 32 - y;
|
||||||
|
draw_sprite(dum, x, draw_y);
|
||||||
|
set_delay(1);
|
||||||
|
while(get_delay()){}
|
||||||
|
|
||||||
|
vy = vy - a;
|
||||||
|
y = y + vy;
|
||||||
|
x = x + vx;
|
||||||
|
if(6-y) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
var vtemp = vy;
|
||||||
|
vy = 0 - vtemp;
|
||||||
|
vy = vy + 1;
|
||||||
|
}
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
}
|
|
@ -67,6 +67,8 @@ module Chalk
|
||||||
table.set_function "get_delay", FunctionEntry.new Builtin::InlineGetDelayFunction.new
|
table.set_function "get_delay", FunctionEntry.new Builtin::InlineGetDelayFunction.new
|
||||||
table.set_function "set_sound", FunctionEntry.new Builtin::InlineSetSoundFunction.new
|
table.set_function "set_sound", FunctionEntry.new Builtin::InlineSetSoundFunction.new
|
||||||
table.set_function "draw_number", FunctionEntry.new Builtin::InlineDrawNumberFunction.new
|
table.set_function "draw_number", FunctionEntry.new Builtin::InlineDrawNumberFunction.new
|
||||||
|
table.set_function "draw_sprite", FunctionEntry.new Builtin::InlineDrawSpriteFunction.new
|
||||||
|
table.set_function "clear", FunctionEntry.new Builtin::InlineClearFunction.new
|
||||||
return table
|
return table
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -80,7 +82,7 @@ module Chalk
|
||||||
@logger.debug("Generating code for #{tree.name}")
|
@logger.debug("Generating code for #{tree.name}")
|
||||||
code = generator.generate!
|
code = generator.generate!
|
||||||
code << instruction
|
code << instruction
|
||||||
return optimizer.optimize(code)
|
return code # optimizer.optimize(code)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generate code for a builtin function. Neither the *table* nor the *instruction*
|
# Generate code for a builtin function. Neither the *table* nor the *instruction*
|
||||||
|
@ -180,15 +182,6 @@ module Chalk
|
||||||
names = collect_calls(table)
|
names = collect_calls(table)
|
||||||
names.delete "main"
|
names.delete "main"
|
||||||
|
|
||||||
sprite_bytes = [] of UInt8
|
|
||||||
offset = 0
|
|
||||||
table.sprites.each do |k, v|
|
|
||||||
data = v.sprite.encode
|
|
||||||
v.offset = offset
|
|
||||||
offset += data.size
|
|
||||||
sprite_bytes.concat data
|
|
||||||
end
|
|
||||||
|
|
||||||
main_entry = table.get_function?("main").not_nil!
|
main_entry = table.get_function?("main").not_nil!
|
||||||
all_instructions.concat create_code(main_entry.function.as(Trees::TreeFunction),
|
all_instructions.concat create_code(main_entry.function.as(Trees::TreeFunction),
|
||||||
table, Ir::JumpRelativeInstruction.new 0)
|
table, Ir::JumpRelativeInstruction.new 0)
|
||||||
|
@ -203,9 +196,19 @@ module Chalk
|
||||||
all_instructions << Ir::ReturnInstruction.new
|
all_instructions << Ir::ReturnInstruction.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sprite_bytes = [] of UInt8
|
||||||
|
offset = 0
|
||||||
|
table.sprites.each do |k, v|
|
||||||
|
data = v.sprite.encode
|
||||||
|
v.addr = offset + all_instructions.size * 2
|
||||||
|
offset += data.size
|
||||||
|
sprite_bytes.concat data
|
||||||
|
end
|
||||||
|
|
||||||
binary = [] of UInt8
|
binary = [] of UInt8
|
||||||
file = File.open(@config.output, "w")
|
file = File.open(@config.output, "w")
|
||||||
generate_binary(table, all_instructions, binary)
|
generate_binary(table, all_instructions, binary)
|
||||||
|
binary.concat sprite_bytes
|
||||||
binary.each do |byte|
|
binary.each do |byte|
|
||||||
file.write_byte byte
|
file.write_byte byte
|
||||||
end
|
end
|
||||||
|
|
|
@ -81,5 +81,31 @@ module Chalk
|
||||||
return Compiler::FunctionType.new([Compiler::Type::U8] * 3, Compiler::Type::U0)
|
return Compiler::FunctionType.new([Compiler::Type::U8] * 3, Compiler::Type::U0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class InlineDrawSpriteFunction < InlineFunction
|
||||||
|
def generate!(emitter, params, table, target, free)
|
||||||
|
raise "First parameter should be a sprite name." if !params[0].is_a?(Trees::TreeId)
|
||||||
|
sprite_name = params[0].as(Trees::TreeId).id
|
||||||
|
sprite = table.get_sprite?(sprite_name).not_nil!.sprite
|
||||||
|
emitter.generate! params[1], table, free, free + 1
|
||||||
|
emitter.generate! params[2], table, free + 1, free + 2
|
||||||
|
emitter.instructions << Ir::SetISpriteInstruction.new params[0].as(Trees::TreeId).id
|
||||||
|
emitter.instructions << Ir::DrawInstruction.new free, free + 1, sprite.height.to_i32
|
||||||
|
end
|
||||||
|
|
||||||
|
def type
|
||||||
|
return Compiler::FunctionType.new([Compiler::Type::U8] * 3, Compiler::Type::U0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class InlineClearFunction < InlineFunction
|
||||||
|
def generate!(emitter, params, table, target, free)
|
||||||
|
emitter.instructions << Ir::ClearInstruction.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def type
|
||||||
|
return Compiler::FunctionType.new(([] of Compiler::Type), Compiler::Type::U0)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -297,6 +297,21 @@ module Chalk
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class SetISpriteInstruction < Instruction
|
||||||
|
getter name
|
||||||
|
|
||||||
|
def initialize(@name : String)
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s(io)
|
||||||
|
io << "setispr " << @name
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_bin(table, stack, index)
|
||||||
|
return 0xa000 | (table.get_sprite?(@name).not_nil!.addr + 0x200)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Instruction to add a register to I.
|
# Instruction to add a register to I.
|
||||||
class AddIRegInstruction < Instruction
|
class AddIRegInstruction < Instruction
|
||||||
def initialize(@reg : Int32)
|
def initialize(@reg : Int32)
|
||||||
|
|
|
@ -5,6 +5,10 @@ module Chalk
|
||||||
@pixels = Hash(UInt8, UInt8).new(default_value: 0_u8)
|
@pixels = Hash(UInt8, UInt8).new(default_value: 0_u8)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def height
|
||||||
|
return (@pixels.keys.max || 0_u8) + 1
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(string, blank_char = ' ')
|
def initialize(string, blank_char = ' ')
|
||||||
@pixels = Hash(UInt8, UInt8).new(default_value: 0_u8)
|
@pixels = Hash(UInt8, UInt8).new(default_value: 0_u8)
|
||||||
string.split("\n").each_with_index do |s, i|
|
string.split("\n").each_with_index do |s, i|
|
||||||
|
|
|
@ -36,9 +36,9 @@ module Chalk
|
||||||
|
|
||||||
class SpriteEntry
|
class SpriteEntry
|
||||||
property sprite : Sprite
|
property sprite : Sprite
|
||||||
property offset : Int32
|
property addr : Int32
|
||||||
|
|
||||||
def initialize(@sprite, @offset = -1)
|
def initialize(@sprite, @addr = -1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue