@@ -157,14 +157,19 @@ pub fn PioImpl(EnumType: type, chip: Chip) type {
157157 break offset ;
158158 } else error .NoSpace ;
159159 }
160+ inline fn is_jmp (insn : u16 ) bool {
161+ return (insn & 0xe000 ) == 0 ;
162+ }
160163
161164 pub fn add_program_at_offset_unlocked (self : EnumType , program : Program , offset : u5 ) ! void {
162165 if (! self .can_add_program_at_offset (program , offset ))
163166 return error .NoSpace ;
164167
165168 const instruction_memory = self .get_instruction_memory ();
166- for (program .instructions , offset .. ) | insn , i |
167- instruction_memory [i ] = insn ;
169+ for (program .instructions , offset .. ) | insn , i | {
170+ // offset jump instructions since they are absolute
171+ instruction_memory [i ] = if (is_jmp (insn )) insn + offset else insn ;
172+ }
168173
169174 const program_mask = program .get_mask ();
170175 UsedInstructionSpace (chip ).val [@intFromEnum (self )] |= program_mask << offset ;
@@ -548,12 +553,12 @@ pub fn PioImpl(EnumType: type, chip: Chip) type {
548553 .pin_mappings = options .pin_mappings ,
549554 .exec = .{
550555 .wrap = if (program .wrap ) | wrap |
551- wrap
556+ wrap + offset // program.wrap is relative but actual wrap is absolute
552557 else
553558 offset + @as (u5 , @intCast (program .instructions .len )),
554559
555560 .wrap_target = if (program .wrap_target ) | wrap_target |
556- wrap_target
561+ wrap_target + offset // program.wrap_target is relative but actual wrap is absolute
557562 else
558563 offset ,
559564
0 commit comments