Class: Ppu

Inherits:
Object
  • Object
show all
Defined in:
lib/ppu.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePpu

Returns a new instance of Ppu.



9
10
11
12
13
14
# File 'lib/ppu.rb', line 9

def initialize()
	@ppu = [0]*0x4000
	@registers = {}
	Registers.each {|adresse| @registers[adresse] = 0}
	init_affichage
end

Instance Attribute Details

#ppuObject

Returns the value of attribute ppu.



4
5
6
# File 'lib/ppu.rb', line 4

def ppu
  @ppu
end

#registersObject

Returns the value of attribute registers.



5
6
7
# File 'lib/ppu.rb', line 5

def registers
  @registers
end

#screenObject

Returns the value of attribute screen.



6
7
8
# File 'lib/ppu.rb', line 6

def screen
  @screen
end

Instance Method Details

#clear_vblankObject



78
79
80
# File 'lib/ppu.rb', line 78

def clear_vblank()
	@registers[0x2002] = @registers[0x2002].clear_bit(7) #Clear du Vblank
end

#color?(color) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
# File 'lib/ppu.rb', line 42

def color?(color)
	rgb = case color
		when 0 then [124,124,124]
		when 1 then [0,0,22]
		when 2 then [0,0,18]
		when 3 then [64,40,188]
	end
	return rgb
end

#draw_screenObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ppu.rb', line 83

def draw_screen()
	@screen.putPixel(@x,@y,color?(get_pixel(@x,@y)))
	@x+=1
	if @x>255
		@x = 0
		@y+=1
	elsif @y>239
		reset_pixel
	end

end

#get_bit(liste_octets, ligne, bit) ⇒ Object



29
30
31
# File 'lib/ppu.rb', line 29

def get_bit(liste_octets,ligne,bit)
	return (liste_octets[ligne].bit?(7-bit) | liste_octets[ligne+8].bit?(7-bit) << 1)
end

#get_pixel(x, y) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/ppu.rb', line 33

def get_pixel(x,y)
	set_tables
	pos_tile = (y/8)*32+(x/8)
	index = @table_nommage_screen[pos_tile] * 16
	pixel = get_bit(@screen_pattern_table[index..index+15],y%8,x%8)
	return pixel
	
end

#init_affichageObject



17
18
19
20
21
# File 'lib/ppu.rb', line 17

def init_affichage()
	SDL.init SDL::INIT_VIDEO
	@screen = SDL::set_video_mode 256, 240, 32, SDL::SWSURFACE
	reset_pixel
end

#reset_pixelObject



24
25
26
# File 'lib/ppu.rb', line 24

def reset_pixel()
	@x = @y = 0
end

#set_tablesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ppu.rb', line 52

def set_tables()
	@sprite_pattern_table = case @registers[0x2000].bit?(3)
		when 0 then @ppu[0x0..0x0FFF]
		when 1 then @ppu[0x1000..0x1FFF]
	end

	@screen_pattern_table = case @registers[0x2000].bit?(4)
		when 0 then @ppu[0x0..0x0FFF]
		when 1 then @ppu[0x1000..0x1FFF]
	end
	
	@sprite_size = @registers[0x2000].bit?(5)

	@table_nommage_screen = @ppu[0x2000..0x23C0]   if @registers[0x2000].bit?(1) == 0 and @registers[0x2000].bit?(0) == 0
	@table_nommage_screen = @ppu[0x2400..0x27C0]   if @registers[0x2000].bit?(1) == 0 and @registers[0x2000].bit?(0) == 1
	@table_nommage_screen = @ppu[0x2800..0x2BC0]   if @registers[0x2000].bit?(1) == 1 and @registers[0x2000].bit?(0) == 0
	@table_nommage_screen = @ppu[0x2C00..0x2FC0]   if @registers[0x2000].bit?(1) == 1 and @registers[0x2000].bit?(0) == 1
	

end

#set_vblankObject



74
75
76
# File 'lib/ppu.rb', line 74

def set_vblank()
	@registers[0x2002] = @registers[0x2002].set_bit(7) #Set du Vblank
end